GitHubAuthProvider.swift 2.1 KB

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