FIRVerifyAssertionResponseTests.m 16 KB

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