GetOOBConfirmationCodeRequest.swift 11 KB

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