VerifyPasswordRequest.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. typealias Response = VerifyPasswordResponse
  40. /// The email of the user.
  41. private(set) var email: String
  42. /// The password inputted by the user.
  43. private(set) var password: String
  44. /// The GITKit token for the non-trusted IDP, which is to be confirmed by the user.
  45. var pendingIDToken: String?
  46. /// The captcha challenge.
  47. var captchaChallenge: String?
  48. /// Response to the captcha.
  49. var captchaResponse: String?
  50. /// The reCAPTCHA version.
  51. var recaptchaVersion: String?
  52. /// Whether the response should return access token and refresh token directly.
  53. /// The default value is `true`.
  54. private(set) var returnSecureToken: Bool = true
  55. init(email: String, password: String,
  56. requestConfiguration: AuthRequestConfiguration) {
  57. self.email = email
  58. self.password = password
  59. super.init(endpoint: kVerifyPasswordEndpoint, requestConfiguration: requestConfiguration)
  60. }
  61. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  62. var body: [String: AnyHashable] = [
  63. kEmailKey: email,
  64. kPasswordKey: password,
  65. ]
  66. if let pendingIDToken {
  67. body[kPendingIDTokenKey] = pendingIDToken
  68. }
  69. if let captchaChallenge {
  70. body[kCaptchaChallengeKey] = captchaChallenge
  71. }
  72. if let captchaResponse {
  73. body[kCaptchaResponseKey] = captchaResponse
  74. }
  75. if let recaptchaVersion {
  76. body[kRecaptchaVersion] = recaptchaVersion
  77. }
  78. if returnSecureToken {
  79. body[kReturnSecureTokenKey] = true
  80. }
  81. if let tenantID {
  82. body[kTenantIDKey] = tenantID
  83. }
  84. body[kClientType] = clientType
  85. return body
  86. }
  87. func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) {
  88. captchaResponse = recaptchaResponse
  89. self.recaptchaVersion = recaptchaVersion
  90. }
  91. }