No Description

FIRSecureTokenRequest.m 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "FIRSecureTokenRequest.h"
  17. #import "FIRAuthRequestConfiguration.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. /** @var kFIRSecureTokenServiceGetTokenURLFormat
  20. @brief The format of the secure token service URLs. Requires string format substitution with
  21. the client's API Key.
  22. */
  23. static NSString *const kFIRSecureTokenServiceGetTokenURLFormat = @"https://%@/v1/token?key=%@";
  24. /** @var kFIRSecureTokenServiceGrantTypeRefreshToken
  25. @brief The string value of the @c FIRSecureTokenRequestGrantTypeRefreshToken request type.
  26. */
  27. static NSString *const kFIRSecureTokenServiceGrantTypeRefreshToken = @"refresh_token";
  28. /** @var kFIRSecureTokenServiceGrantTypeAuthorizationCode
  29. @brief The string value of the @c FIRSecureTokenRequestGrantTypeAuthorizationCode request type.
  30. */
  31. static NSString *const kFIRSecureTokenServiceGrantTypeAuthorizationCode = @"authorization_code";
  32. /** @var kGrantTypeKey
  33. @brief The key for the "grantType" parameter in the request.
  34. */
  35. static NSString *const kGrantTypeKey = @"grantType";
  36. /** @var kScopeKey
  37. @brief The key for the "scope" parameter in the request.
  38. */
  39. static NSString *const kScopeKey = @"scope";
  40. /** @var kRefreshTokenKey
  41. @brief The key for the "refreshToken" parameter in the request.
  42. */
  43. static NSString *const kRefreshTokenKey = @"refreshToken";
  44. /** @var kCodeKey
  45. @brief The key for the "code" parameter in the request.
  46. */
  47. static NSString *const kCodeKey = @"code";
  48. /** @var gAPIHost
  49. @brief Host for server API calls.
  50. */
  51. static NSString *gAPIHost = @"securetoken.googleapis.com";
  52. @implementation FIRSecureTokenRequest {
  53. /** @var _requestConfiguration
  54. @brief Contains configuration relevant to the request.
  55. */
  56. FIRAuthRequestConfiguration *_requestConfiguration;
  57. }
  58. + (FIRSecureTokenRequest *)authCodeRequestWithCode:(NSString *)code
  59. requestConfiguration:(FIRAuthRequestConfiguration *)
  60. requestConfiguration {
  61. return [[self alloc] initWithGrantType:FIRSecureTokenRequestGrantTypeAuthorizationCode
  62. scope:nil
  63. refreshToken:nil
  64. code:code
  65. requestConfiguration:requestConfiguration];
  66. }
  67. + (FIRSecureTokenRequest *)refreshRequestWithRefreshToken:(NSString *)refreshToken
  68. requestConfiguration:(FIRAuthRequestConfiguration *)
  69. requestConfiguration {
  70. return [[self alloc] initWithGrantType:FIRSecureTokenRequestGrantTypeRefreshToken
  71. scope:nil
  72. refreshToken:refreshToken
  73. code:nil
  74. requestConfiguration:requestConfiguration];
  75. }
  76. /** @fn grantTypeStringWithGrantType:
  77. @brief Converts a @c FIRSecureTokenRequestGrantType to it's @c NSString equivilent.
  78. */
  79. + (NSString *)grantTypeStringWithGrantType:(FIRSecureTokenRequestGrantType)grantType {
  80. switch (grantType) {
  81. case FIRSecureTokenRequestGrantTypeAuthorizationCode:
  82. return kFIRSecureTokenServiceGrantTypeAuthorizationCode;
  83. case FIRSecureTokenRequestGrantTypeRefreshToken:
  84. return kFIRSecureTokenServiceGrantTypeRefreshToken;
  85. // No Default case so we will notice if new grant types are added to the enum.
  86. }
  87. }
  88. - (nullable instancetype)initWithGrantType:(FIRSecureTokenRequestGrantType)grantType
  89. scope:(nullable NSString *)scope
  90. refreshToken:(nullable NSString *)refreshToken
  91. code:(nullable NSString *)code
  92. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  93. self = [super init];
  94. if (self) {
  95. _grantType = grantType;
  96. _scope = [scope copy];
  97. _refreshToken = [refreshToken copy];
  98. _code = [code copy];
  99. _APIKey = [requestConfiguration.APIKey copy];
  100. _requestConfiguration = requestConfiguration;
  101. }
  102. return self;
  103. }
  104. - (FIRAuthRequestConfiguration *)requestConfiguration {
  105. return _requestConfiguration;
  106. }
  107. - (NSURL *)requestURL {
  108. NSString *URLString =
  109. [NSString stringWithFormat:kFIRSecureTokenServiceGetTokenURLFormat, gAPIHost, _APIKey];
  110. NSURL *URL = [NSURL URLWithString:URLString];
  111. return URL;
  112. }
  113. - (BOOL)containsPostBody {
  114. return YES;
  115. }
  116. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  117. NSMutableDictionary *postBody = [@{
  118. kGrantTypeKey : [[self class] grantTypeStringWithGrantType:_grantType]
  119. } mutableCopy];
  120. if (_scope) {
  121. postBody[kScopeKey] = _scope;
  122. }
  123. if (_refreshToken) {
  124. postBody[kRefreshTokenKey] = _refreshToken;
  125. }
  126. if (_code) {
  127. postBody[kCodeKey] = _code;
  128. }
  129. return postBody;
  130. }
  131. #pragma mark - Internal API for development
  132. + (NSString *)host {
  133. return gAPIHost;
  134. }
  135. + (void)setHost:(NSString *)host {
  136. gAPIHost = host;
  137. }
  138. @end
  139. NS_ASSUME_NONNULL_END