FIRFinalizePasskeySignInRequestTests.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2023 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_OSX || TARGET_OS_MACCATALYST
  18. #import <XCTest/XCTest.h>
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRFinalizePasskeySignInRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRFinalizePasskeySignInResponse.h"
  21. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  22. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  23. /**
  24. @var kTestAPIKey
  25. @brief Fake API key used for testing.
  26. */
  27. static NSString *const kTestAPIKey = @"APIKey";
  28. /**
  29. @var kTestFirebaseAppID
  30. @brief Fake Firebase app ID used for testing.
  31. */
  32. static NSString *const kTestFirebaseAppID = @"appID";
  33. /**
  34. @var kExpectedAPIURL
  35. @brief The expected URL for the test calls.
  36. */
  37. static NSString *const kExpectedAPIURL =
  38. @"https://identitytoolkit.googleapis.com/v2/accounts/passkeySignIn:finalize?key=APIKey";
  39. /**
  40. @var kAuthenticatorAuthRespKey
  41. @brief The key for authentication response object from the authenticator.
  42. */
  43. static NSString *const kAuthenticatorAuthRespKey = @"authenticatorAuthenticationResponse";
  44. /**
  45. @var kAuthAssertionRespKey
  46. @brief The key for authentication assertion from the authenticator.
  47. */
  48. static NSString *const kAuthAssertionRespKey = @"response";
  49. /**
  50. @var kCredentialID
  51. @brief credential ID.
  52. */
  53. static NSString *const kCredentialID = @"testCredentialID";
  54. /**
  55. @var kCredentialIDKey
  56. @brief credential ID field.
  57. */
  58. static NSString *const kCredentialIDKey = @"id";
  59. /**
  60. @var kRawClientDataJSON
  61. @brief CollectedClientData object from the authenticator.
  62. */
  63. static NSString *const kRawClientDataJSON = @"testRawClientDataJSON";
  64. /**
  65. @var kRawClientDataJSONKey
  66. @brief The key for the attestation object from the authenticator.
  67. */
  68. static NSString *const kRawClientDataJSONKey = @"clientDataJSON";
  69. /**
  70. @var kAuthenticatorData
  71. @brief The authenticatorData from the authenticator.
  72. */
  73. static NSString *const kAuthenticatorData = @"TestAuthenticatorData";
  74. /**
  75. @var kAuthenticatorDataKey
  76. @brief The key for authenticatorData from the authenticator.
  77. */
  78. static NSString *const kAuthenticatorDataKey = @"authenticatorData";
  79. /**
  80. @var kSignature
  81. @brief The signature from the authenticator
  82. */
  83. static NSString *const kSignature = @"testSignature";
  84. /**
  85. @var kSignatureKey
  86. @brief The key for the signature from the authenticator.
  87. */
  88. static NSString *const kSignatureKey = @"signature";
  89. /**
  90. @var kUserHandle
  91. @brief The key for the user handle.
  92. */
  93. static NSString *const kUserHandle = @"testUserHandle";
  94. /**
  95. @var kUserHandleKey
  96. @brief The key for the user handle.
  97. */
  98. static NSString *const kUserHandleKey = @"userHandle";
  99. /**
  100. @class FIRFinalizePasskeySignInRequestTests
  101. @brief Tests for @c FIRFinalizePasskeySignInRequest.
  102. */
  103. @interface FIRFinalizePasskeySignInRequestTests : XCTestCase
  104. @end
  105. @implementation FIRFinalizePasskeySignInRequestTests {
  106. /**
  107. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  108. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  109. */
  110. FIRFakeBackendRPCIssuer *_RPCIssuer;
  111. /**
  112. @brief This is the request configuration used for testing.
  113. */
  114. FIRAuthRequestConfiguration *_requestConfiguration;
  115. }
  116. - (void)setUp {
  117. [super setUp];
  118. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  119. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  120. _RPCIssuer = RPCIssuer;
  121. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  122. appID:kTestFirebaseAppID];
  123. }
  124. - (void)tearDown {
  125. _RPCIssuer = nil;
  126. _requestConfiguration = nil;
  127. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  128. [super tearDown];
  129. }
  130. - (void)testFinalizePasskeySignInRequest {
  131. if (@available(iOS 15.0, *)) {
  132. FIRFinalizePasskeySignInRequest *request =
  133. [[FIRFinalizePasskeySignInRequest alloc] initWithCredentialID:kCredentialID
  134. clientDataJson:kRawClientDataJSON
  135. authenticatorData:kAuthenticatorData
  136. signature:kSignature
  137. userID:kUserHandle
  138. requestConfiguration:_requestConfiguration];
  139. [FIRAuthBackend finalizePasskeySignIn:request
  140. callback:^(FIRFinalizePasskeySignInResponse *_Nullable response,
  141. NSError *_Nullable error){
  142. }];
  143. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  144. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  145. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAuthenticatorAuthRespKey][kCredentialIDKey],
  146. kCredentialID);
  147. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAuthenticatorAuthRespKey]
  148. [kAuthAssertionRespKey][kRawClientDataJSONKey],
  149. kRawClientDataJSON);
  150. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAuthenticatorAuthRespKey]
  151. [kAuthAssertionRespKey][kAuthenticatorDataKey],
  152. kAuthenticatorData);
  153. XCTAssertEqualObjects(
  154. _RPCIssuer.decodedRequest[kAuthenticatorAuthRespKey][kAuthAssertionRespKey][kSignatureKey],
  155. kSignature);
  156. XCTAssertEqualObjects(
  157. _RPCIssuer.decodedRequest[kAuthenticatorAuthRespKey][kAuthAssertionRespKey][kUserHandleKey],
  158. kUserHandle);
  159. }
  160. }
  161. @end
  162. #endif