SetAccountInfoRequest.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. typealias Response = SetAccountInfoResponse
  60. /// The STS Access Token of the authenticated user.
  61. var accessToken: String?
  62. /// The name of the user.
  63. var displayName: String?
  64. /// The local ID of the user.
  65. var localID: String? = nil
  66. /// The email of the user.
  67. var email: String? = nil
  68. /// The photoURL of the user.
  69. var photoURL: URL?
  70. /// The new password of the user.
  71. var password: String? = nil
  72. /// The associated identity providers of the user.
  73. var providers: [String]? = nil
  74. /// The out-of-band code of the change email request.
  75. var oobCode: String?
  76. /// Whether to mark the email as verified or not.
  77. var emailVerified: Bool = false
  78. /// Whether to mark the user to upgrade to federated login.
  79. var upgradeToFederatedLogin: Bool = false
  80. /// The captcha challenge.
  81. var captchaChallenge: String? = nil
  82. /// Response to the captcha.
  83. var captchaResponse: String? = nil
  84. /// The list of user attributes to delete.
  85. ///
  86. /// Every element of the list must be one of the predefined constant starts with
  87. /// `SetAccountInfoUserAttribute`.
  88. var deleteAttributes: [String]? = nil
  89. /// The list of identity providers to delete.
  90. var deleteProviders: [String]?
  91. /// Whether the response should return access token and refresh token directly.
  92. /// The default value is `true` .
  93. var returnSecureToken: Bool = true
  94. init(accessToken: String? = nil, requestConfiguration: AuthRequestConfiguration) {
  95. self.accessToken = accessToken
  96. super.init(endpoint: kSetAccountInfoEndpoint, requestConfiguration: requestConfiguration)
  97. }
  98. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  99. var postBody: [String: AnyHashable] = [:]
  100. if let accessToken {
  101. postBody[kIDTokenKey] = accessToken
  102. }
  103. if let displayName {
  104. postBody[kDisplayNameKey] = displayName
  105. }
  106. if let localID {
  107. postBody[kLocalIDKey] = localID
  108. }
  109. if let email {
  110. postBody[kEmailKey] = email
  111. }
  112. if let password {
  113. postBody[kPasswordKey] = password
  114. }
  115. if let photoURL {
  116. postBody[kPhotoURLKey] = photoURL.absoluteString
  117. }
  118. if let providers {
  119. postBody[kProvidersKey] = providers
  120. }
  121. if let oobCode {
  122. postBody[kOOBCodeKey] = oobCode
  123. }
  124. if emailVerified {
  125. postBody[kEmailVerifiedKey] = true
  126. }
  127. if upgradeToFederatedLogin {
  128. postBody[kUpgradeToFederatedLoginKey] = true
  129. }
  130. if let captchaChallenge {
  131. postBody[kCaptchaChallengeKey] = captchaChallenge
  132. }
  133. if let captchaResponse {
  134. postBody[kCaptchaResponseKey] = captchaResponse
  135. }
  136. if let deleteAttributes {
  137. postBody[kDeleteAttributesKey] = deleteAttributes
  138. }
  139. if let deleteProviders {
  140. postBody[kDeleteProvidersKey] = deleteProviders
  141. }
  142. if returnSecureToken {
  143. postBody[kReturnSecureTokenKey] = true
  144. }
  145. if let tenantID {
  146. postBody[kTenantIDKey] = tenantID
  147. }
  148. return postBody
  149. }
  150. }