SignUpNewUserRequest.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 kSignupNewUserEndpoint
  16. @brief The "SingupNewUserEndpoint" endpoint.
  17. */
  18. private let kSignupNewUserEndpoint = "signupNewUser"
  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 kDisplayNameKey
  28. @brief The key for the "kDisplayName" value in the request.
  29. */
  30. private let kDisplayNameKey = "displayName"
  31. /** @var kIDToken
  32. @brief The key for the "kIDToken" value in the request.
  33. */
  34. private let kIDToken = "idToken"
  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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  56. class SignUpNewUserRequest: IdentityToolkitRequest, AuthRPCRequest {
  57. typealias Response = SignUpNewUserResponse
  58. /** @property email
  59. @brief The email of the user.
  60. */
  61. private(set) var email: String?
  62. /** @property password
  63. @brief The password inputed by the user.
  64. */
  65. private(set) var password: String?
  66. /** @property displayName
  67. @brief The password inputed by the user.
  68. */
  69. private(set) var displayName: String?
  70. /** @property idToken
  71. @brief The idToken of the user.
  72. */
  73. private(set) var idToken: String?
  74. /** @property captchaResponse
  75. @brief Response to the captcha.
  76. */
  77. var captchaResponse: String?
  78. /** @property captchaResponse
  79. @brief The reCAPTCHA version.
  80. */
  81. var recaptchaVersion: String?
  82. /** @property returnSecureToken
  83. @brief Whether the response should return access token and refresh token directly.
  84. @remarks The default value is @c YES .
  85. */
  86. var returnSecureToken: Bool = true
  87. init(requestConfiguration: AuthRequestConfiguration) {
  88. super.init(endpoint: kSignupNewUserEndpoint, requestConfiguration: requestConfiguration)
  89. }
  90. /** @fn initWithAPIKey:email:password:displayName:requestConfiguration
  91. @brief Designated initializer.
  92. @param requestConfiguration An object containing configurations to be added to the request.
  93. */
  94. init(email: String?,
  95. password: String?,
  96. displayName: String?,
  97. idToken: String?,
  98. requestConfiguration: AuthRequestConfiguration) {
  99. self.email = email
  100. self.password = password
  101. self.displayName = displayName
  102. self.idToken = idToken
  103. super.init(endpoint: kSignupNewUserEndpoint, requestConfiguration: requestConfiguration)
  104. }
  105. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  106. var postBody: [String: AnyHashable] = [:]
  107. if let email {
  108. postBody[kEmailKey] = email
  109. }
  110. if let password {
  111. postBody[kPasswordKey] = password
  112. }
  113. if let displayName {
  114. postBody[kDisplayNameKey] = displayName
  115. }
  116. if let idToken {
  117. postBody[kIDToken] = idToken
  118. }
  119. if let captchaResponse {
  120. postBody[kCaptchaResponseKey] = captchaResponse
  121. }
  122. postBody[kClientType] = clientType
  123. if let recaptchaVersion {
  124. postBody[kRecaptchaVersion] = recaptchaVersion
  125. }
  126. if returnSecureToken {
  127. postBody[kReturnSecureTokenKey] = true
  128. }
  129. if let tenantID {
  130. postBody[kTenantIDKey] = tenantID
  131. }
  132. return postBody
  133. }
  134. func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) {
  135. captchaResponse = recaptchaResponse
  136. self.recaptchaVersion = recaptchaVersion
  137. }
  138. }