SendVerificationTokenRequest.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  40. class SendVerificationCodeRequest: IdentityToolkitRequest, AuthRPCRequest {
  41. typealias Response = SendVerificationCodeResponse
  42. /** @property phoneNumber
  43. @brief The phone number to which the verification code should be sent.
  44. */
  45. let phoneNumber: String
  46. /** @property appCredential
  47. @brief The credential to prove the identity of the app in order to send the verification code.
  48. */
  49. let appCredential: AuthAppCredential?
  50. /** @property reCAPTCHAToken
  51. @brief The reCAPTCHA token to prove the identity of the app in order to send the verification
  52. code.
  53. */
  54. let reCAPTCHAToken: String?
  55. init(phoneNumber: String, appCredential: AuthAppCredential?,
  56. reCAPTCHAToken: String?, requestConfiguration: AuthRequestConfiguration) {
  57. self.phoneNumber = phoneNumber
  58. self.appCredential = appCredential
  59. self.reCAPTCHAToken = reCAPTCHAToken
  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. if let receipt = appCredential?.receipt {
  69. postBody[kReceiptKey] = receipt
  70. }
  71. if let secret = appCredential?.secret {
  72. postBody[kSecretKey] = secret
  73. }
  74. if let reCAPTCHAToken {
  75. postBody[kreCAPTCHATokenKey] = reCAPTCHAToken
  76. }
  77. if let tenantID {
  78. postBody[kTenantIDKey] = tenantID
  79. }
  80. return postBody
  81. }
  82. }