RevokeTokenRequest.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /// The endpoint for the revokeToken request.
  16. private let kRevokeTokenEndpoint = "accounts:revokeToken"
  17. /// The key for the provider that issued the token to revoke.
  18. private let kProviderIDKey = "providerId"
  19. /// The key for the type of the token to revoke.
  20. private let kTokenTypeKey = "tokenType"
  21. /// The key for the token to be revoked.
  22. private let kTokenKey = "token"
  23. /// The key for the ID Token associated with this credential.
  24. private let kIDTokenKey = "idToken"
  25. /// Represents the parameters for the verifyPassword endpoint.
  26. ///
  27. /// See https: // developers.google.com/identity/toolkit/web/reference/relyingparty/verifyPassword
  28. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  29. class RevokeTokenRequest: IdentityToolkitRequest, AuthRPCRequest,
  30. @unchecked Sendable /* TODO: sendable */ {
  31. typealias Response = RevokeTokenResponse
  32. /// The provider that issued the token to revoke.
  33. let providerID: String
  34. /// The type of the token to revoke.
  35. let tokenType: TokenType
  36. /// The token to be revoked.
  37. let token: String
  38. /// The ID Token associated with this credential.
  39. private let idToken: String
  40. enum TokenType: Int {
  41. case unspecified = 0, refreshToken = 1, accessToken = 2, authorizationCode = 3
  42. }
  43. @available(*, unavailable)
  44. init(withEndpoint endpoint: String, requestConfiguration: AuthRequestConfiguration) {
  45. fatalError("Use init(withToken: ... instead")
  46. }
  47. init(withToken token: String,
  48. idToken: String,
  49. requestConfiguration: AuthRequestConfiguration) {
  50. // Apple and authorization code are the only provider and token type we support for now.
  51. // Generalize this initializer to accept other providers and token types once supported.
  52. providerID = AuthProviderID.apple.rawValue
  53. tokenType = .authorizationCode
  54. self.token = token
  55. self.idToken = idToken
  56. super.init(endpoint: kRevokeTokenEndpoint,
  57. requestConfiguration: requestConfiguration,
  58. useIdentityPlatform: true)
  59. }
  60. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  61. let body: [String: AnyHashable] = [
  62. kProviderIDKey: providerID,
  63. kTokenTypeKey: "\(tokenType.rawValue)",
  64. kTokenKey: token,
  65. kIDTokenKey: idToken,
  66. ]
  67. return body
  68. }
  69. }