StartMFAEnrollmentRequest.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 kStartMFAEnrollmentEndPoint = "accounts/mfaEnrollment: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 StartMFAEnrollmentRequest: IdentityToolkitRequest, AuthRPCRequest {
  20. typealias Response = StartMFAEnrollmentResponse
  21. let idToken: String?
  22. private(set) var phoneEnrollmentInfo: AuthProtoStartMFAPhoneRequestInfo?
  23. private(set) var totpEnrollmentInfo: AuthProtoStartMFATOTPEnrollmentRequestInfo?
  24. init(idToken: String?,
  25. enrollmentInfo: AuthProtoStartMFAPhoneRequestInfo?,
  26. requestConfiguration: AuthRequestConfiguration) {
  27. self.idToken = idToken
  28. phoneEnrollmentInfo = enrollmentInfo
  29. super.init(
  30. endpoint: kStartMFAEnrollmentEndPoint,
  31. requestConfiguration: requestConfiguration,
  32. useIdentityPlatform: true
  33. )
  34. }
  35. init(idToken: String?,
  36. totpEnrollmentInfo: AuthProtoStartMFATOTPEnrollmentRequestInfo?,
  37. requestConfiguration: AuthRequestConfiguration) {
  38. self.idToken = idToken
  39. self.totpEnrollmentInfo = totpEnrollmentInfo
  40. super.init(
  41. endpoint: kStartMFAEnrollmentEndPoint,
  42. requestConfiguration: requestConfiguration,
  43. useIdentityPlatform: true
  44. )
  45. }
  46. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  47. var body: [String: AnyHashable] = [:]
  48. if let idToken = idToken {
  49. body["idToken"] = idToken
  50. }
  51. if let phoneEnrollmentInfo {
  52. body["phoneEnrollmentInfo"] = phoneEnrollmentInfo.dictionary
  53. } else if let totpEnrollmentInfo {
  54. body["totpEnrollmentInfo"] = totpEnrollmentInfo.dictionary
  55. }
  56. if let tenantID = tenantID {
  57. body[kTenantIDKey] = tenantID
  58. }
  59. return body
  60. }
  61. }