VerifyPasswordRequest.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "verifyPassword" endpoint.
  16. private let kVerifyPasswordEndpoint = "verifyPassword"
  17. /// The key for the "email" value in the request.
  18. private let kEmailKey = "email"
  19. /// The key for the "password" value in the request.
  20. private let kPasswordKey = "password"
  21. /// The key for the "pendingIdToken" value in the request.
  22. private let kPendingIDTokenKey = "pendingIdToken"
  23. /// The key for the "captchaChallenge" value in the request.
  24. private let kCaptchaChallengeKey = "captchaChallenge"
  25. /// The key for the "captchaResponse" value in the request.
  26. private let kCaptchaResponseKey = "captchaResponse"
  27. /// The key for the "clientType" value in the request.
  28. private let kClientType = "clientType"
  29. /// The key for the "recaptchaVersion" value in the request.
  30. private let kRecaptchaVersion = "recaptchaVersion"
  31. /// The key for the "returnSecureToken" value in the request.
  32. private let kReturnSecureTokenKey = "returnSecureToken"
  33. /// The key for the tenant id value in the request.
  34. private let kTenantIDKey = "tenantId"
  35. /// Represents the parameters for the verifyPassword endpoint.
  36. /// See https: // developers.google.com/identity/toolkit/web/reference/relyingparty/verifyPassword
  37. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  38. class VerifyPasswordRequest: IdentityToolkitRequest, AuthRPCRequest,
  39. @unchecked Sendable /* TODO: sendable */ {
  40. typealias Response = VerifyPasswordResponse
  41. /// The email of the user.
  42. private(set) var email: String
  43. /// The password inputted by the user.
  44. private(set) var password: String
  45. /// The GITKit token for the non-trusted IDP, which is to be confirmed by the user.
  46. var pendingIDToken: String?
  47. /// The captcha challenge.
  48. var captchaChallenge: String?
  49. /// Response to the captcha.
  50. var captchaResponse: String?
  51. /// The reCAPTCHA version.
  52. var recaptchaVersion: String?
  53. /// Whether the response should return access token and refresh token directly.
  54. /// The default value is `true`.
  55. private(set) var returnSecureToken: Bool = true
  56. init(email: String, password: String,
  57. requestConfiguration: AuthRequestConfiguration) {
  58. self.email = email
  59. self.password = password
  60. super.init(endpoint: kVerifyPasswordEndpoint, requestConfiguration: requestConfiguration)
  61. }
  62. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  63. var body: [String: AnyHashable] = [
  64. kEmailKey: email,
  65. kPasswordKey: password,
  66. ]
  67. if let pendingIDToken {
  68. body[kPendingIDTokenKey] = pendingIDToken
  69. }
  70. if let captchaChallenge {
  71. body[kCaptchaChallengeKey] = captchaChallenge
  72. }
  73. if let captchaResponse {
  74. body[kCaptchaResponseKey] = captchaResponse
  75. }
  76. if let recaptchaVersion {
  77. body[kRecaptchaVersion] = recaptchaVersion
  78. }
  79. if returnSecureToken {
  80. body[kReturnSecureTokenKey] = true
  81. }
  82. if let tenantID {
  83. body[kTenantIDKey] = tenantID
  84. }
  85. body[kClientType] = clientType
  86. return body
  87. }
  88. func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) {
  89. captchaResponse = recaptchaResponse
  90. self.recaptchaVersion = recaptchaVersion
  91. }
  92. }