| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- // Copyright 2023 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- import Foundation
- enum SecureTokenRequestGrantType: Int {
- case authorizationCode
- case refreshToken
- var value: String {
- switch self {
- case .refreshToken:
- return kFIRSecureTokenServiceGrantTypeRefreshToken
- case .authorizationCode:
- return kFIRSecureTokenServiceGrantTypeAuthorizationCode
- }
- }
- }
- /** @var kFIRSecureTokenServiceGetTokenURLFormat
- @brief The format of the secure token service URLs. Requires string format substitution with
- the client's API Key.
- */
- private let kFIRSecureTokenServiceGetTokenURLFormat = "https://%@/v1/token?key=%@"
- /** @var kFIREmulatorURLFormat
- @brief The format of the emulated secure token service URLs. Requires string format substitution
- with the emulator host, the gAPIHost, and the client's API Key.
- */
- private let kFIREmulatorURLFormat = "http://%@/%@/v1/token?key=%@"
- /** @var kFIRSecureTokenServiceGrantTypeRefreshToken
- @brief The string value of the @c FIRSecureTokenRequestGrantTypeRefreshToken request type.
- */
- private let kFIRSecureTokenServiceGrantTypeRefreshToken = "refresh_token"
- /** @var kFIRSecureTokenServiceGrantTypeAuthorizationCode
- @brief The string value of the @c FIRSecureTokenRequestGrantTypeAuthorizationCode request type.
- */
- private let kFIRSecureTokenServiceGrantTypeAuthorizationCode = "authorization_code"
- /** @var kGrantTypeKey
- @brief The key for the "grantType" parameter in the request.
- */
- private let kGrantTypeKey = "grantType"
- /** @var kScopeKey
- @brief The key for the "scope" parameter in the request.
- */
- private let kScopeKey = "scope"
- /** @var kRefreshTokenKey
- @brief The key for the "refreshToken" parameter in the request.
- */
- private let kRefreshTokenKey = "refreshToken"
- /** @var kCodeKey
- @brief The key for the "code" parameter in the request.
- */
- private let kCodeKey = "code"
- /** @var gAPIHost
- @brief Host for server API calls.
- */
- private var gAPIHost = "securetoken.googleapis.com"
- /** @class FIRSecureTokenRequest
- @brief Represents the parameters for the token endpoint.
- */
- @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
- class SecureTokenRequest: AuthRPCRequest {
- typealias Response = SecureTokenResponse
- /** @property grantType
- @brief The type of grant requested.
- @see FIRSecureTokenRequestGrantType
- */
- var grantType: SecureTokenRequestGrantType
- /** @property scope
- @brief The scopes requested (a comma-delimited list of scope strings.)
- */
- var scope: String?
- /** @property refreshToken
- @brief The client's refresh token.
- */
- var refreshToken: String?
- /** @property code
- @brief The client's authorization code (legacy Gitkit "ID Token").
- */
- var code: String?
- /** @property APIKey
- @brief The client's API Key.
- */
- let apiKey: String
- let _requestConfiguration: AuthRequestConfiguration
- func requestConfiguration() -> AuthRequestConfiguration {
- _requestConfiguration
- }
- static func authCodeRequest(code: String,
- requestConfiguration: AuthRequestConfiguration)
- -> SecureTokenRequest {
- SecureTokenRequest(
- grantType: .authorizationCode,
- scope: nil,
- refreshToken: nil,
- code: code,
- requestConfiguration: requestConfiguration
- )
- }
- static func refreshRequest(refreshToken: String,
- requestConfiguration: AuthRequestConfiguration)
- -> SecureTokenRequest {
- SecureTokenRequest(
- grantType: .refreshToken,
- scope: nil,
- refreshToken: refreshToken,
- code: nil,
- requestConfiguration: requestConfiguration
- )
- }
- init(grantType: SecureTokenRequestGrantType, scope: String?, refreshToken: String?,
- code: String?, requestConfiguration: AuthRequestConfiguration) {
- self.grantType = grantType
- self.scope = scope
- self.refreshToken = refreshToken
- self.code = code
- apiKey = requestConfiguration.apiKey
- _requestConfiguration = requestConfiguration
- }
- func requestURL() -> URL {
- let urlString: String
- if let emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort {
- urlString = "http://\(emulatorHostAndPort)/\(gAPIHost)/v1/token?key=\(apiKey)"
- } else {
- urlString = "https://\(gAPIHost)/v1/token?key=\(apiKey)"
- }
- return URL(string: urlString)!
- }
- func containsPostBody() -> Bool { true }
- func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
- var postBody: [String: AnyHashable] = [
- kGrantTypeKey: grantType.value,
- ]
- if let scope = scope {
- postBody[kScopeKey] = scope
- }
- if let refreshToken = refreshToken {
- postBody[kRefreshTokenKey] = refreshToken
- }
- if let code = code {
- postBody[kCodeKey] = code
- }
- return postBody
- }
- // MARK: Internal API for development
- static var host: String { gAPIHost }
- static func setHost(_ host: String) {
- gAPIHost = host
- }
- }
|