RevokeTokenRequest.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. typealias Response = RevokeTokenResponse
  31. /// The provider that issued the token to revoke.
  32. private(set) var providerID: String
  33. /// The type of the token to revoke.
  34. private(set) var tokenType: TokenType
  35. /// The token to be revoked.
  36. private(set) var token: String
  37. /// The ID Token associated with this credential.
  38. private(set) var idToken: String
  39. enum TokenType: Int {
  40. case unspecified = 0, refreshToken = 1, accessToken = 2, authorizationCode = 3
  41. }
  42. @available(*, unavailable)
  43. init(withEndpoint endpoint: String, requestConfiguration: AuthRequestConfiguration) {
  44. fatalError("Use init(withToken: ... instead")
  45. }
  46. init(withToken token: String,
  47. idToken: String,
  48. requestConfiguration: AuthRequestConfiguration) {
  49. // Apple and authorization code are the only provider and token type we support for now.
  50. // Generalize this initializer to accept other providers and token types once supported.
  51. providerID = AuthProviderID.apple.rawValue
  52. tokenType = .authorizationCode
  53. self.token = token
  54. self.idToken = idToken
  55. super.init(endpoint: kRevokeTokenEndpoint,
  56. requestConfiguration: requestConfiguration,
  57. useIdentityPlatform: true)
  58. }
  59. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  60. let body: [String: AnyHashable] = [
  61. kProviderIDKey: providerID,
  62. kTokenTypeKey: "\(tokenType.rawValue)",
  63. kTokenKey: token,
  64. kIDTokenKey: idToken,
  65. ]
  66. return body
  67. }
  68. }