FacebookAuthProvider.swift 2.2 KB

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