SendVerificationTokenRequest.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 tenant id value in the request.
  26. private let kTenantIDKey = "tenantId"
  27. /// A verification code can be an appCredential or a reCaptcha Token
  28. enum CodeIdentity {
  29. case credential(AuthAppCredential)
  30. case recaptcha(String)
  31. case empty
  32. }
  33. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  34. class SendVerificationCodeRequest: IdentityToolkitRequest, AuthRPCRequest {
  35. typealias Response = SendVerificationCodeResponse
  36. /// The phone number to which the verification code should be sent.
  37. let phoneNumber: String
  38. /// The credential or reCAPTCHA token to prove the identity of the app in order to send the
  39. /// verification code.
  40. let codeIdentity: CodeIdentity
  41. init(phoneNumber: String, codeIdentity: CodeIdentity,
  42. requestConfiguration: AuthRequestConfiguration) {
  43. self.phoneNumber = phoneNumber
  44. self.codeIdentity = codeIdentity
  45. super.init(
  46. endpoint: kSendVerificationCodeEndPoint,
  47. requestConfiguration: requestConfiguration
  48. )
  49. }
  50. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  51. var postBody: [String: AnyHashable] = [:]
  52. postBody[kPhoneNumberKey] = phoneNumber
  53. switch codeIdentity {
  54. case let .credential(appCredential):
  55. postBody[kReceiptKey] = appCredential.receipt
  56. postBody[kSecretKey] = appCredential.secret
  57. case let .recaptcha(reCAPTCHAToken):
  58. postBody[kreCAPTCHATokenKey] = reCAPTCHAToken
  59. case .empty: break
  60. }
  61. if let tenantID {
  62. postBody[kTenantIDKey] = tenantID
  63. }
  64. return postBody
  65. }
  66. }