GetOOBConfirmationCodeRequest.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. enum GetOOBConfirmationCodeRequestType: Int {
  16. /** @var FIRGetOOBConfirmationCodeRequestTypePasswordReset
  17. @brief Requests a password reset code.
  18. */
  19. case passwordReset
  20. /** @var FIRGetOOBConfirmationCodeRequestTypeVerifyEmail
  21. @brief Requests an email verification code.
  22. */
  23. case verifyEmail
  24. /** @var FIRGetOOBConfirmationCodeRequestTypeEmailLink
  25. @brief Requests an email sign-in link.
  26. */
  27. case emailLink
  28. /** @var FIRGetOOBConfirmationCodeRequestTypeVerifyBeforeUpdateEmail
  29. @brief Requests an verify before update email.
  30. */
  31. case verifyBeforeUpdateEmail
  32. var value: String {
  33. switch self {
  34. case .passwordReset:
  35. return kPasswordResetRequestTypeValue
  36. case .verifyEmail:
  37. return kVerifyEmailRequestTypeValue
  38. case .emailLink:
  39. return kEmailLinkSignInTypeValue
  40. case .verifyBeforeUpdateEmail:
  41. return kVerifyBeforeUpdateEmailRequestTypeValue
  42. }
  43. }
  44. }
  45. private let kGetOobConfirmationCodeEndpoint = "getOobConfirmationCode"
  46. /** @var kRequestTypeKey
  47. @brief The name of the required "requestType" property in the request.
  48. */
  49. private let kRequestTypeKey = "requestType"
  50. /** @var kEmailKey
  51. @brief The name of the "email" property in the request.
  52. */
  53. private let kEmailKey = "email"
  54. /** @var kNewEmailKey
  55. @brief The name of the "newEmail" property in the request.
  56. */
  57. private let kNewEmailKey = "newEmail"
  58. /** @var kIDTokenKey
  59. @brief The key for the "idToken" value in the request. This is actually the STS Access Token,
  60. despite it's confusing (backwards compatiable) parameter name.
  61. */
  62. private let kIDTokenKey = "idToken"
  63. /** @var kContinueURLKey
  64. @brief The key for the "continue URL" value in the request.
  65. */
  66. private let kContinueURLKey = "continueUrl"
  67. /** @var kIosBundeIDKey
  68. @brief The key for the "iOS Bundle Identifier" value in the request.
  69. */
  70. private let kIosBundleIDKey = "iOSBundleId"
  71. /** @var kAndroidPackageNameKey
  72. @brief The key for the "Android Package Name" value in the request.
  73. */
  74. private let kAndroidPackageNameKey = "androidPackageName"
  75. /** @var kAndroidInstallAppKey
  76. @brief The key for the request parameter indicating whether the android app should be installed
  77. or not.
  78. */
  79. private let kAndroidInstallAppKey = "androidInstallApp"
  80. /** @var kAndroidMinimumVersionKey
  81. @brief The key for the "minimum Android version supported" value in the request.
  82. */
  83. private let kAndroidMinimumVersionKey = "androidMinimumVersion"
  84. /** @var kCanHandleCodeInAppKey
  85. @brief The key for the request parameter indicating whether the action code can be handled in
  86. the app or not.
  87. */
  88. private let kCanHandleCodeInAppKey = "canHandleCodeInApp"
  89. /** @var kDynamicLinkDomainKey
  90. @brief The key for the "dynamic link domain" value in the request.
  91. */
  92. private let kDynamicLinkDomainKey = "dynamicLinkDomain"
  93. /** @var kPasswordResetRequestTypeValue
  94. @brief The value for the "PASSWORD_RESET" request type.
  95. */
  96. private let kPasswordResetRequestTypeValue = "PASSWORD_RESET"
  97. /** @var kEmailLinkSignInTypeValue
  98. @brief The value for the "EMAIL_SIGNIN" request type.
  99. */
  100. private let kEmailLinkSignInTypeValue = "EMAIL_SIGNIN"
  101. /** @var kVerifyEmailRequestTypeValue
  102. @brief The value for the "VERIFY_EMAIL" request type.
  103. */
  104. private let kVerifyEmailRequestTypeValue = "VERIFY_EMAIL"
  105. /** @var kVerifyBeforeUpdateEmailRequestTypeValue
  106. @brief The value for the "VERIFY_AND_CHANGE_EMAIL" request type.
  107. */
  108. private let kVerifyBeforeUpdateEmailRequestTypeValue = "VERIFY_AND_CHANGE_EMAIL"
  109. /** @var kTenantIDKey
  110. @brief The key for the tenant id value in the request.
  111. */
  112. private let kTenantIDKey = "tenantId"
  113. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  114. public class GetOOBConfirmationCodeRequest: IdentityToolkitRequest, AuthRPCRequest {
  115. typealias Response = GetOOBConfirmationCodeResponse
  116. /** @property requestType
  117. @brief The types of OOB Confirmation Code to request.
  118. */
  119. let requestType: GetOOBConfirmationCodeRequestType
  120. /** @property email
  121. @brief The email of the user.
  122. @remarks For password reset.
  123. */
  124. public var email: String?
  125. /** @property updatedEmail
  126. @brief The new email to be updated.
  127. @remarks For verifyBeforeUpdateEmail.
  128. */
  129. public var updatedEmail: String?
  130. /** @property accessToken
  131. @brief The STS Access Token of the authenticated user.
  132. @remarks For email change.
  133. */
  134. public var accessToken: String?
  135. /** @property continueURL
  136. @brief This URL represents the state/Continue URL in the form of a universal link.
  137. */
  138. public var continueURL: String?
  139. /** @property iOSBundleID
  140. @brief The iOS bundle Identifier, if available.
  141. */
  142. public var iOSBundleID: String?
  143. /** @property androidPackageName
  144. @brief The Android package name, if available.
  145. */
  146. public var androidPackageName: String?
  147. /** @property androidMinimumVersion
  148. @brief The minimum Android version supported, if available.
  149. */
  150. public var androidMinimumVersion: String?
  151. /** @property androidInstallIfNotAvailable
  152. @brief Indicates whether or not the Android app should be installed if not already available.
  153. */
  154. public var androidInstallApp: Bool
  155. /** @property handleCodeInApp
  156. @brief Indicates whether the action code link will open the app directly or after being
  157. redirected from a Firebase owned web widget.
  158. */
  159. public var handleCodeInApp: Bool
  160. /** @property dynamicLinkDomain
  161. @brief The Firebase Dynamic Link domain used for out of band code flow.
  162. */
  163. public var dynamicLinkDomain: String?
  164. /** @fn initWithRequestType:email:APIKey:
  165. @brief Designated initializer.
  166. @param requestType The types of OOB Confirmation Code to request.
  167. @param email The email of the user.
  168. @param newEmail The email of the user to be updated.
  169. @param accessToken The STS Access Token of the currently signed in user.
  170. @param actionCodeSettings An object of FIRActionCodeSettings which specifies action code
  171. settings to be applied to the OOB code request.
  172. @param requestConfiguration An object containing configurations to be added to the request.
  173. */
  174. required init(requestType: GetOOBConfirmationCodeRequestType,
  175. email: String?,
  176. newEmail: String?,
  177. accessToken: String?,
  178. actionCodeSettings: ActionCodeSettings?,
  179. requestConfiguration: AuthRequestConfiguration) {
  180. self.requestType = requestType
  181. self.email = email
  182. updatedEmail = newEmail
  183. self.accessToken = accessToken
  184. continueURL = actionCodeSettings?.url?.absoluteString
  185. iOSBundleID = actionCodeSettings?.iOSBundleID
  186. androidPackageName = actionCodeSettings?.androidPackageName
  187. androidMinimumVersion = actionCodeSettings?.androidMinimumVersion
  188. androidInstallApp = actionCodeSettings?.androidInstallIfNotAvailable ?? false
  189. handleCodeInApp = actionCodeSettings?.handleCodeInApp ?? false
  190. dynamicLinkDomain = actionCodeSettings?.dynamicLinkDomain
  191. super.init(
  192. endpoint: kGetOobConfirmationCodeEndpoint,
  193. requestConfiguration: requestConfiguration
  194. )
  195. }
  196. public static func passwordResetRequest(email: String,
  197. actionCodeSettings: ActionCodeSettings?,
  198. requestConfiguration: AuthRequestConfiguration) ->
  199. GetOOBConfirmationCodeRequest {
  200. Self(requestType: .passwordReset,
  201. email: email,
  202. newEmail: nil,
  203. accessToken: nil,
  204. actionCodeSettings: actionCodeSettings,
  205. requestConfiguration: requestConfiguration)
  206. }
  207. public static func verifyEmailRequest(accessToken: String,
  208. actionCodeSettings: ActionCodeSettings?,
  209. requestConfiguration: AuthRequestConfiguration) ->
  210. GetOOBConfirmationCodeRequest {
  211. Self(requestType: .verifyEmail,
  212. email: nil,
  213. newEmail: nil,
  214. accessToken: accessToken,
  215. actionCodeSettings: actionCodeSettings,
  216. requestConfiguration: requestConfiguration)
  217. }
  218. public static func signInWithEmailLinkRequest(_ email: String,
  219. actionCodeSettings: ActionCodeSettings?,
  220. requestConfiguration: AuthRequestConfiguration)
  221. -> Self {
  222. Self(requestType: .emailLink,
  223. email: email,
  224. newEmail: nil,
  225. accessToken: nil,
  226. actionCodeSettings: actionCodeSettings,
  227. requestConfiguration: requestConfiguration)
  228. }
  229. public static func verifyBeforeUpdateEmail(accessToken: String,
  230. newEmail: String,
  231. actionCodeSettings: ActionCodeSettings?,
  232. requestConfiguration: AuthRequestConfiguration)
  233. -> Self {
  234. Self(requestType: .verifyBeforeUpdateEmail,
  235. email: nil,
  236. newEmail: newEmail,
  237. accessToken: accessToken,
  238. actionCodeSettings: actionCodeSettings,
  239. requestConfiguration: requestConfiguration)
  240. }
  241. public func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  242. var body: [String: AnyHashable] = [
  243. kRequestTypeKey: requestType.value,
  244. ]
  245. // For password reset requests, we only need an email address in addition to the already
  246. // required fields.
  247. if case .passwordReset = requestType {
  248. body[kEmailKey] = email
  249. }
  250. // For verify email requests, we only need an STS Access Token in addition to the already
  251. // required fields.
  252. if case .verifyEmail = requestType {
  253. body[kIDTokenKey] = accessToken
  254. }
  255. // For email sign-in link requests, we only need an email address in addition to the already
  256. // required fields.
  257. if case .emailLink = requestType {
  258. body[kEmailKey] = email
  259. }
  260. // For email sign-in link requests, we only need an STS Access Token, a new email address in
  261. // addition to the already required fields.
  262. if case .verifyBeforeUpdateEmail = requestType {
  263. body[kNewEmailKey] = updatedEmail
  264. body[kIDTokenKey] = accessToken
  265. }
  266. if let continueURL = continueURL {
  267. body[kContinueURLKey] = continueURL
  268. }
  269. if let iOSBundleID = iOSBundleID {
  270. body[kIosBundleIDKey] = iOSBundleID
  271. }
  272. if let androidPackageName = androidPackageName {
  273. body[kAndroidPackageNameKey] = androidPackageName
  274. }
  275. if let androidMinimumVersion = androidMinimumVersion {
  276. body[kAndroidMinimumVersionKey] = androidMinimumVersion
  277. }
  278. if androidInstallApp {
  279. body[kAndroidInstallAppKey] = true
  280. }
  281. if handleCodeInApp {
  282. body[kCanHandleCodeInAppKey] = true
  283. }
  284. if let dynamicLinkDomain = dynamicLinkDomain {
  285. body[kDynamicLinkDomainKey] = dynamicLinkDomain
  286. }
  287. if let tenantID = tenantID {
  288. body[kTenantIDKey] = tenantID
  289. }
  290. return body
  291. }
  292. }