GoogleAuthProvider.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. let idToken: String
  36. let accessToken: String
  37. init(withIDToken idToken: String, accessToken: String) {
  38. self.idToken = idToken
  39. self.accessToken = accessToken
  40. super.init(provider: GoogleAuthProvider.id)
  41. }
  42. override func prepare(_ request: VerifyAssertionRequest) {
  43. request.providerIDToken = idToken
  44. request.providerAccessToken = accessToken
  45. }
  46. // MARK: Secure Coding
  47. static var supportsSecureCoding = true
  48. func encode(with coder: NSCoder) {
  49. coder.encode(idToken, forKey: "idToken")
  50. coder.encode(accessToken, forKey: "accessToken")
  51. }
  52. required init?(coder: NSCoder) {
  53. guard let idToken = coder.decodeObject(of: NSString.self, forKey: "idToken") as? String,
  54. let accessToken = coder.decodeObject(of: NSString.self, forKey: "accessToken") as? String
  55. else {
  56. return nil
  57. }
  58. self.idToken = idToken
  59. self.accessToken = accessToken
  60. super.init(provider: GoogleAuthProvider.id)
  61. }
  62. }