VerifyPasswordRequest.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 kReturnSecureTokenKey
  40. @brief The key for the "returnSecureToken" value in the request.
  41. */
  42. private let kReturnSecureTokenKey = "returnSecureToken"
  43. /** @var kTenantIDKey
  44. @brief The key for the tenant id value in the request.
  45. */
  46. private let kTenantIDKey = "tenantId"
  47. /** @class FIRVerifyPasswordRequest
  48. @brief Represents the parameters for the verifyPassword endpoint.
  49. @see https://developers.google.com/identity/toolkit/web/reference/relyingparty/verifyPassword
  50. */
  51. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  52. class VerifyPasswordRequest: IdentityToolkitRequest, AuthRPCRequest {
  53. typealias Response = VerifyPasswordResponse
  54. /** @property email
  55. @brief The email of the user.
  56. */
  57. var email: String
  58. /** @property password
  59. @brief The password inputed by the user.
  60. */
  61. var password: String
  62. /** @property pendingIDToken
  63. @brief The GITKit token for the non-trusted IDP, which is to be confirmed by the user.
  64. */
  65. var pendingIDToken: String?
  66. /** @property captchaChallenge
  67. @brief The captcha challenge.
  68. */
  69. var captchaChallenge: String?
  70. /** @property captchaResponse
  71. @brief Response to the captcha.
  72. */
  73. var captchaResponse: String?
  74. /** @property returnSecureToken
  75. @brief Whether the response should return access token and refresh token directly.
  76. @remarks The default value is @c YES .
  77. */
  78. var returnSecureToken: Bool
  79. init(email: String, password: String,
  80. requestConfiguration: AuthRequestConfiguration) {
  81. self.email = email
  82. self.password = password
  83. returnSecureToken = true
  84. super.init(endpoint: kVerifyPasswordEndpoint, requestConfiguration: requestConfiguration)
  85. }
  86. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  87. var body: [String: AnyHashable] = [
  88. kEmailKey: email,
  89. kPasswordKey: password,
  90. ]
  91. if let pendingIDToken = pendingIDToken {
  92. body[kPendingIDTokenKey] = pendingIDToken
  93. }
  94. if let captchaChallenge = captchaChallenge {
  95. body[kCaptchaChallengeKey] = captchaChallenge
  96. }
  97. if let captchaResponse = captchaResponse {
  98. body[kCaptchaResponseKey] = captchaResponse
  99. }
  100. if returnSecureToken {
  101. body[kReturnSecureTokenKey] = true
  102. }
  103. if let tenantID = tenantID {
  104. body[kTenantIDKey] = tenantID
  105. }
  106. return body
  107. }
  108. }