GoogleAuthProvider.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 open 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. // MARK: Secure Coding
  50. static var supportsSecureCoding = true
  51. func encode(with coder: NSCoder) {
  52. coder.encode(idToken, forKey: "idToken")
  53. coder.encode(accessToken, forKey: "accessToken")
  54. }
  55. required init?(coder: NSCoder) {
  56. guard let idToken = coder.decodeObject(of: NSString.self, forKey: "idToken") as? String,
  57. let accessToken = coder.decodeObject(of: NSString.self, forKey: "accessToken") as? String
  58. else {
  59. return nil
  60. }
  61. self.idToken = idToken
  62. self.accessToken = accessToken
  63. super.init(provider: GoogleAuthProvider.id)
  64. }
  65. }