GoogleAuthProvider.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2022 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. /// Utility class for constructing Google Sign In credentials.
  16. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  17. @objc(FIRGoogleAuthProvider) open class GoogleAuthProvider: NSObject {
  18. /// A string constant identifying the Google identity provider.
  19. @objc public static let id = "google.com"
  20. /// Creates an `AuthCredential` for a Google sign in.
  21. /// - Parameter idToken: The ID Token from Google.
  22. /// - Parameter accessToken: The Access Token from Google.
  23. /// - Returns: An AuthCredential containing the Google credentials.
  24. @objc open class func credential(withIDToken idToken: String,
  25. accessToken: String) -> AuthCredential {
  26. return GoogleAuthCredential(withIDToken: idToken, accessToken: accessToken)
  27. }
  28. @available(*, unavailable)
  29. @objc override public init() {
  30. fatalError("This class is not meant to be initialized.")
  31. }
  32. }
  33. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  34. @objc(FIRGoogleAuthCredential) class GoogleAuthCredential: AuthCredential, NSSecureCoding,
  35. @unchecked Sendable {
  36. let idToken: String?
  37. let accessToken: String?
  38. init(withIDToken idToken: String, accessToken: String) {
  39. self.idToken = idToken
  40. self.accessToken = accessToken
  41. super.init(provider: GoogleAuthProvider.id)
  42. }
  43. override func prepare(_ request: VerifyAssertionRequest) {
  44. request.providerIDToken = idToken
  45. request.providerAccessToken = accessToken
  46. }
  47. // MARK: Secure Coding
  48. static let supportsSecureCoding = true
  49. func encode(with coder: NSCoder) {
  50. coder.encode(idToken, forKey: "idToken")
  51. coder.encode(accessToken, forKey: "accessToken")
  52. }
  53. required init?(coder: NSCoder) {
  54. idToken = coder.decodeObject(of: NSString.self, forKey: "idToken") as String?
  55. accessToken = coder.decodeObject(of: NSString.self, forKey: "accessToken") as String?
  56. super.init(provider: GoogleAuthProvider.id)
  57. }
  58. }