FIRSecureTokenRequest.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 kGrantTypeKey
  34. @brief The key for the "grantType" parameter in the request.
  35. */
  36. static NSString *const kGrantTypeKey = @"grantType";
  37. /** @var kRefreshTokenKey
  38. @brief The key for the "refreshToken" parameter in the request.
  39. */
  40. static NSString *const kRefreshTokenKey = @"refreshToken";
  41. /** @var gAPIHost
  42. @brief Host for server API calls.
  43. */
  44. static NSString *gAPIHost = @"securetoken.googleapis.com";
  45. @implementation FIRSecureTokenRequest {
  46. /** @var _requestConfiguration
  47. @brief Contains configuration relevant to the request.
  48. */
  49. FIRAuthRequestConfiguration *_requestConfiguration;
  50. }
  51. + (FIRSecureTokenRequest *)refreshRequestWithRefreshToken:(NSString *)refreshToken
  52. requestConfiguration:
  53. (FIRAuthRequestConfiguration *)requestConfiguration {
  54. return [[self alloc] initWithRefreshToken:refreshToken requestConfiguration:requestConfiguration];
  55. }
  56. - (nullable instancetype)initWithRefreshToken:(NSString *)refreshToken
  57. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  58. self = [super init];
  59. if (self) {
  60. _refreshToken = [refreshToken copy];
  61. _APIKey = [requestConfiguration.APIKey copy];
  62. _requestConfiguration = requestConfiguration;
  63. }
  64. return self;
  65. }
  66. - (FIRAuthRequestConfiguration *)requestConfiguration {
  67. return _requestConfiguration;
  68. }
  69. - (NSURL *)requestURL {
  70. NSString *URLString;
  71. NSString *emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort;
  72. if (emulatorHostAndPort) {
  73. URLString =
  74. [NSString stringWithFormat:kFIREmulatorURLFormat, emulatorHostAndPort, gAPIHost, _APIKey];
  75. } else {
  76. URLString =
  77. [NSString stringWithFormat:kFIRSecureTokenServiceGetTokenURLFormat, gAPIHost, _APIKey];
  78. }
  79. NSURL *URL = [NSURL URLWithString:URLString];
  80. return URL;
  81. }
  82. - (BOOL)containsPostBody {
  83. return YES;
  84. }
  85. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  86. NSMutableDictionary *postBody = [@{
  87. kGrantTypeKey : kFIRSecureTokenServiceGrantTypeRefreshToken,
  88. kRefreshTokenKey : _refreshToken
  89. } mutableCopy];
  90. return [postBody copy];
  91. }
  92. #pragma mark - Internal API for development
  93. + (NSString *)host {
  94. return gAPIHost;
  95. }
  96. + (void)setHost:(NSString *)host {
  97. gAPIHost = host;
  98. }
  99. @end
  100. NS_ASSUME_NONNULL_END