FIRVerifyAssertionResponseTests.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Copyright 2017 Google
  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 <XCTest/XCTest.h>
  17. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  18. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeResponse.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionRequest.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionResponse.h"
  22. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  23. /** @var kTestAPIKey
  24. @brief Fake API key used for testing.
  25. */
  26. static NSString *const kTestAPIKey = @"APIKey";
  27. /** @var kTestFirebaseAppID
  28. @brief Fake Firebase app ID used for testing.
  29. */
  30. static NSString *const kTestFirebaseAppID = @"appID";
  31. /** @var kProviderIDKey
  32. @brief The name of the "providerId" property in the response.
  33. */
  34. static NSString *const kProviderIDKey = @"providerId";
  35. /** @var kIDTokenKey
  36. @brief The name of the "IDToken" property in the response.
  37. */
  38. static NSString *const kIDTokenKey = @"idToken";
  39. /** @var kExpiresInKey
  40. @brief The name of the "expiresIn" property in the response.
  41. */
  42. static NSString *const kExpiresInKey = @"expiresIn";
  43. /** @var kRefreshTokenKey
  44. @brief The name of the "refreshToken" property in the response.
  45. */
  46. static NSString *const kRefreshTokenKey = @"refreshToken";
  47. /** @var kVerifiedProviderKey
  48. @brief The name of the "VerifiedProvider" property in the response.
  49. */
  50. static NSString *const kVerifiedProviderKey = @"verifiedProvider";
  51. /** @var kRawUserInfoKey
  52. @brief The name of the "rawUserInfo" property in the response.
  53. */
  54. static NSString *const kRawUserInfoKey = @"rawUserInfo";
  55. /** @var kUsernameKey
  56. @brief The name of the "username" property in the response.
  57. */
  58. static NSString *const kUsernameKey = @"username";
  59. /** @var kIsNewUserKey
  60. @brief The name of the "isNewUser" property in the response.
  61. */
  62. static NSString *const kIsNewUserKey = @"isNewUser";
  63. /** @var kTestProviderID
  64. @brief Fake provider ID used for testing.
  65. */
  66. static NSString *const kTestProviderID = @"ProviderID";
  67. /** @var kTestProviderIDToken
  68. @brief Fake provider ID token used for testing.
  69. */
  70. static NSString *const kTestProviderIDToken = @"ProviderIDToken";
  71. /** @var kTestIDToken
  72. @brief Testing ID token for verifying assertion.
  73. */
  74. static NSString *const kTestIDToken = @"ID_TOKEN";
  75. /** @var kTestExpiresIn
  76. @brief Fake token expiration time.
  77. */
  78. static NSString *const kTestExpiresIn = @"12345";
  79. /** @var kTestRefreshToken
  80. @brief Fake refresh token.
  81. */
  82. static NSString *const kTestRefreshToken = @"REFRESH_TOKEN";
  83. /** @var kTestProvider
  84. @brief Fake provider used for testing.
  85. */
  86. static NSString *const kTestProvider = @"Provider";
  87. /** @var kPhotoUrlKey
  88. @brief The name of the "PhotoUrl" property in the response.
  89. */
  90. static NSString *const kPhotoUrlKey = @"photoUrl";
  91. /** @var kTestPhotoUrl
  92. @brief The "PhotoUrl" value for testing the response.
  93. */
  94. static NSString *const kTestPhotoUrl = @"www.example.com";
  95. /** @var kUsername
  96. @brief The "username" value for testing the response.
  97. */
  98. static NSString *const kUsername = @"Joe Doe";
  99. /** @var testInvalidCredentialError
  100. @brief This is the error message the server will respond with if the IDP token or requestUri is
  101. invalid.
  102. */
  103. static NSString *const ktestInvalidCredentialError = @"INVALID_IDP_RESPONSE";
  104. /** @var kUserDisabledErrorMessage
  105. @brief This is the error message the server will respond with if the user's account has been
  106. disabled.
  107. */
  108. static NSString *const kUserDisabledErrorMessage = @"USER_DISABLED";
  109. /** @var kOperationNotAllowedErrorMessage
  110. @brief This is the error message the server will respond with if Admin disables IDP specified by
  111. provider.
  112. */
  113. static NSString *const kOperationNotAllowedErrorMessage = @"OPERATION_NOT_ALLOWED";
  114. /** @var kPasswordLoginDisabledErrorMessage
  115. @brief This is the error message the server responds with if password login is disabled.
  116. */
  117. static NSString *const kPasswordLoginDisabledErrorMessage = @"PASSWORD_LOGIN_DISABLED";
  118. /** @var kFederatedUserIDAlreadyLinkedMessage
  119. @brief This is the error message the server will respond with if the federated user ID has been
  120. already linked with another account.
  121. */
  122. static NSString *const kFederatedUserIDAlreadyLinkedMessage = @"FEDERATED_USER_ID_ALREADY_LINKED:";
  123. /** @var kAllowedTimeDifference
  124. @brief Allowed difference when comparing times because of execution time and floating point
  125. error.
  126. */
  127. static const double kAllowedTimeDifference = 0.1;
  128. /** @class FIRVerifyAssertionResponseTests
  129. @brief Tests for @c FIRVerifyAssertionResponse
  130. */
  131. @interface FIRVerifyAssertionResponseTests : XCTestCase
  132. @end
  133. @implementation FIRVerifyAssertionResponseTests {
  134. /** @var _RPCIssuer
  135. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  136. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  137. */
  138. FIRFakeBackendRPCIssuer *_RPCIssuer;
  139. /** @var _requestConfiguration
  140. @brief This is the request configuration used for testing.
  141. */
  142. FIRAuthRequestConfiguration *_requestConfiguration;
  143. }
  144. /** @fn profile
  145. @brief The "rawUserInfo" value for testing the response.
  146. */
  147. + (NSDictionary *)profile {
  148. static NSDictionary *kGoogleProfile = nil;
  149. static dispatch_once_t onceToken;
  150. dispatch_once(&onceToken, ^{
  151. kGoogleProfile = @{
  152. @"iss" : @"https://accounts.google.com\\",
  153. @"email" : @"test@email.com",
  154. @"given_name" : @"User",
  155. @"family_name" : @"Doe"
  156. };
  157. });
  158. return kGoogleProfile;
  159. }
  160. - (void)setUp {
  161. [super setUp];
  162. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  163. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  164. _RPCIssuer = RPCIssuer;
  165. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  166. appID:kTestFirebaseAppID];
  167. }
  168. - (void)tearDown {
  169. _RPCIssuer = nil;
  170. _requestConfiguration = nil;
  171. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  172. [super tearDown];
  173. }
  174. /** @fn testInvalidIDPResponseError
  175. @brief This test simulates @c invalidIDPResponseError with @c FIRAuthErrorCodeInvalidIDPResponse
  176. error code.
  177. */
  178. - (void)testInvalidIDPResponseError {
  179. FIRVerifyAssertionRequest *request =
  180. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  181. requestConfiguration:_requestConfiguration];
  182. request.providerIDToken = kTestProviderIDToken;
  183. __block BOOL callbackInvoked;
  184. __block FIRVerifyAssertionResponse *RPCResponse;
  185. __block NSError *RPCError;
  186. [FIRAuthBackend
  187. verifyAssertion:request
  188. callback:^(FIRVerifyAssertionResponse *_Nullable response, NSError *_Nullable error) {
  189. callbackInvoked = YES;
  190. RPCResponse = response;
  191. RPCError = error;
  192. }];
  193. [_RPCIssuer respondWithServerErrorMessage:ktestInvalidCredentialError];
  194. XCTAssert(callbackInvoked);
  195. XCTAssertNotNil(RPCError);
  196. XCTAssertNil(RPCResponse);
  197. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidCredential);
  198. }
  199. /** @fn testUserDisabledError
  200. @brief This test simulates @c userDisabledError with @c
  201. FIRAuthErrorCodeUserDisabled error code.
  202. */
  203. - (void)testUserDisabledError {
  204. FIRVerifyAssertionRequest *request =
  205. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  206. requestConfiguration:_requestConfiguration];
  207. request.providerIDToken = kTestProviderIDToken;
  208. __block BOOL callbackInvoked;
  209. __block FIRVerifyAssertionResponse *RPCResponse;
  210. __block NSError *RPCError;
  211. [FIRAuthBackend
  212. verifyAssertion:request
  213. callback:^(FIRVerifyAssertionResponse *_Nullable response, NSError *_Nullable error) {
  214. callbackInvoked = YES;
  215. RPCResponse = response;
  216. RPCError = error;
  217. }];
  218. [_RPCIssuer respondWithServerErrorMessage:kUserDisabledErrorMessage];
  219. XCTAssert(callbackInvoked);
  220. XCTAssertNotNil(RPCError);
  221. XCTAssertNil(RPCResponse);
  222. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeUserDisabled);
  223. }
  224. #if TARGET_OS_IOS
  225. /** @fn testCredentialAlreadyInUseError
  226. @brief This test simulates a @c FIRAuthErrorCodeCredentialAlreadyInUse error.
  227. */
  228. - (void)testCredentialAlreadyInUseError {
  229. FIRVerifyAssertionRequest *request =
  230. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  231. requestConfiguration:_requestConfiguration];
  232. request.providerIDToken = kTestProviderIDToken;
  233. __block BOOL callbackInvoked;
  234. __block FIRVerifyAssertionResponse *RPCResponse;
  235. __block NSError *RPCError;
  236. [FIRAuthBackend
  237. verifyAssertion:request
  238. callback:^(FIRVerifyAssertionResponse *_Nullable response, NSError *_Nullable error) {
  239. callbackInvoked = YES;
  240. RPCResponse = response;
  241. RPCError = error;
  242. }];
  243. [_RPCIssuer respondWithServerErrorMessage:kFederatedUserIDAlreadyLinkedMessage];
  244. XCTAssert(callbackInvoked);
  245. XCTAssertNotNil(RPCError);
  246. XCTAssertNil(RPCResponse);
  247. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeCredentialAlreadyInUse);
  248. }
  249. #endif // TARGET_OS_IOS
  250. /** @fn testOperationNotAllowedError
  251. @brief This test simulates a @c FIRAuthErrorCodeOperationNotAllowed error.
  252. */
  253. - (void)testOperationNotAllowedError {
  254. FIRVerifyAssertionRequest *request =
  255. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  256. requestConfiguration:_requestConfiguration];
  257. request.providerIDToken = kTestProviderIDToken;
  258. __block BOOL callbackInvoked;
  259. __block FIRVerifyAssertionResponse *RPCResponse;
  260. __block NSError *RPCError;
  261. [FIRAuthBackend
  262. verifyAssertion:request
  263. callback:^(FIRVerifyAssertionResponse *_Nullable response, NSError *_Nullable error) {
  264. callbackInvoked = YES;
  265. RPCResponse = response;
  266. RPCError = error;
  267. }];
  268. [_RPCIssuer respondWithServerErrorMessage:kOperationNotAllowedErrorMessage];
  269. XCTAssert(callbackInvoked);
  270. XCTAssertNotNil(RPCError);
  271. XCTAssertNil(RPCResponse);
  272. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  273. }
  274. /** @fn testPasswordLoginDisabledError
  275. @brief This test simulates a @c FIRAuthErrorCodeOperationNotAllowed error.
  276. */
  277. - (void)testPasswordLoginDisabledError {
  278. FIRVerifyAssertionRequest *request =
  279. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  280. requestConfiguration:_requestConfiguration];
  281. request.providerIDToken = kTestProviderIDToken;
  282. __block BOOL callbackInvoked;
  283. __block FIRVerifyAssertionResponse *RPCResponse;
  284. __block NSError *RPCError;
  285. [FIRAuthBackend
  286. verifyAssertion:request
  287. callback:^(FIRVerifyAssertionResponse *_Nullable response, NSError *_Nullable error) {
  288. callbackInvoked = YES;
  289. RPCResponse = response;
  290. RPCError = error;
  291. }];
  292. [_RPCIssuer respondWithServerErrorMessage:kPasswordLoginDisabledErrorMessage];
  293. XCTAssert(callbackInvoked);
  294. XCTAssertNotNil(RPCError);
  295. XCTAssertNil(RPCResponse);
  296. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  297. }
  298. /** @fn testSuccessfulVerifyAssertionResponse
  299. @brief This test simulates a successful verify assertion flow.
  300. */
  301. - (void)testSuccessfulVerifyAssertionResponse {
  302. FIRVerifyAssertionRequest *request =
  303. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  304. requestConfiguration:_requestConfiguration];
  305. request.providerIDToken = kTestProviderIDToken;
  306. __block BOOL callbackInvoked;
  307. __block FIRVerifyAssertionResponse *RPCResponse;
  308. __block NSError *RPCError;
  309. [FIRAuthBackend
  310. verifyAssertion:request
  311. callback:^(FIRVerifyAssertionResponse *_Nullable response, NSError *_Nullable error) {
  312. callbackInvoked = YES;
  313. RPCResponse = response;
  314. RPCError = error;
  315. }];
  316. [_RPCIssuer respondWithJSON:@{
  317. kProviderIDKey : kTestProviderID,
  318. kIDTokenKey : kTestIDToken,
  319. kExpiresInKey : kTestExpiresIn,
  320. kRefreshTokenKey : kTestRefreshToken,
  321. kVerifiedProviderKey : @[ kTestProvider ],
  322. kPhotoUrlKey : kTestPhotoUrl,
  323. kUsernameKey : kUsername,
  324. kIsNewUserKey : @YES,
  325. kRawUserInfoKey : [[self class] profile]
  326. }];
  327. XCTAssert(callbackInvoked);
  328. XCTAssertNil(RPCError);
  329. XCTAssertNotNil(RPCResponse);
  330. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  331. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  332. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  333. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  334. XCTAssertEqualObjects(RPCResponse.verifiedProvider, @[ kTestProvider ]);
  335. XCTAssertEqualObjects(RPCResponse.photoURL, [NSURL URLWithString:kTestPhotoUrl]);
  336. XCTAssertEqualObjects(RPCResponse.username, kUsername);
  337. XCTAssertEqualObjects(RPCResponse.profile, [[self class] profile]);
  338. XCTAssertEqualObjects(RPCResponse.providerID, kTestProviderID);
  339. XCTAssertTrue(RPCResponse.isNewUser);
  340. }
  341. /** @fn testSuccessfulVerifyAssertionResponseWithTextData
  342. @brief This test simulates a successful verify assertion flow when response collection
  343. fields are sent as text values.
  344. */
  345. - (void)testSuccessfulVerifyAssertionResponseWithTextData {
  346. FIRVerifyAssertionRequest *request =
  347. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  348. requestConfiguration:_requestConfiguration];
  349. request.providerIDToken = kTestProviderIDToken;
  350. __block BOOL callbackInvoked;
  351. __block FIRVerifyAssertionResponse *RPCResponse;
  352. __block NSError *RPCError;
  353. [FIRAuthBackend
  354. verifyAssertion:request
  355. callback:^(FIRVerifyAssertionResponse *_Nullable response, NSError *_Nullable error) {
  356. callbackInvoked = YES;
  357. RPCResponse = response;
  358. RPCError = error;
  359. }];
  360. [_RPCIssuer respondWithJSON:@{
  361. kProviderIDKey : kTestProviderID,
  362. kIDTokenKey : kTestIDToken,
  363. kExpiresInKey : kTestExpiresIn,
  364. kRefreshTokenKey : kTestRefreshToken,
  365. kVerifiedProviderKey : [[self class] convertToJSONString:@[ kTestProvider ]],
  366. kPhotoUrlKey : kTestPhotoUrl,
  367. kUsernameKey : kUsername,
  368. kIsNewUserKey : @NO,
  369. kRawUserInfoKey : [[self class] convertToJSONString:[[self class] profile]]
  370. }];
  371. XCTAssert(callbackInvoked);
  372. XCTAssertNil(RPCError);
  373. XCTAssertNotNil(RPCResponse);
  374. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  375. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  376. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  377. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  378. XCTAssertEqualObjects(RPCResponse.verifiedProvider, @[ kTestProvider ]);
  379. XCTAssertEqualObjects(RPCResponse.photoURL, [NSURL URLWithString:kTestPhotoUrl]);
  380. XCTAssertEqualObjects(RPCResponse.username, kUsername);
  381. XCTAssertEqualObjects(RPCResponse.profile, [[self class] profile]);
  382. XCTAssertEqualObjects(RPCResponse.providerID, kTestProviderID);
  383. XCTAssertFalse(RPCResponse.isNewUser);
  384. }
  385. #pragma mark - Helpers
  386. + (NSString *)convertToJSONString:(NSObject *)object {
  387. NSData *objectAsData = [NSJSONSerialization dataWithJSONObject:object options:0 error:nil];
  388. return [[NSString alloc] initWithData:objectAsData encoding:NSUTF8StringEncoding];
  389. }
  390. @end