No Description

FIRVerifyAssertionRequest.m 5.4KB

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