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