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.

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