SetAccountInfoRequest.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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?
  66. /// The email of the user.
  67. var email: String?
  68. /// The photoURL of the user.
  69. var photoURL: URL?
  70. /// The new password of the user.
  71. var password: String?
  72. /// The associated identity providers of the user.
  73. var providers: [String]?
  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?
  82. /// Response to the captcha.
  83. var captchaResponse: String?
  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]?
  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(requestConfiguration: AuthRequestConfiguration) {
  95. super.init(endpoint: kSetAccountInfoEndpoint, requestConfiguration: requestConfiguration)
  96. }
  97. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  98. var postBody: [String: AnyHashable] = [:]
  99. if let accessToken {
  100. postBody[kIDTokenKey] = accessToken
  101. }
  102. if let displayName {
  103. postBody[kDisplayNameKey] = displayName
  104. }
  105. if let localID {
  106. postBody[kLocalIDKey] = localID
  107. }
  108. if let email {
  109. postBody[kEmailKey] = email
  110. }
  111. if let password {
  112. postBody[kPasswordKey] = password
  113. }
  114. if let photoURL {
  115. postBody[kPhotoURLKey] = photoURL.absoluteString
  116. }
  117. if let providers {
  118. postBody[kProvidersKey] = providers
  119. }
  120. if let oobCode {
  121. postBody[kOOBCodeKey] = oobCode
  122. }
  123. if emailVerified {
  124. postBody[kEmailVerifiedKey] = true
  125. }
  126. if upgradeToFederatedLogin {
  127. postBody[kUpgradeToFederatedLoginKey] = true
  128. }
  129. if let captchaChallenge {
  130. postBody[kCaptchaChallengeKey] = captchaChallenge
  131. }
  132. if let captchaResponse {
  133. postBody[kCaptchaResponseKey] = captchaResponse
  134. }
  135. if let deleteAttributes {
  136. postBody[kDeleteAttributesKey] = deleteAttributes
  137. }
  138. if let deleteProviders {
  139. postBody[kDeleteProvidersKey] = deleteProviders
  140. }
  141. if returnSecureToken {
  142. postBody[kReturnSecureTokenKey] = true
  143. }
  144. if let tenantID {
  145. postBody[kTenantIDKey] = tenantID
  146. }
  147. return postBody
  148. }
  149. }