VerifyPhoneNumberRequest.swift 5.6 KB

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