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.

145 lines
4.8 KiB

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. /** @var kVerifyAssertionEndpoint
  18. @brief The "verifyAssertion" endpoint.
  19. */
  20. static NSString *const kVerifyAssertionEndpoint = @"verifyAssertion";
  21. /** @var kProviderIDKey
  22. @brief The key for the "providerId" value in the request.
  23. */
  24. static NSString *const kProviderIDKey = @"providerId";
  25. /** @var kProviderIDTokenKey
  26. @brief The key for the "id_token" value in the request.
  27. */
  28. static NSString *const kProviderIDTokenKey = @"id_token";
  29. /** @var kProviderAccessTokenKey
  30. @brief The key for the "access_token" value in the request.
  31. */
  32. static NSString *const kProviderAccessTokenKey = @"access_token";
  33. /** @var kProviderOAuthTokenSecretKey
  34. @brief The key for the "oauth_token_secret" value in the request.
  35. */
  36. static NSString *const kProviderOAuthTokenSecretKey = @"oauth_token_secret";
  37. /** @var kIdentifierKey
  38. @brief The key for the "identifier" value in the request.
  39. */
  40. static NSString *const kIdentifierKey = @"identifier";
  41. /** @var kRequestURIKey
  42. @brief The key for the "requestUri" value in the request.
  43. */
  44. static NSString *const kRequestURIKey = @"requestUri";
  45. /** @var kPostBodyKey
  46. @brief The key for the "postBody" value in the request.
  47. */
  48. static NSString *const kPostBodyKey = @"postBody";
  49. /** @var kPendingIDTokenKey
  50. @brief The key for the "pendingIdToken" value in the request.
  51. */
  52. static NSString *const kPendingIDTokenKey = @"pendingIdToken";
  53. /** @var kAutoCreateKey
  54. @brief The key for the "autoCreate" value in the request.
  55. */
  56. static NSString *const kAutoCreateKey = @"autoCreate";
  57. /** @var kIDTokenKey
  58. @brief The key for the "idToken" value in the request. This is actually the STS Access Token,
  59. despite it's confusing (backwards compatiable) parameter name.
  60. */
  61. static NSString *const kIDTokenKey = @"idToken";
  62. /** @var kReturnSecureTokenKey
  63. @brief The key for the "returnSecureToken" value in the request.
  64. */
  65. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  66. @implementation FIRVerifyAssertionRequest
  67. - (nullable instancetype)initWithProviderID:(NSString *)providerID
  68. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  69. self = [super initWithEndpoint:kVerifyAssertionEndpoint
  70. requestConfiguration:requestConfiguration];
  71. if (self) {
  72. _providerID = providerID;
  73. _returnSecureToken = YES;
  74. _autoCreate = YES;
  75. }
  76. return self;
  77. }
  78. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  79. NSURLComponents *components = [[NSURLComponents alloc] init];
  80. NSMutableArray<NSURLQueryItem *> *queryItems = [@[[NSURLQueryItem queryItemWithName:kProviderIDKey
  81. value:_providerID]]
  82. mutableCopy];
  83. if (_providerIDToken) {
  84. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderIDTokenKey
  85. value:_providerIDToken]];
  86. }
  87. if (_providerAccessToken) {
  88. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderAccessTokenKey
  89. value:_providerAccessToken]];
  90. }
  91. if (!_providerIDToken && !_providerAccessToken) {
  92. [NSException raise:NSInvalidArgumentException
  93. format:@"Either IDToken or accessToken must be supplied."];
  94. }
  95. if (_providerOAuthTokenSecret) {
  96. [queryItems addObject:[NSURLQueryItem queryItemWithName:kProviderOAuthTokenSecretKey
  97. value:_providerOAuthTokenSecret]];
  98. }
  99. if (_inputEmail) {
  100. [queryItems addObject:[NSURLQueryItem queryItemWithName:kIdentifierKey
  101. value:_inputEmail]];
  102. }
  103. [components setQueryItems:queryItems];
  104. NSMutableDictionary *body = [@{
  105. kRequestURIKey : @"http://localhost", // Unused by server, but required
  106. kPostBodyKey : [components query]
  107. } mutableCopy];
  108. if (_pendingIDToken) {
  109. body[kPendingIDTokenKey] = _pendingIDToken;
  110. }
  111. if (_accessToken) {
  112. body[kIDTokenKey] = _accessToken;
  113. }
  114. if (_returnSecureToken) {
  115. body[kReturnSecureTokenKey] = @YES;
  116. }
  117. body[kAutoCreateKey] = @(_autoCreate);
  118. return body;
  119. }
  120. @end