GoogleAuthProvider.swift 2.5 KB

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