FIRVerifyAssertionRequest.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 kReturnIDPCredentialKey
  72. @brief The key for the "returnIdpCredential" value in the request.
  73. */
  74. static NSString *const kReturnIDPCredentialKey = @"returnIdpCredential";
  75. /** @var kSessionIDKey
  76. @brief The key for the "sessionID" value in the request.
  77. */
  78. static NSString *const kSessionIDKey = @"sessionId";
  79. /** @var kTenantIDKey
  80. @brief The key for the tenant id value in the request.
  81. */
  82. static NSString *const kTenantIDKey = @"tenantId";
  83. @implementation FIRVerifyAssertionRequest
  84. - (nullable instancetype)initWithProviderID:(NSString *)providerID
  85. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  86. self = [super initWithEndpoint:kVerifyAssertionEndpoint
  87. requestConfiguration:requestConfiguration];
  88. if (self) {
  89. _providerID = providerID;
  90. _returnSecureToken = YES;
  91. _autoCreate = YES;
  92. _returnIDPCredential = YES;
  93. }
  94. return self;
  95. }
  96. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  97. NSURLComponents *components = [[NSURLComponents alloc] init];
  98. NSMutableArray<NSURLQueryItem *> *queryItems =
  99. [@[ [NSURLQueryItem queryItemWithName:kProviderIDKey value:_providerID] ] mutableCopy];
  100. if (_providerIDToken) {
  101. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderIDTokenKey
  102. value:_providerIDToken]];
  103. }
  104. if (_providerRawNonce) {
  105. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderNonceKey
  106. value:_providerRawNonce]];
  107. }
  108. if (_providerAccessToken) {
  109. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderAccessTokenKey
  110. value:_providerAccessToken]];
  111. }
  112. if (!_providerIDToken && !_providerAccessToken && !_pendingToken && !_requestURI) {
  113. [NSException
  114. raise:NSInvalidArgumentException
  115. format:@"One of IDToken, accessToken, pendingToken, or requestURI must be supplied."];
  116. }
  117. if (_providerOAuthTokenSecret) {
  118. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderOAuthTokenSecretKey
  119. value:_providerOAuthTokenSecret]];
  120. }
  121. if (_inputEmail) {
  122. [queryItems addObject:[NSURLQueryItem queryItemWithName:kIdentifierKey value:_inputEmail]];
  123. }
  124. [components setQueryItems:queryItems];
  125. NSMutableDictionary *body = [@{
  126. kRequestURIKey : _requestURI ?: @"http://localhost", // Unused by server, but required
  127. kPostBodyKey : [components query]
  128. } mutableCopy];
  129. if (_pendingToken) {
  130. body[kPendingTokenKey] = _pendingToken;
  131. }
  132. if (_accessToken) {
  133. body[kIDTokenKey] = _accessToken;
  134. }
  135. if (_returnSecureToken) {
  136. body[kReturnSecureTokenKey] = @YES;
  137. }
  138. if (_returnIDPCredential) {
  139. body[kReturnIDPCredentialKey] = @YES;
  140. }
  141. if (_sessionID) {
  142. body[kSessionIDKey] = _sessionID;
  143. }
  144. if (self.tenantID) {
  145. body[kTenantIDKey] = self.tenantID;
  146. }
  147. body[kAutoCreateKey] = @(_autoCreate);
  148. return body;
  149. }
  150. @end
  151. NS_ASSUME_NONNULL_END