FIRSecureTokenRequest.m 4.7 KB

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