VerifyPhoneNumberRequest.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /// The "verifyPhoneNumber" endpoint.
  16. private let kVerifyPhoneNumberEndPoint = "verifyPhoneNumber"
  17. /// The key for the verification ID parameter in the request.
  18. private let kVerificationIDKey = "sessionInfo"
  19. /// The key for the verification code parameter in the request.
  20. private let kVerificationCodeKey = "code"
  21. /// The key for the "ID Token" value in the request.
  22. private let kIDTokenKey = "idToken"
  23. /// The key for the temporary proof value in the request.
  24. private let kTemporaryProofKey = "temporaryProof"
  25. /// The key for the phone number value in the request.
  26. private let kPhoneNumberKey = "phoneNumber"
  27. /// The key for the operation value in the request.
  28. private let kOperationKey = "operation"
  29. /// The key for the tenant id value in the request.
  30. private let kTenantIDKey = "tenantId"
  31. extension AuthOperationType {
  32. /// - Returns: The string value corresponding to the AuthOperationType.
  33. var operationString: String {
  34. switch self {
  35. case .unspecified:
  36. return "VERIFY_OP_UNSPECIFIED"
  37. case .signUpOrSignIn:
  38. return "SIGN_UP_OR_IN"
  39. case .reauth:
  40. return "REAUTH"
  41. case .link:
  42. return "LINK"
  43. case .update:
  44. return "UPDATE"
  45. }
  46. }
  47. }
  48. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  49. class VerifyPhoneNumberRequest: IdentityToolkitRequest, AuthRPCRequest {
  50. typealias Response = VerifyPhoneNumberResponse
  51. /// The verification ID obtained from the response of `sendVerificationCode`.
  52. var verificationID: String?
  53. /// The verification code provided by the user.
  54. var verificationCode: String?
  55. /// The STS Access Token for the authenticated user.
  56. var accessToken: String?
  57. /// The temporary proof code, previously returned from the backend.
  58. var temporaryProof: String?
  59. /// The phone number to be verified in the request.
  60. var phoneNumber: String?
  61. /// The type of operation triggering this verify phone number request.
  62. var operation: AuthOperationType
  63. /// Designated initializer.
  64. /// - Parameter temporaryProof: The temporary proof sent by the backed.
  65. /// - Parameter phoneNumber: The phone number associated with the credential to be signed in .
  66. /// - Parameter operation: Indicates what operation triggered the verify phone number request.
  67. /// - Parameter requestConfiguration: An object containing configurations to be added to the
  68. /// request.
  69. init(temporaryProof: String, phoneNumber: String, operation: AuthOperationType,
  70. requestConfiguration: AuthRequestConfiguration) {
  71. self.temporaryProof = temporaryProof
  72. self.phoneNumber = phoneNumber
  73. self.operation = operation
  74. super.init(endpoint: kVerifyPhoneNumberEndPoint, requestConfiguration: requestConfiguration)
  75. }
  76. /// Designated initializer.
  77. /// - Parameter verificationID: The verification ID obtained from the response of
  78. /// `sendVerificationCode`.
  79. /// - Parameter verificationCode: The verification code provided by the user.
  80. /// - Parameter operation: Indicates what operation triggered the verify phone number request.
  81. /// - Parameter requestConfiguration: An object containing configurations to be added to the
  82. /// request.
  83. init(verificationID: String,
  84. verificationCode: String,
  85. operation: AuthOperationType,
  86. requestConfiguration: AuthRequestConfiguration) {
  87. self.verificationID = verificationID
  88. self.verificationCode = verificationCode
  89. self.operation = operation
  90. super.init(endpoint: kVerifyPhoneNumberEndPoint, requestConfiguration: requestConfiguration)
  91. }
  92. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  93. var postBody: [String: AnyHashable] = [:]
  94. if let verificationID {
  95. postBody[kVerificationIDKey] = verificationID
  96. }
  97. if let verificationCode {
  98. postBody[kVerificationCodeKey] = verificationCode
  99. }
  100. if let accessToken {
  101. postBody[kIDTokenKey] = accessToken
  102. }
  103. if let temporaryProof {
  104. postBody[kTemporaryProofKey] = temporaryProof
  105. }
  106. if let phoneNumber {
  107. postBody[kPhoneNumberKey] = phoneNumber
  108. }
  109. if let tenantID {
  110. postBody[kTenantIDKey] = tenantID
  111. }
  112. postBody[kOperationKey] = operation.operationString
  113. return postBody
  114. }
  115. }