FIRVerifyAssertionRequest.m 4.2 KB

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