GitHubAuthProvider.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. @unchecked Sendable {
  34. let token: String?
  35. init(withToken token: String) {
  36. self.token = token
  37. super.init(provider: GitHubAuthProvider.id)
  38. }
  39. override func prepare(_ request: VerifyAssertionRequest) {
  40. request.providerAccessToken = token
  41. }
  42. // MARK: Secure Coding
  43. public static let supportsSecureCoding = true
  44. func encode(with coder: NSCoder) {
  45. coder.encode(token, forKey: "token")
  46. }
  47. required init?(coder: NSCoder) {
  48. token = coder.decodeObject(of: NSString.self, forKey: "token") as String?
  49. super.init(provider: GitHubAuthProvider.id)
  50. }
  51. }