SetAccountInfoRequest.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /// The "setAccountInfo" endpoint.
  21. private let kSetAccountInfoEndpoint = "setAccountInfo"
  22. /// The key for the "idToken" value in the request. This is actually the STS Access Token,
  23. /// despite its confusing (backwards compatible) parameter name.
  24. private let kIDTokenKey = "idToken"
  25. /// The key for the "displayName" value in the request.
  26. private let kDisplayNameKey = "displayName"
  27. /// The key for the "localID" value in the request.
  28. private let kLocalIDKey = "localId"
  29. /// The key for the "email" value in the request.
  30. private let kEmailKey = "email"
  31. /// The key for the "password" value in the request.
  32. private let kPasswordKey = "password"
  33. /// The key for the "photoURL" value in the request.
  34. private let kPhotoURLKey = "photoUrl"
  35. /// The key for the "providers" value in the request.
  36. private let kProvidersKey = "provider"
  37. /// The key for the "OOBCode" value in the request.
  38. private let kOOBCodeKey = "oobCode"
  39. /// The key for the "emailVerified" value in the request.
  40. private let kEmailVerifiedKey = "emailVerified"
  41. /// The key for the "upgradeToFederatedLogin" value in the request.
  42. private let kUpgradeToFederatedLoginKey = "upgradeToFederatedLogin"
  43. /// The key for the "captchaChallenge" value in the request.
  44. private let kCaptchaChallengeKey = "captchaChallenge"
  45. /// The key for the "captchaResponse" value in the request.
  46. private let kCaptchaResponseKey = "captchaResponse"
  47. /// The key for the "deleteAttribute" value in the request.
  48. private let kDeleteAttributesKey = "deleteAttribute"
  49. /// The key for the "deleteProvider" value in the request.
  50. private let kDeleteProvidersKey = "deleteProvider"
  51. /// The key for the "returnSecureToken" value in the request.
  52. private let kReturnSecureTokenKey = "returnSecureToken"
  53. /// The key for the tenant id value in the request.
  54. private let kTenantIDKey = "tenantId"
  55. /// Represents the parameters for the setAccountInfo endpoint.
  56. /// See https://developers.google.com/identity/toolkit/web/reference/relyingparty/setAccountInfo
  57. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  58. class SetAccountInfoRequest: IdentityToolkitRequest, AuthRPCRequest,
  59. @unchecked Sendable /* TODO: sendable */ {
  60. typealias Response = SetAccountInfoResponse
  61. /// The STS Access Token of the authenticated user.
  62. var accessToken: String?
  63. /// The name of the user.
  64. var displayName: String?
  65. /// The local ID of the user.
  66. var localID: String?
  67. /// The email of the user.
  68. var email: String?
  69. /// The photoURL of the user.
  70. var photoURL: URL?
  71. /// The new password of the user.
  72. var password: String?
  73. /// The associated identity providers of the user.
  74. var providers: [String]?
  75. /// The out-of-band code of the change email request.
  76. var oobCode: String?
  77. /// Whether to mark the email as verified or not.
  78. var emailVerified: Bool = false
  79. /// Whether to mark the user to upgrade to federated login.
  80. var upgradeToFederatedLogin: Bool = false
  81. /// The captcha challenge.
  82. var captchaChallenge: String?
  83. /// Response to the captcha.
  84. var captchaResponse: String?
  85. /// The list of user attributes to delete.
  86. ///
  87. /// Every element of the list must be one of the predefined constant starts with
  88. /// `SetAccountInfoUserAttribute`.
  89. var deleteAttributes: [String]?
  90. /// The list of identity providers to delete.
  91. var deleteProviders: [String]?
  92. /// Whether the response should return access token and refresh token directly.
  93. /// The default value is `true` .
  94. var returnSecureToken: Bool = true
  95. init(accessToken: String? = nil, requestConfiguration: AuthRequestConfiguration) {
  96. self.accessToken = accessToken
  97. super.init(endpoint: kSetAccountInfoEndpoint, requestConfiguration: requestConfiguration)
  98. }
  99. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  100. var postBody: [String: AnyHashable] = [:]
  101. if let accessToken {
  102. postBody[kIDTokenKey] = accessToken
  103. }
  104. if let displayName {
  105. postBody[kDisplayNameKey] = displayName
  106. }
  107. if let localID {
  108. postBody[kLocalIDKey] = localID
  109. }
  110. if let email {
  111. postBody[kEmailKey] = email
  112. }
  113. if let password {
  114. postBody[kPasswordKey] = password
  115. }
  116. if let photoURL {
  117. postBody[kPhotoURLKey] = photoURL.absoluteString
  118. }
  119. if let providers {
  120. postBody[kProvidersKey] = providers
  121. }
  122. if let oobCode {
  123. postBody[kOOBCodeKey] = oobCode
  124. }
  125. if emailVerified {
  126. postBody[kEmailVerifiedKey] = true
  127. }
  128. if upgradeToFederatedLogin {
  129. postBody[kUpgradeToFederatedLoginKey] = true
  130. }
  131. if let captchaChallenge {
  132. postBody[kCaptchaChallengeKey] = captchaChallenge
  133. }
  134. if let captchaResponse {
  135. postBody[kCaptchaResponseKey] = captchaResponse
  136. }
  137. if let deleteAttributes {
  138. postBody[kDeleteAttributesKey] = deleteAttributes
  139. }
  140. if let deleteProviders {
  141. postBody[kDeleteProvidersKey] = deleteProviders
  142. }
  143. if returnSecureToken {
  144. postBody[kReturnSecureTokenKey] = true
  145. }
  146. if let tenantID {
  147. postBody[kTenantIDKey] = tenantID
  148. }
  149. return postBody
  150. }
  151. }