SecureTokenRequest.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. enum SecureTokenRequestGrantType: Int {
  16. case authorizationCode
  17. case refreshToken
  18. var value: String {
  19. switch self {
  20. case .refreshToken:
  21. return kFIRSecureTokenServiceGrantTypeRefreshToken
  22. case .authorizationCode:
  23. return kFIRSecureTokenServiceGrantTypeAuthorizationCode
  24. }
  25. }
  26. }
  27. /// The format of the secure token service URLs. Requires string format substitution with
  28. /// the client's API Key.
  29. private let kFIRSecureTokenServiceGetTokenURLFormat = "https://%@/v1/token?key=%@"
  30. /// The format of the emulated secure token service URLs. Requires string format substitution
  31. /// with the emulator host, the gAPIHost, and the client's API Key.
  32. private let kFIREmulatorURLFormat = "http://%@/%@/v1/token?key=%@"
  33. /// The string value of the `SecureTokenRequestGrantTypeRefreshToken` request type.
  34. private let kFIRSecureTokenServiceGrantTypeRefreshToken = "refresh_token"
  35. /// The string value of the `SecureTokenRequestGrantTypeAuthorizationCode` request type.
  36. private let kFIRSecureTokenServiceGrantTypeAuthorizationCode = "authorization_code"
  37. /// The key for the "grantType" parameter in the request.
  38. private let kGrantTypeKey = "grantType"
  39. /// The key for the "scope" parameter in the request.
  40. private let kScopeKey = "scope"
  41. /// The key for the "refreshToken" parameter in the request.
  42. private let kRefreshTokenKey = "refreshToken"
  43. /// The key for the "code" parameter in the request.
  44. private let kCodeKey = "code"
  45. #if compiler(>=6)
  46. /// Host for server API calls. This should be changed via
  47. /// `SecureTokenRequest.setHost(_ host:)` for testing purposes only.
  48. private nonisolated(unsafe) var gAPIHost = "securetoken.googleapis.com"
  49. #else
  50. /// Host for server API calls. This should be changed via
  51. /// `SecureTokenRequest.setHost(_ host:)` for testing purposes only.
  52. private var gAPIHost = "securetoken.googleapis.com"
  53. #endif // compiler(>=6)
  54. /// Represents the parameters for the token endpoint.
  55. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  56. class SecureTokenRequest: AuthRPCRequest {
  57. typealias Response = SecureTokenResponse
  58. /// The type of grant requested.
  59. /// See FIRSecureTokenRequestGrantType
  60. let grantType: SecureTokenRequestGrantType
  61. /// The scopes requested (a comma-delimited list of scope strings).
  62. let scope: String?
  63. /// The client's refresh token.
  64. let refreshToken: String?
  65. /// The client's authorization code (legacy Gitkit "ID Token").
  66. let code: String?
  67. /// The client's API Key.
  68. let apiKey: String
  69. let _requestConfiguration: AuthRequestConfiguration
  70. func requestConfiguration() -> AuthRequestConfiguration {
  71. _requestConfiguration
  72. }
  73. static func authCodeRequest(code: String,
  74. requestConfiguration: AuthRequestConfiguration)
  75. -> SecureTokenRequest {
  76. SecureTokenRequest(
  77. grantType: .authorizationCode,
  78. scope: nil,
  79. refreshToken: nil,
  80. code: code,
  81. requestConfiguration: requestConfiguration
  82. )
  83. }
  84. static func refreshRequest(refreshToken: String,
  85. requestConfiguration: AuthRequestConfiguration)
  86. -> SecureTokenRequest {
  87. SecureTokenRequest(
  88. grantType: .refreshToken,
  89. scope: nil,
  90. refreshToken: refreshToken,
  91. code: nil,
  92. requestConfiguration: requestConfiguration
  93. )
  94. }
  95. private init(grantType: SecureTokenRequestGrantType, scope: String?, refreshToken: String?,
  96. code: String?, requestConfiguration: AuthRequestConfiguration) {
  97. self.grantType = grantType
  98. self.scope = scope
  99. self.refreshToken = refreshToken
  100. self.code = code
  101. apiKey = requestConfiguration.apiKey
  102. _requestConfiguration = requestConfiguration
  103. }
  104. func requestURL() -> URL {
  105. let urlString: String
  106. if let emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort {
  107. urlString = "http://\(emulatorHostAndPort)/\(gAPIHost)/v1/token?key=\(apiKey)"
  108. } else {
  109. urlString = "https://\(gAPIHost)/v1/token?key=\(apiKey)"
  110. }
  111. return URL(string: urlString)!
  112. }
  113. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  114. var postBody: [String: AnyHashable] = [
  115. kGrantTypeKey: grantType.value,
  116. ]
  117. if let scope = scope {
  118. postBody[kScopeKey] = scope
  119. }
  120. if let refreshToken = refreshToken {
  121. postBody[kRefreshTokenKey] = refreshToken
  122. }
  123. if let code = code {
  124. postBody[kCodeKey] = code
  125. }
  126. return postBody
  127. }
  128. // MARK: Internal API for development
  129. static var host: String { gAPIHost }
  130. static func setHost(_ host: String) {
  131. gAPIHost = host
  132. }
  133. }