SecureTokenRequest.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.
  46. private var gAPIHost = "securetoken.googleapis.com"
  47. /// Represents the parameters for the token endpoint.
  48. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  49. class SecureTokenRequest: AuthRPCRequest {
  50. typealias Response = SecureTokenResponse
  51. /// The type of grant requested.
  52. /// See FIRSecureTokenRequestGrantType
  53. var grantType: SecureTokenRequestGrantType
  54. /// The scopes requested (a comma-delimited list of scope strings).
  55. var scope: String?
  56. /// The client's refresh token.
  57. var refreshToken: String?
  58. /// The client's authorization code (legacy Gitkit "ID Token").
  59. var code: String?
  60. /// The client's API Key.
  61. let apiKey: String
  62. let _requestConfiguration: AuthRequestConfiguration
  63. func requestConfiguration() -> AuthRequestConfiguration {
  64. _requestConfiguration
  65. }
  66. static func authCodeRequest(code: String,
  67. requestConfiguration: AuthRequestConfiguration)
  68. -> SecureTokenRequest {
  69. SecureTokenRequest(
  70. grantType: .authorizationCode,
  71. scope: nil,
  72. refreshToken: nil,
  73. code: code,
  74. requestConfiguration: requestConfiguration
  75. )
  76. }
  77. static func refreshRequest(refreshToken: String,
  78. requestConfiguration: AuthRequestConfiguration)
  79. -> SecureTokenRequest {
  80. SecureTokenRequest(
  81. grantType: .refreshToken,
  82. scope: nil,
  83. refreshToken: refreshToken,
  84. code: nil,
  85. requestConfiguration: requestConfiguration
  86. )
  87. }
  88. init(grantType: SecureTokenRequestGrantType, scope: String?, refreshToken: String?,
  89. code: String?, requestConfiguration: AuthRequestConfiguration) {
  90. self.grantType = grantType
  91. self.scope = scope
  92. self.refreshToken = refreshToken
  93. self.code = code
  94. apiKey = requestConfiguration.apiKey
  95. _requestConfiguration = requestConfiguration
  96. }
  97. func requestURL() -> URL {
  98. let urlString: String
  99. if let emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort {
  100. urlString = "http://\(emulatorHostAndPort)/\(gAPIHost)/v1/token?key=\(apiKey)"
  101. } else {
  102. urlString = "https://\(gAPIHost)/v1/token?key=\(apiKey)"
  103. }
  104. return URL(string: urlString)!
  105. }
  106. var containsPostBody: Bool { return true }
  107. func unencodedHTTPRequestBody() throws -> [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. }