SetAccountInfoRequest.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. private let FIRSetAccountInfoUserAttributeEmail = "EMAIL"
  16. private let FIRSetAccountInfoUserAttributeDisplayName = "DISPLAY_NAME"
  17. private let FIRSetAccountInfoUserAttributeProvider = "PROVIDER"
  18. private let FIRSetAccountInfoUserAttributePhotoURL = "PHOTO_URL"
  19. private let FIRSetAccountInfoUserAttributePassword = "PASSWORD"
  20. /** @var kCreateAuthURIEndpoint
  21. @brief The "setAccountInfo" endpoint.
  22. */
  23. private let kSetAccountInfoEndpoint = "setAccountInfo"
  24. /** @var kIDTokenKey
  25. @brief The key for the "idToken" value in the request. This is actually the STS Access Token,
  26. despite it's confusing (backwards compatiable) parameter name.
  27. */
  28. private let kIDTokenKey = "idToken"
  29. /** @var kDisplayNameKey
  30. @brief The key for the "displayName" value in the request.
  31. */
  32. private let kDisplayNameKey = "displayName"
  33. /** @var kLocalIDKey
  34. @brief The key for the "localID" value in the request.
  35. */
  36. private let kLocalIDKey = "localId"
  37. /** @var kEmailKey
  38. @brief The key for the "email" value in the request.
  39. */
  40. private let kEmailKey = "email"
  41. /** @var kPasswordKey
  42. @brief The key for the "password" value in the request.
  43. */
  44. private let kPasswordKey = "password"
  45. /** @var kPhotoURLKey
  46. @brief The key for the "photoURL" value in the request.
  47. */
  48. private let kPhotoURLKey = "photoUrl"
  49. /** @var kProvidersKey
  50. @brief The key for the "providers" value in the request.
  51. */
  52. private let kProvidersKey = "provider"
  53. /** @var kOOBCodeKey
  54. @brief The key for the "OOBCode" value in the request.
  55. */
  56. private let kOOBCodeKey = "oobCode"
  57. /** @var kEmailVerifiedKey
  58. @brief The key for the "emailVerified" value in the request.
  59. */
  60. private let kEmailVerifiedKey = "emailVerified"
  61. /** @var kUpgradeToFederatedLoginKey
  62. @brief The key for the "upgradeToFederatedLogin" value in the request.
  63. */
  64. private let kUpgradeToFederatedLoginKey = "upgradeToFederatedLogin"
  65. /** @var kCaptchaChallengeKey
  66. @brief The key for the "captchaChallenge" value in the request.
  67. */
  68. private let kCaptchaChallengeKey = "captchaChallenge"
  69. /** @var kCaptchaResponseKey
  70. @brief The key for the "captchaResponse" value in the request.
  71. */
  72. private let kCaptchaResponseKey = "captchaResponse"
  73. /** @var kDeleteAttributesKey
  74. @brief The key for the "deleteAttribute" value in the request.
  75. */
  76. private let kDeleteAttributesKey = "deleteAttribute"
  77. /** @var kDeleteProvidersKey
  78. @brief The key for the "deleteProvider" value in the request.
  79. */
  80. private let kDeleteProvidersKey = "deleteProvider"
  81. /** @var kReturnSecureTokenKey
  82. @brief The key for the "returnSecureToken" value in the request.
  83. */
  84. private let kReturnSecureTokenKey = "returnSecureToken"
  85. /** @var kTenantIDKey
  86. @brief The key for the tenant id value in the request.
  87. */
  88. private let kTenantIDKey = "tenantId"
  89. /** @class FIRSetAccountInfoRequest
  90. @brief Represents the parameters for the setAccountInfo endpoint.
  91. @see https://developers.google.com/identity/toolkit/web/reference/relyingparty/setAccountInfo
  92. */
  93. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  94. class SetAccountInfoRequest: IdentityToolkitRequest, AuthRPCRequest {
  95. typealias Response = SetAccountInfoResponse
  96. /** @property accessToken
  97. @brief The STS Access Token of the authenticated user.
  98. */
  99. var accessToken: String?
  100. /** @property displayName
  101. @brief The name of the user.
  102. */
  103. var displayName: String?
  104. /** @property localID
  105. @brief The local ID of the user.
  106. */
  107. var localID: String?
  108. /** @property email
  109. @brief The email of the user.
  110. */
  111. var email: String?
  112. /** @property photoURL
  113. @brief The photoURL of the user.
  114. */
  115. var photoURL: URL?
  116. /** @property password
  117. @brief The new password of the user.
  118. */
  119. var password: String?
  120. /** @property providers
  121. @brief The associated identity providers of the user.
  122. */
  123. var providers: [String]?
  124. /** @property OOBCode
  125. @brief The out-of-band code of the change email request.
  126. */
  127. var oobCode: String?
  128. /** @property emailVerified
  129. @brief Whether to mark the email as verified or not.
  130. */
  131. var emailVerified: Bool = false
  132. /** @property upgradeToFederatedLogin
  133. @brief Whether to mark the user to upgrade to federated login.
  134. */
  135. var upgradeToFederatedLogin: Bool = false
  136. /** @property captchaChallenge
  137. @brief The captcha challenge.
  138. */
  139. var captchaChallenge: String?
  140. /** @property captchaResponse
  141. @brief Response to the captcha.
  142. */
  143. var captchaResponse: String?
  144. /** @property deleteAttributes
  145. @brief The list of user attributes to delete.
  146. @remarks Every element of the list must be one of the predefined constant starts with
  147. "FIRSetAccountInfoUserAttribute".
  148. */
  149. var deleteAttributes: [String]?
  150. /** @property deleteProviders
  151. @brief The list of identity providers to delete.
  152. */
  153. var deleteProviders: [String]?
  154. /** @property returnSecureToken
  155. @brief Whether the response should return access token and refresh token directly.
  156. @remarks The default value is @c YES .
  157. */
  158. var returnSecureToken: Bool = false
  159. init(requestConfiguration: AuthRequestConfiguration) {
  160. returnSecureToken = true
  161. super.init(endpoint: kSetAccountInfoEndpoint, requestConfiguration: requestConfiguration)
  162. }
  163. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  164. var postBody: [String: AnyHashable] = [:]
  165. if let accessToken {
  166. postBody[kIDTokenKey] = accessToken
  167. }
  168. if let displayName {
  169. postBody[kDisplayNameKey] = displayName
  170. }
  171. if let localID {
  172. postBody[kLocalIDKey] = localID
  173. }
  174. if let email {
  175. postBody[kEmailKey] = email
  176. }
  177. if let password {
  178. postBody[kPasswordKey] = password
  179. }
  180. if let photoURL {
  181. postBody[kPhotoURLKey] = photoURL.absoluteString
  182. }
  183. if let providers {
  184. postBody[kProvidersKey] = providers
  185. }
  186. if let oobCode {
  187. postBody[kOOBCodeKey] = oobCode
  188. }
  189. if emailVerified {
  190. postBody[kEmailVerifiedKey] = true
  191. }
  192. if upgradeToFederatedLogin {
  193. postBody[kUpgradeToFederatedLoginKey] = true
  194. }
  195. if let captchaChallenge {
  196. postBody[kCaptchaChallengeKey] = captchaChallenge
  197. }
  198. if let captchaResponse {
  199. postBody[kCaptchaResponseKey] = captchaResponse
  200. }
  201. if let deleteAttributes {
  202. postBody[kDeleteAttributesKey] = deleteAttributes
  203. }
  204. if let deleteProviders {
  205. postBody[kDeleteProvidersKey] = deleteProviders
  206. }
  207. if returnSecureToken {
  208. postBody[kReturnSecureTokenKey] = true
  209. }
  210. if let tenantID {
  211. postBody[kTenantIDKey] = tenantID
  212. }
  213. return postBody
  214. }
  215. }