EmailLinkSignInRequest.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 kEmailLinkSigninEndpoint
  16. @brief The "EmailLinkSignin" endpoint.
  17. */
  18. private let kEmailLinkSigninEndpoint = "emailLinkSignin"
  19. /** @var kEmailKey
  20. @brief The key for the "identifier" value in the request.
  21. */
  22. private let kEmailKey = "email"
  23. /** @var kEmailLinkKey
  24. @brief The key for the "emailLink" value in the request.
  25. */
  26. private let kOOBCodeKey = "oobCode"
  27. /** @var kIDTokenKey
  28. @brief The key for the "IDToken" value in the request.
  29. */
  30. private let kIDTokenKey = "idToken"
  31. /** @var kPostBodyKey
  32. @brief The key for the "postBody" value in the request.
  33. */
  34. private let kPostBodyKey = "postBody"
  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 EmailLinkSignInRequest: IdentityToolkitRequest, AuthRPCRequest {
  41. typealias Response = EmailLinkSignInResponse
  42. let email: String
  43. /** @property oobCode
  44. @brief The OOB code used to complete the email link sign-in flow.
  45. */
  46. let oobCode: String
  47. /** @property IDToken
  48. @brief The ID Token code potentially used to complete the email link sign-in flow.
  49. */
  50. var idToken: String?
  51. init(email: String, oobCode: String,
  52. requestConfiguration: AuthRequestConfiguration) {
  53. self.email = email
  54. self.oobCode = oobCode
  55. super.init(endpoint: kEmailLinkSigninEndpoint, requestConfiguration: requestConfiguration)
  56. }
  57. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  58. var postBody: [String: AnyHashable] = [
  59. kEmailKey: email,
  60. kOOBCodeKey: oobCode,
  61. ]
  62. if let idToken = idToken {
  63. postBody[kIDTokenKey] = idToken
  64. }
  65. if let tenantID = tenantID {
  66. postBody[kTenantIDKey] = tenantID
  67. }
  68. return postBody
  69. }
  70. }