StartMFASignInRequest.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 kStartMFASignInEndPoint = "accounts/mfaSignIn:start"
  16. /// The key for the tenant id value in the request.
  17. private let kTenantIDKey = "tenantId"
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. class StartMFASignInRequest: IdentityToolkitRequest, AuthRPCRequest,
  20. @unchecked Sendable /* TODO: sendable */ {
  21. typealias Response = StartMFASignInResponse
  22. let MFAPendingCredential: String?
  23. let MFAEnrollmentID: String?
  24. let signInInfo: AuthProtoStartMFAPhoneRequestInfo?
  25. init(MFAPendingCredential: String?, MFAEnrollmentID: String?,
  26. signInInfo: AuthProtoStartMFAPhoneRequestInfo?,
  27. requestConfiguration: AuthRequestConfiguration) {
  28. self.MFAPendingCredential = MFAPendingCredential
  29. self.MFAEnrollmentID = MFAEnrollmentID
  30. self.signInInfo = signInInfo
  31. super.init(
  32. endpoint: kStartMFASignInEndPoint,
  33. requestConfiguration: requestConfiguration,
  34. useIdentityPlatform: true
  35. )
  36. }
  37. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  38. var body: [String: AnyHashable] = [:]
  39. if let MFAPendingCredential = MFAPendingCredential {
  40. body["mfaPendingCredential"] = MFAPendingCredential
  41. }
  42. if let MFAEnrollmentID = MFAEnrollmentID {
  43. body["mfaEnrollmentId"] = MFAEnrollmentID
  44. }
  45. if let signInInfo = signInInfo {
  46. body["phoneSignInInfo"] = signInInfo.dictionary
  47. }
  48. if let tenantID = tenantID {
  49. body[kTenantIDKey] = tenantID
  50. }
  51. return body
  52. }
  53. func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) {
  54. // reCAPTCHA check is only available for phone based MFA
  55. if let signInInfo {
  56. signInInfo.injectRecaptchaFields(
  57. recaptchaResponse: recaptchaResponse,
  58. recaptchaVersion: recaptchaVersion,
  59. clientType: clientType
  60. )
  61. }
  62. }
  63. }