GitHubAuthProvider.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 GitHub Sign In credentials.
  16. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  17. @objc(FIRGitHubAuthProvider) open class GitHubAuthProvider: NSObject {
  18. /// A string constant identifying the GitHub identity provider.
  19. @objc public static let id = "github.com"
  20. /// Creates an `AuthCredential` for a GitHub sign in.
  21. /// - Parameter token: The GitHub OAuth access token.
  22. /// - Returns: An AuthCredential containing the GitHub credentials.
  23. @objc open class func credential(withToken token: String) -> AuthCredential {
  24. return GitHubAuthCredential(withToken: token)
  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(FIRGitHubAuthCredential) class GitHubAuthCredential: AuthCredential, NSSecureCoding {
  33. let token: String
  34. init(withToken token: String) {
  35. self.token = token
  36. super.init(provider: GitHubAuthProvider.id)
  37. }
  38. override func prepare(_ request: VerifyAssertionRequest) {
  39. request.providerAccessToken = token
  40. }
  41. // MARK: Secure Coding
  42. public static var supportsSecureCoding = true
  43. func encode(with coder: NSCoder) {
  44. coder.encode(token, forKey: "token")
  45. }
  46. required init?(coder: NSCoder) {
  47. guard let token = coder.decodeObject(of: NSString.self, forKey: "token") as? String else {
  48. return nil
  49. }
  50. self.token = token
  51. super.init(provider: GitHubAuthProvider.id)
  52. }
  53. }