SecureTokenRequest.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /// Host for server API calls. This should be changed via
  46. /// `SecureTokenRequest.setHost(_ host:)` for testing purposes only.
  47. private nonisolated(unsafe) var gAPIHost = "securetoken.googleapis.com"
  48. /// Represents the parameters for the token endpoint.
  49. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  50. class SecureTokenRequest: AuthRPCRequest {
  51. typealias Response = SecureTokenResponse
  52. /// The type of grant requested.
  53. /// See FIRSecureTokenRequestGrantType
  54. let grantType: SecureTokenRequestGrantType
  55. /// The scopes requested (a comma-delimited list of scope strings).
  56. let scope: String?
  57. /// The client's refresh token.
  58. let refreshToken: String?
  59. /// The client's authorization code (legacy Gitkit "ID Token").
  60. let code: String?
  61. /// The client's API Key.
  62. let apiKey: String
  63. let _requestConfiguration: AuthRequestConfiguration
  64. func requestConfiguration() -> AuthRequestConfiguration {
  65. _requestConfiguration
  66. }
  67. static func authCodeRequest(code: String,
  68. requestConfiguration: AuthRequestConfiguration)
  69. -> SecureTokenRequest {
  70. SecureTokenRequest(
  71. grantType: .authorizationCode,
  72. scope: nil,
  73. refreshToken: nil,
  74. code: code,
  75. requestConfiguration: requestConfiguration
  76. )
  77. }
  78. static func refreshRequest(refreshToken: String,
  79. requestConfiguration: AuthRequestConfiguration)
  80. -> SecureTokenRequest {
  81. SecureTokenRequest(
  82. grantType: .refreshToken,
  83. scope: nil,
  84. refreshToken: refreshToken,
  85. code: nil,
  86. requestConfiguration: requestConfiguration
  87. )
  88. }
  89. private init(grantType: SecureTokenRequestGrantType, scope: String?, refreshToken: String?,
  90. code: String?, requestConfiguration: AuthRequestConfiguration) {
  91. self.grantType = grantType
  92. self.scope = scope
  93. self.refreshToken = refreshToken
  94. self.code = code
  95. apiKey = requestConfiguration.apiKey
  96. _requestConfiguration = requestConfiguration
  97. }
  98. func requestURL() -> URL {
  99. let urlString: String
  100. if let emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort {
  101. urlString = "http://\(emulatorHostAndPort)/\(gAPIHost)/v1/token?key=\(apiKey)"
  102. } else {
  103. urlString = "https://\(gAPIHost)/v1/token?key=\(apiKey)"
  104. }
  105. return URL(string: urlString)!
  106. }
  107. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  108. var postBody: [String: AnyHashable] = [
  109. kGrantTypeKey: grantType.value,
  110. ]
  111. if let scope = scope {
  112. postBody[kScopeKey] = scope
  113. }
  114. if let refreshToken = refreshToken {
  115. postBody[kRefreshTokenKey] = refreshToken
  116. }
  117. if let code = code {
  118. postBody[kCodeKey] = code
  119. }
  120. return postBody
  121. }
  122. // MARK: Internal API for development
  123. static var host: String { gAPIHost }
  124. static func setHost(_ host: String) {
  125. gAPIHost = host
  126. }
  127. }