VerifyPhoneNumberRequest.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. let verificationID: String?
  53. /// The verification code provided by the user.
  54. let 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. let temporaryProof: String?
  59. /// The phone number to be verified in the request.
  60. let phoneNumber: String?
  61. /// The type of operation triggering this verify phone number request.
  62. let operation: AuthOperationType
  63. /// Convenience 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. convenience init(temporaryProof: String, phoneNumber: String, operation: AuthOperationType,
  70. requestConfiguration: AuthRequestConfiguration) {
  71. self.init(
  72. temporaryProof: temporaryProof,
  73. phoneNumber: phoneNumber,
  74. verificationID: nil,
  75. verificationCode: nil,
  76. operation: operation,
  77. requestConfiguration: requestConfiguration
  78. )
  79. }
  80. /// Convenience initializer.
  81. /// - Parameter verificationID: The verification ID obtained from the response of
  82. /// `sendVerificationCode`.
  83. /// - Parameter verificationCode: The verification code provided by the user.
  84. /// - Parameter operation: Indicates what operation triggered the verify phone number request.
  85. /// - Parameter requestConfiguration: An object containing configurations to be added to the
  86. /// request.
  87. convenience init(verificationID: String,
  88. verificationCode: String,
  89. operation: AuthOperationType,
  90. requestConfiguration: AuthRequestConfiguration) {
  91. self.init(
  92. temporaryProof: nil,
  93. phoneNumber: nil,
  94. verificationID: verificationID,
  95. verificationCode: verificationCode,
  96. operation: operation,
  97. requestConfiguration: requestConfiguration
  98. )
  99. }
  100. private init(temporaryProof: String?, phoneNumber: String?, verificationID: String?,
  101. verificationCode: String?, operation: AuthOperationType,
  102. requestConfiguration: AuthRequestConfiguration) {
  103. self.temporaryProof = temporaryProof
  104. self.phoneNumber = phoneNumber
  105. self.verificationID = verificationID
  106. self.verificationCode = verificationCode
  107. self.operation = operation
  108. super.init(
  109. endpoint: kVerifyPhoneNumberEndPoint,
  110. requestConfiguration: requestConfiguration,
  111. useIdentityPlatform: false,
  112. useStaging: false
  113. )
  114. }
  115. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  116. var postBody: [String: AnyHashable] = [:]
  117. if let verificationID {
  118. postBody[kVerificationIDKey] = verificationID
  119. }
  120. if let verificationCode {
  121. postBody[kVerificationCodeKey] = verificationCode
  122. }
  123. if let accessToken {
  124. postBody[kIDTokenKey] = accessToken
  125. }
  126. if let temporaryProof {
  127. postBody[kTemporaryProofKey] = temporaryProof
  128. }
  129. if let phoneNumber {
  130. postBody[kPhoneNumberKey] = phoneNumber
  131. }
  132. if let tenantID {
  133. postBody[kTenantIDKey] = tenantID
  134. }
  135. postBody[kOperationKey] = operation.operationString
  136. return postBody
  137. }
  138. }