VerifyPasswordRequest.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 kVerifyPasswordEndpoint
  16. @brief The "verifyPassword" endpoint.
  17. */
  18. private let kVerifyPasswordEndpoint = "verifyPassword"
  19. /** @var kEmailKey
  20. @brief The key for the "email" value in the request.
  21. */
  22. private let kEmailKey = "email"
  23. /** @var kPasswordKey
  24. @brief The key for the "password" value in the request.
  25. */
  26. private let kPasswordKey = "password"
  27. /** @var kPendingIDTokenKey
  28. @brief The key for the "pendingIdToken" value in the request.
  29. */
  30. private let kPendingIDTokenKey = "pendingIdToken"
  31. /** @var kCaptchaChallengeKey
  32. @brief The key for the "captchaChallenge" value in the request.
  33. */
  34. private let kCaptchaChallengeKey = "captchaChallenge"
  35. /** @var kCaptchaResponseKey
  36. @brief The key for the "captchaResponse" value in the request.
  37. */
  38. private let kCaptchaResponseKey = "captchaResponse"
  39. /** @var kClientType
  40. @brief The key for the "clientType" value in the request.
  41. */
  42. private let kClientType = "clientType"
  43. /** @var kRecaptchaVersion
  44. @brief The key for the "recaptchaVersion" value in the request.
  45. */
  46. private let kRecaptchaVersion = "recaptchaVersion"
  47. /** @var kReturnSecureTokenKey
  48. @brief The key for the "returnSecureToken" value in the request.
  49. */
  50. private let kReturnSecureTokenKey = "returnSecureToken"
  51. /** @var kTenantIDKey
  52. @brief The key for the tenant id value in the request.
  53. */
  54. private let kTenantIDKey = "tenantId"
  55. /** @class FIRVerifyPasswordRequest
  56. @brief Represents the parameters for the verifyPassword endpoint.
  57. @see https://developers.google.com/identity/toolkit/web/reference/relyingparty/verifyPassword
  58. */
  59. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  60. class VerifyPasswordRequest: IdentityToolkitRequest, AuthRPCRequest {
  61. typealias Response = VerifyPasswordResponse
  62. /** @property email
  63. @brief The email of the user.
  64. */
  65. private(set) var email: String
  66. /** @property password
  67. @brief The password inputed by the user.
  68. */
  69. private(set) var password: String
  70. /** @property pendingIDToken
  71. @brief The GITKit token for the non-trusted IDP, which is to be confirmed by the user.
  72. */
  73. var pendingIDToken: String?
  74. /** @property captchaChallenge
  75. @brief The captcha challenge.
  76. */
  77. var captchaChallenge: String?
  78. /** @property captchaResponse
  79. @brief Response to the captcha.
  80. */
  81. var captchaResponse: String?
  82. /** @property captchaResponse
  83. @brief The reCAPTCHA version.
  84. */
  85. var recaptchaVersion: String?
  86. /** @property returnSecureToken
  87. @brief Whether the response should return access token and refresh token directly.
  88. @remarks The default value is @c YES .
  89. */
  90. private(set) var returnSecureToken: Bool
  91. init(email: String, password: String,
  92. requestConfiguration: AuthRequestConfiguration) {
  93. self.email = email
  94. self.password = password
  95. returnSecureToken = true
  96. super.init(endpoint: kVerifyPasswordEndpoint, requestConfiguration: requestConfiguration)
  97. }
  98. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  99. var body: [String: AnyHashable] = [
  100. kEmailKey: email,
  101. kPasswordKey: password,
  102. ]
  103. if let pendingIDToken {
  104. body[kPendingIDTokenKey] = pendingIDToken
  105. }
  106. if let captchaChallenge {
  107. body[kCaptchaChallengeKey] = captchaChallenge
  108. }
  109. if let captchaResponse {
  110. body[kCaptchaResponseKey] = captchaResponse
  111. }
  112. if let recaptchaVersion {
  113. body[kRecaptchaVersion] = recaptchaVersion
  114. }
  115. if returnSecureToken {
  116. body[kReturnSecureTokenKey] = true
  117. }
  118. if let tenantID {
  119. body[kTenantIDKey] = tenantID
  120. }
  121. body[kClientType] = clientType
  122. return body
  123. }
  124. func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) {
  125. captchaResponse = recaptchaResponse
  126. self.recaptchaVersion = recaptchaVersion
  127. }
  128. }