RevokeTokenRequest.swift 3.2 KB

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