GetOOBConfirmationCodeRequest.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. @objc(FIRGetOOBConfirmationCodeRequest)
  115. public class GetOOBConfirmationCodeRequest: IdentityToolkitRequest,
  116. AuthRPCRequest {
  117. /** @property requestType
  118. @brief The types of OOB Confirmation Code to request.
  119. */
  120. let requestType: GetOOBConfirmationCodeRequestType
  121. /** @property email
  122. @brief The email of the user.
  123. @remarks For password reset.
  124. */
  125. @objc public var email: String?
  126. /** @property updatedEmail
  127. @brief The new email to be updated.
  128. @remarks For verifyBeforeUpdateEmail.
  129. */
  130. @objc public var updatedEmail: String?
  131. /** @property accessToken
  132. @brief The STS Access Token of the authenticated user.
  133. @remarks For email change.
  134. */
  135. @objc public var accessToken: String?
  136. /** @property continueURL
  137. @brief This URL represents the state/Continue URL in the form of a universal link.
  138. */
  139. @objc public var continueURL: String?
  140. /** @property iOSBundleID
  141. @brief The iOS bundle Identifier, if available.
  142. */
  143. @objc public var iOSBundleID: String?
  144. /** @property androidPackageName
  145. @brief The Android package name, if available.
  146. */
  147. @objc public var androidPackageName: String?
  148. /** @property androidMinimumVersion
  149. @brief The minimum Android version supported, if available.
  150. */
  151. @objc public var androidMinimumVersion: String?
  152. /** @property androidInstallIfNotAvailable
  153. @brief Indicates whether or not the Android app should be installed if not already available.
  154. */
  155. @objc public var androidInstallApp: Bool
  156. /** @property handleCodeInApp
  157. @brief Indicates whether the action code link will open the app directly or after being
  158. redirected from a Firebase owned web widget.
  159. */
  160. @objc public var handleCodeInApp: Bool
  161. /** @property dynamicLinkDomain
  162. @brief The Firebase Dynamic Link domain used for out of band code flow.
  163. */
  164. @objc public var dynamicLinkDomain: String?
  165. /** @var response
  166. @brief The corresponding response for this request
  167. */
  168. @objc public var response: AuthRPCResponse = GetOOBConfirmationCodeResponse()
  169. /** @fn initWithRequestType:email:APIKey:
  170. @brief Designated initializer.
  171. @param requestType The types of OOB Confirmation Code to request.
  172. @param email The email of the user.
  173. @param newEmail The email of the user to be updated.
  174. @param accessToken The STS Access Token of the currently signed in user.
  175. @param actionCodeSettings An object of FIRActionCodeSettings which specifies action code
  176. settings to be applied to the OOB code request.
  177. @param requestConfiguration An object containing configurations to be added to the request.
  178. */
  179. required init(requestType: GetOOBConfirmationCodeRequestType,
  180. email: String?,
  181. newEmail: String?,
  182. accessToken: String?,
  183. actionCodeSettings: ActionCodeSettings?,
  184. requestConfiguration: AuthRequestConfiguration) {
  185. self.requestType = requestType
  186. self.email = email
  187. updatedEmail = newEmail
  188. self.accessToken = accessToken
  189. continueURL = actionCodeSettings?.url?.absoluteString
  190. iOSBundleID = actionCodeSettings?.iOSBundleID
  191. androidPackageName = actionCodeSettings?.androidPackageName
  192. androidMinimumVersion = actionCodeSettings?.androidMinimumVersion
  193. androidInstallApp = actionCodeSettings?.androidInstallIfNotAvailable ?? false
  194. handleCodeInApp = actionCodeSettings?.handleCodeInApp ?? false
  195. dynamicLinkDomain = actionCodeSettings?.dynamicLinkDomain
  196. super.init(
  197. endpoint: kGetOobConfirmationCodeEndpoint,
  198. requestConfiguration: requestConfiguration
  199. )
  200. }
  201. @objc public static func passwordResetRequest(email: String,
  202. actionCodeSettings: ActionCodeSettings?,
  203. requestConfiguration: AuthRequestConfiguration) ->
  204. GetOOBConfirmationCodeRequest {
  205. Self(requestType: .passwordReset,
  206. email: email,
  207. newEmail: nil,
  208. accessToken: nil,
  209. actionCodeSettings: actionCodeSettings,
  210. requestConfiguration: requestConfiguration)
  211. }
  212. @objc public static func verifyEmailRequest(accessToken: String,
  213. actionCodeSettings: ActionCodeSettings?,
  214. requestConfiguration: AuthRequestConfiguration) ->
  215. GetOOBConfirmationCodeRequest {
  216. Self(requestType: .verifyEmail,
  217. email: nil,
  218. newEmail: nil,
  219. accessToken: accessToken,
  220. actionCodeSettings: actionCodeSettings,
  221. requestConfiguration: requestConfiguration)
  222. }
  223. @objc public static func signInWithEmailLinkRequest(_ email: String,
  224. actionCodeSettings: ActionCodeSettings?,
  225. requestConfiguration: AuthRequestConfiguration)
  226. -> Self {
  227. Self(requestType: .emailLink,
  228. email: email,
  229. newEmail: nil,
  230. accessToken: nil,
  231. actionCodeSettings: actionCodeSettings,
  232. requestConfiguration: requestConfiguration)
  233. }
  234. @objc public static func verifyBeforeUpdateEmail(accessToken: String,
  235. newEmail: String,
  236. actionCodeSettings: ActionCodeSettings?,
  237. requestConfiguration: AuthRequestConfiguration)
  238. -> Self {
  239. Self(requestType: .verifyBeforeUpdateEmail,
  240. email: nil,
  241. newEmail: newEmail,
  242. accessToken: accessToken,
  243. actionCodeSettings: actionCodeSettings,
  244. requestConfiguration: requestConfiguration)
  245. }
  246. public func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  247. var body: [String: AnyHashable] = [
  248. kRequestTypeKey: requestType.value,
  249. ]
  250. // For password reset requests, we only need an email address in addition to the already required
  251. // fields.
  252. if case .passwordReset = requestType {
  253. body[kEmailKey] = email
  254. }
  255. // For verify email requests, we only need an STS Access Token in addition to the already required
  256. // fields.
  257. if case .verifyEmail = requestType {
  258. body[kIDTokenKey] = accessToken
  259. }
  260. // For email sign-in link requests, we only need an email address in addition to the already
  261. // required fields.
  262. if case .emailLink = requestType {
  263. body[kEmailKey] = email
  264. }
  265. // For email sign-in link requests, we only need an STS Access Token, a new email address in
  266. // addition to the already required fields.
  267. if case .verifyBeforeUpdateEmail = requestType {
  268. body[kNewEmailKey] = updatedEmail
  269. body[kIDTokenKey] = accessToken
  270. }
  271. if let continueURL = continueURL {
  272. body[kContinueURLKey] = continueURL
  273. }
  274. if let iOSBundleID = iOSBundleID {
  275. body[kIosBundleIDKey] = iOSBundleID
  276. }
  277. if let androidPackageName = androidPackageName {
  278. body[kAndroidPackageNameKey] = androidPackageName
  279. }
  280. if let androidMinimumVersion = androidMinimumVersion {
  281. body[kAndroidMinimumVersionKey] = androidMinimumVersion
  282. }
  283. if androidInstallApp {
  284. body[kAndroidInstallAppKey] = true
  285. }
  286. if handleCodeInApp {
  287. body[kCanHandleCodeInAppKey] = true
  288. }
  289. if let dynamicLinkDomain = dynamicLinkDomain {
  290. body[kDynamicLinkDomainKey] = dynamicLinkDomain
  291. }
  292. if let tenantID = tenantID {
  293. body[kTenantIDKey] = tenantID
  294. }
  295. return body
  296. }
  297. }