FacebookAuthProvider.swift 2.1 KB

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