EmailLinkSignInRequest.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "EmailLinkSignin" endpoint.
  16. private let kEmailLinkSigninEndpoint = "emailLinkSignin"
  17. /// The key for the "identifier" value in the request.
  18. private let kEmailKey = "email"
  19. /// The key for the "emailLink" value in the request.
  20. private let kOOBCodeKey = "oobCode"
  21. /// The key for the "IDToken" value in the request.
  22. private let kIDTokenKey = "idToken"
  23. /// The key for the "postBody" value in the request.
  24. private let kPostBodyKey = "postBody"
  25. /// The key for the tenant id value in the request.
  26. private let kTenantIDKey = "tenantId"
  27. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  28. class EmailLinkSignInRequest: IdentityToolkitRequest, AuthRPCRequest {
  29. typealias Response = EmailLinkSignInResponse
  30. let email: String
  31. /// The OOB code used to complete the email link sign-in flow.
  32. let oobCode: String
  33. /// The ID Token code potentially used to complete the email link sign-in flow.
  34. var idToken: String?
  35. init(email: String, oobCode: String,
  36. requestConfiguration: AuthRequestConfiguration) {
  37. self.email = email
  38. self.oobCode = oobCode
  39. super.init(endpoint: kEmailLinkSigninEndpoint, requestConfiguration: requestConfiguration)
  40. }
  41. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  42. var postBody: [String: AnyHashable] = [
  43. kEmailKey: email,
  44. kOOBCodeKey: oobCode,
  45. ]
  46. if let idToken = idToken {
  47. postBody[kIDTokenKey] = idToken
  48. }
  49. if let tenantID = tenantID {
  50. postBody[kTenantIDKey] = tenantID
  51. }
  52. return postBody
  53. }
  54. }