SendVerificationTokenRequest.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. typealias Response = SendVerificationCodeResponse
  42. /// The phone number to which the verification code should be sent.
  43. let phoneNumber: String
  44. /// The credential or reCAPTCHA token to prove the identity of the app in order to send the
  45. /// verification code.
  46. let codeIdentity: CodeIdentity
  47. /// Response to the captcha.
  48. var captchaResponse: String?
  49. /// The reCAPTCHA version.
  50. var recaptchaVersion: String?
  51. init(phoneNumber: String, codeIdentity: CodeIdentity,
  52. requestConfiguration: AuthRequestConfiguration) {
  53. self.phoneNumber = phoneNumber
  54. self.codeIdentity = codeIdentity
  55. super.init(
  56. endpoint: kSendVerificationCodeEndPoint,
  57. requestConfiguration: requestConfiguration
  58. )
  59. }
  60. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  61. var postBody: [String: AnyHashable] = [:]
  62. postBody[kPhoneNumberKey] = phoneNumber
  63. switch codeIdentity {
  64. case let .credential(appCredential):
  65. postBody[kReceiptKey] = appCredential.receipt
  66. postBody[kSecretKey] = appCredential.secret
  67. case let .recaptcha(reCAPTCHAToken):
  68. postBody[kreCAPTCHATokenKey] = reCAPTCHAToken
  69. case .empty: break
  70. }
  71. if let captchaResponse {
  72. postBody[kCaptchaResponseKey] = captchaResponse
  73. }
  74. if let recaptchaVersion {
  75. postBody[kRecaptchaVersion] = recaptchaVersion
  76. }
  77. if let tenantID {
  78. postBody[kTenantIDKey] = tenantID
  79. }
  80. postBody[kClientType] = clientType
  81. return postBody
  82. }
  83. func injectRecaptchaFields(recaptchaResponse: String?, recaptchaVersion: String) {
  84. captchaResponse = recaptchaResponse
  85. self.recaptchaVersion = recaptchaVersion
  86. }
  87. }