GitHubAuthProvider.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 public 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. 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. static var supportsSecureCoding = true
  45. func encode(with coder: NSCoder) {
  46. coder.encode(token, forKey: "token")
  47. }
  48. required init?(coder: NSCoder) {
  49. guard let token = coder.decodeObject(of: NSString.self, forKey: "token") as? String else {
  50. return nil
  51. }
  52. self.token = token
  53. super.init(provider: GitHubAuthProvider.id)
  54. }
  55. }