FIRVerifyAssertionRequest.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionRequest.h"
  17. NS_ASSUME_NONNULL_BEGIN
  18. /** @var kVerifyAssertionEndpoint
  19. @brief The "verifyAssertion" endpoint.
  20. */
  21. static NSString *const kVerifyAssertionEndpoint = @"verifyAssertion";
  22. /** @var kProviderIDKey
  23. @brief The key for the "providerId" value in the request.
  24. */
  25. static NSString *const kProviderIDKey = @"providerId";
  26. /** @var kProviderIDTokenKey
  27. @brief The key for the "id_token" value in the request.
  28. */
  29. static NSString *const kProviderIDTokenKey = @"id_token";
  30. /** @var kProviderNonceKey
  31. @brief The key for the "nonce" value in the request.
  32. */
  33. static NSString *const kProviderNonceKey = @"nonce";
  34. /** @var kProviderAccessTokenKey
  35. @brief The key for the "access_token" value in the request.
  36. */
  37. static NSString *const kProviderAccessTokenKey = @"access_token";
  38. /** @var kProviderOAuthTokenSecretKey
  39. @brief The key for the "oauth_token_secret" value in the request.
  40. */
  41. static NSString *const kProviderOAuthTokenSecretKey = @"oauth_token_secret";
  42. /** @var kIdentifierKey
  43. @brief The key for the "identifier" value in the request.
  44. */
  45. static NSString *const kIdentifierKey = @"identifier";
  46. /** @var kRequestURIKey
  47. @brief The key for the "requestUri" value in the request.
  48. */
  49. static NSString *const kRequestURIKey = @"requestUri";
  50. /** @var kPostBodyKey
  51. @brief The key for the "postBody" value in the request.
  52. */
  53. static NSString *const kPostBodyKey = @"postBody";
  54. /** @var kPendingTokenKey
  55. @brief The key for the "pendingToken" value in the request.
  56. */
  57. static NSString *const kPendingTokenKey = @"pendingToken";
  58. /** @var kAutoCreateKey
  59. @brief The key for the "autoCreate" value in the request.
  60. */
  61. static NSString *const kAutoCreateKey = @"autoCreate";
  62. /** @var kIDTokenKey
  63. @brief The key for the "idToken" value in the request. This is actually the STS Access Token,
  64. despite it's confusing (backwards compatiable) parameter name.
  65. */
  66. static NSString *const kIDTokenKey = @"idToken";
  67. /** @var kReturnSecureTokenKey
  68. @brief The key for the "returnSecureToken" value in the request.
  69. */
  70. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  71. /** @var kUserKey
  72. @brief The key for the "user" value in the request. The value is a JSON object that contains the
  73. name of the user.
  74. */
  75. static NSString *const kUserKey = @"user";
  76. /** @var kNameKey
  77. @brief The key for the "name" value in the request. The value is a JSON object that contains the
  78. first and/or last name of the user.
  79. */
  80. static NSString *const kNameKey = @"name";
  81. /** @var kFirstNameKey
  82. @brief The key for the "firstName" value in the request.
  83. */
  84. static NSString *const kFirstNameKey = @"firstName";
  85. /** @var kLastNameKey
  86. @brief The key for the "lastName" value in the request.
  87. */
  88. static NSString *const kLastNameKey = @"lastName";
  89. /** @var kReturnIDPCredentialKey
  90. @brief The key for the "returnIdpCredential" value in the request.
  91. */
  92. static NSString *const kReturnIDPCredentialKey = @"returnIdpCredential";
  93. /** @var kSessionIDKey
  94. @brief The key for the "sessionID" value in the request.
  95. */
  96. static NSString *const kSessionIDKey = @"sessionId";
  97. /** @var kTenantIDKey
  98. @brief The key for the tenant id value in the request.
  99. */
  100. static NSString *const kTenantIDKey = @"tenantId";
  101. @implementation FIRVerifyAssertionRequest
  102. - (nullable instancetype)initWithProviderID:(NSString *)providerID
  103. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  104. self = [super initWithEndpoint:kVerifyAssertionEndpoint
  105. requestConfiguration:requestConfiguration];
  106. if (self) {
  107. _providerID = providerID;
  108. _returnSecureToken = YES;
  109. _autoCreate = YES;
  110. _returnIDPCredential = YES;
  111. }
  112. return self;
  113. }
  114. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  115. NSURLComponents *components = [[NSURLComponents alloc] init];
  116. NSMutableArray<NSURLQueryItem *> *queryItems =
  117. [@[ [NSURLQueryItem queryItemWithName:kProviderIDKey value:_providerID] ] mutableCopy];
  118. if (_providerIDToken) {
  119. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderIDTokenKey
  120. value:_providerIDToken]];
  121. }
  122. if (_providerRawNonce) {
  123. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderNonceKey
  124. value:_providerRawNonce]];
  125. }
  126. if (_providerAccessToken) {
  127. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderAccessTokenKey
  128. value:_providerAccessToken]];
  129. }
  130. if (!_providerIDToken && !_providerAccessToken && !_pendingToken && !_requestURI) {
  131. [NSException
  132. raise:NSInvalidArgumentException
  133. format:@"One of IDToken, accessToken, pendingToken, or requestURI must be supplied."];
  134. }
  135. if (_providerOAuthTokenSecret) {
  136. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderOAuthTokenSecretKey
  137. value:_providerOAuthTokenSecret]];
  138. }
  139. if (_inputEmail) {
  140. [queryItems addObject:[NSURLQueryItem queryItemWithName:kIdentifierKey value:_inputEmail]];
  141. }
  142. if (_fullName.givenName || _fullName.familyName) {
  143. NSMutableDictionary *nameDict = [[NSMutableDictionary alloc] init];
  144. if (_fullName.givenName) {
  145. nameDict[kFirstNameKey] = _fullName.givenName;
  146. }
  147. if (_fullName.familyName) {
  148. nameDict[kLastNameKey] = _fullName.familyName;
  149. }
  150. NSDictionary *userDict = [NSDictionary dictionaryWithObject:nameDict forKey:kNameKey];
  151. NSData *userJson = [NSJSONSerialization dataWithJSONObject:userDict options:0 error:error];
  152. [queryItems
  153. addObject:[NSURLQueryItem
  154. queryItemWithName:kUserKey
  155. value:[[NSString alloc] initWithData:userJson
  156. encoding:NSUTF8StringEncoding]]];
  157. }
  158. [components setQueryItems:queryItems];
  159. NSMutableDictionary *body = [@{
  160. kRequestURIKey : _requestURI ?: @"http://localhost", // Unused by server, but required
  161. kPostBodyKey : [components query]
  162. } mutableCopy];
  163. if (_pendingToken) {
  164. body[kPendingTokenKey] = _pendingToken;
  165. }
  166. if (_accessToken) {
  167. body[kIDTokenKey] = _accessToken;
  168. }
  169. if (_returnSecureToken) {
  170. body[kReturnSecureTokenKey] = @YES;
  171. }
  172. if (_returnIDPCredential) {
  173. body[kReturnIDPCredentialKey] = @YES;
  174. }
  175. if (_sessionID) {
  176. body[kSessionIDKey] = _sessionID;
  177. }
  178. if (self.tenantID) {
  179. body[kTenantIDKey] = self.tenantID;
  180. }
  181. body[kAutoCreateKey] = @(_autoCreate);
  182. return body;
  183. }
  184. @end
  185. NS_ASSUME_NONNULL_END