GetOOBConfirmationCodeRequest.swift 11 KB

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