SignUpNewUserRequest.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "SignupNewUserEndpoint" endpoint.
  16. private let kSignupNewUserEndpoint = "signupNewUser"
  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 "kDisplayName" value in the request.
  22. private let kDisplayNameKey = "displayName"
  23. /// The key for the "kIDToken" value in the request.
  24. private let kIDToken = "idToken"
  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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  36. class SignUpNewUserRequest: IdentityToolkitRequest, AuthRPCRequest,
  37. @unchecked Sendable /* TODO: sendable */ {
  38. typealias Response = SignUpNewUserResponse
  39. /// The email of the user.
  40. private(set) var email: String?
  41. /// The password inputted by the user.
  42. private(set) var password: String?
  43. /// The password inputted by the user.
  44. private(set) var displayName: String?
  45. /// The idToken of the user.
  46. private(set) var idToken: String?
  47. /// Response to the captcha.
  48. var captchaResponse: String?
  49. /// The reCAPTCHA version.
  50. var recaptchaVersion: String?
  51. /// Whether the response should return access token and refresh token directly.
  52. /// The default value is `true`.
  53. var returnSecureToken: Bool = true
  54. init(requestConfiguration: AuthRequestConfiguration) {
  55. super.init(endpoint: kSignupNewUserEndpoint, requestConfiguration: requestConfiguration)
  56. }
  57. /// Designated initializer.
  58. /// - Parameter requestConfiguration: An object containing configurations to be added to the
  59. /// request.
  60. init(email: String?,
  61. password: String?,
  62. displayName: String?,
  63. idToken: String?,
  64. requestConfiguration: AuthRequestConfiguration) {
  65. self.email = email
  66. self.password = password
  67. self.displayName = displayName
  68. self.idToken = idToken
  69. super.init(endpoint: kSignupNewUserEndpoint, requestConfiguration: requestConfiguration)
  70. }
  71. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  72. var postBody: [String: AnyHashable] = [:]
  73. if let email {
  74. postBody[kEmailKey] = email
  75. }
  76. if let password {
  77. postBody[kPasswordKey] = password
  78. }
  79. if let displayName {
  80. postBody[kDisplayNameKey] = displayName
  81. }
  82. if let idToken {
  83. postBody[kIDToken] = idToken
  84. }
  85. if let captchaResponse {
  86. postBody[kCaptchaResponseKey] = captchaResponse
  87. }
  88. postBody[kClientType] = clientType
  89. if let recaptchaVersion {
  90. postBody[kRecaptchaVersion] = recaptchaVersion
  91. }
  92. if returnSecureToken {
  93. postBody[kReturnSecureTokenKey] = true
  94. }
  95. if let tenantID {
  96. postBody[kTenantIDKey] = tenantID
  97. }
  98. return postBody
  99. }
  100. func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) {
  101. captchaResponse = recaptchaResponse
  102. self.recaptchaVersion = recaptchaVersion
  103. }
  104. }