SendVerificationTokenRequest.swift 3.5 KB

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