FIRSecureTokenRequest.m 6.0 KB

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