SendVerificationTokenRequest.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 kSendVerificationCodeEndPoint
  16. @brief The "sendVerificationCodeEnd" endpoint.
  17. */
  18. private let kSendVerificationCodeEndPoint = "sendVerificationCode"
  19. /** @var kPhoneNumberKey
  20. @brief The key for the Phone Number parameter in the request.
  21. */
  22. private let kPhoneNumberKey = "phoneNumber"
  23. /** @var kReceiptKey
  24. @brief The key for the receipt parameter in the request.
  25. */
  26. private let kReceiptKey = "iosReceipt"
  27. /** @var kSecretKey
  28. @brief The key for the Secret parameter in the request.
  29. */
  30. private let kSecretKey = "iosSecret"
  31. /** @var kreCAPTCHATokenKey
  32. @brief The key for the reCAPTCHAToken parameter in the request.
  33. */
  34. private let kreCAPTCHATokenKey = "recaptchaToken"
  35. /** @var kTenantIDKey
  36. @brief The key for the tenant id value in the request.
  37. */
  38. private let kTenantIDKey = "tenantId"
  39. /// A verification code can be an appCredential or a reCaptcha Token
  40. enum CodeIdentity {
  41. case credential(AuthAppCredential)
  42. case recaptcha(String)
  43. case empty
  44. }
  45. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  46. class SendVerificationCodeRequest: IdentityToolkitRequest, AuthRPCRequest {
  47. typealias Response = SendVerificationCodeResponse
  48. /** @property phoneNumber
  49. @brief The phone number to which the verification code should be sent.
  50. */
  51. let phoneNumber: String
  52. /** @property verificationCode
  53. @brief The credential or reCAPTCHA token to prove the identity of the app in order to send the verification code.
  54. */
  55. let codeIdentity: CodeIdentity
  56. init(phoneNumber: String, codeIdentity: CodeIdentity,
  57. requestConfiguration: AuthRequestConfiguration) {
  58. self.phoneNumber = phoneNumber
  59. self.codeIdentity = codeIdentity
  60. super.init(
  61. endpoint: kSendVerificationCodeEndPoint,
  62. requestConfiguration: requestConfiguration
  63. )
  64. }
  65. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  66. var postBody: [String: AnyHashable] = [:]
  67. postBody[kPhoneNumberKey] = phoneNumber
  68. switch codeIdentity {
  69. case let .credential(appCredential):
  70. postBody[kReceiptKey] = appCredential.receipt
  71. postBody[kSecretKey] = appCredential.secret
  72. case let .recaptcha(reCAPTCHAToken):
  73. postBody[kreCAPTCHATokenKey] = reCAPTCHAToken
  74. case .empty: break
  75. }
  76. if let tenantID {
  77. postBody[kTenantIDKey] = tenantID
  78. }
  79. return postBody
  80. }
  81. }