FinalizeMFAEnrollmentRequest.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 kFinalizeMFAEnrollmentEndPoint = "accounts/mfaEnrollment:finalize"
  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 FinalizeMFAEnrollmentRequest: IdentityToolkitRequest, AuthRPCRequest {
  20. typealias Response = FinalizeMFAEnrollmentResponse
  21. let idToken: String?
  22. let displayName: String?
  23. let phoneVerificationInfo: AuthProtoFinalizeMFAPhoneRequestInfo?
  24. let totpVerificationInfo: AuthProtoFinalizeMFATOTPEnrollmentRequestInfo?
  25. convenience init(idToken: String?, displayName: String?,
  26. phoneVerificationInfo: AuthProtoFinalizeMFAPhoneRequestInfo?,
  27. requestConfiguration: AuthRequestConfiguration) {
  28. self.init(
  29. idToken: idToken,
  30. displayName: displayName,
  31. phoneVerificationInfo: phoneVerificationInfo,
  32. totpVerificationInfo: nil,
  33. requestConfiguration: requestConfiguration
  34. )
  35. }
  36. convenience init(idToken: String?, displayName: String?,
  37. totpVerificationInfo: AuthProtoFinalizeMFATOTPEnrollmentRequestInfo?,
  38. requestConfiguration: AuthRequestConfiguration) {
  39. self.init(
  40. idToken: idToken,
  41. displayName: displayName,
  42. phoneVerificationInfo: nil,
  43. totpVerificationInfo: totpVerificationInfo,
  44. requestConfiguration: requestConfiguration
  45. )
  46. }
  47. private init(idToken: String?, displayName: String?,
  48. phoneVerificationInfo: AuthProtoFinalizeMFAPhoneRequestInfo?,
  49. totpVerificationInfo: AuthProtoFinalizeMFATOTPEnrollmentRequestInfo?,
  50. requestConfiguration: AuthRequestConfiguration) {
  51. self.idToken = idToken
  52. self.displayName = displayName
  53. self.phoneVerificationInfo = phoneVerificationInfo
  54. self.totpVerificationInfo = totpVerificationInfo
  55. super.init(
  56. endpoint: kFinalizeMFAEnrollmentEndPoint,
  57. requestConfiguration: requestConfiguration,
  58. useIdentityPlatform: true,
  59. useStaging: false
  60. )
  61. }
  62. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  63. var body: [String: AnyHashable] = [:]
  64. if let idToken = idToken {
  65. body["idToken"] = idToken
  66. }
  67. if let displayName = displayName {
  68. body["displayName"] = displayName
  69. }
  70. if let phoneVerificationInfo {
  71. body["phoneVerificationInfo"] = phoneVerificationInfo.dictionary
  72. } else if let totpVerificationInfo {
  73. body["totpVerificationInfo"] = totpVerificationInfo.dictionary
  74. }
  75. if let tenantID = tenantID {
  76. body[kTenantIDKey] = tenantID
  77. }
  78. return body
  79. }
  80. }