StartMFASignInRequest.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. typealias Response = StartMFASignInResponse
  21. let MFAPendingCredential: String?
  22. let MFAEnrollmentID: String?
  23. let signInInfo: AuthProtoStartMFAPhoneRequestInfo?
  24. init(MFAPendingCredential: String?, MFAEnrollmentID: String?,
  25. signInInfo: AuthProtoStartMFAPhoneRequestInfo?,
  26. requestConfiguration: AuthRequestConfiguration) {
  27. self.MFAPendingCredential = MFAPendingCredential
  28. self.MFAEnrollmentID = MFAEnrollmentID
  29. self.signInInfo = signInInfo
  30. super.init(
  31. endpoint: kStartMFASignInEndPoint,
  32. requestConfiguration: requestConfiguration,
  33. useIdentityPlatform: true
  34. )
  35. }
  36. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  37. var body: [String: AnyHashable] = [:]
  38. if let MFAPendingCredential = MFAPendingCredential {
  39. body["mfaPendingCredential"] = MFAPendingCredential
  40. }
  41. if let MFAEnrollmentID = MFAEnrollmentID {
  42. body["mfaEnrollmentId"] = MFAEnrollmentID
  43. }
  44. if let signInInfo = signInInfo {
  45. body["phoneSignInInfo"] = signInInfo.dictionary
  46. }
  47. if let tenantID = tenantID {
  48. body[kTenantIDKey] = tenantID
  49. }
  50. return body
  51. }
  52. func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) {
  53. // reCAPTCHA check is only available for phone based MFA
  54. if let signInInfo {
  55. signInInfo.injectRecaptchaFields(
  56. recaptchaResponse: recaptchaResponse,
  57. recaptchaVersion: recaptchaVersion,
  58. clientType: clientType
  59. )
  60. }
  61. }
  62. }