You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.7 KiB

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