TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Feb 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Adam Sharp.
GitHub authentication providers for Superb.
SuperbGitHub implements two different authentication flows. Choose the flow that best suits your application's needs.
GitHubOAuthProvider
implements the GitHub OAuth
web application flow using Safari View Controller.
Register your provider:
// GitHub+Providers.swift
import Superb
import SuperbGitHub
extension GitHubOAuthProvider {
static var shared: GitHubOAuthProvider {
return Superb.register(
GitHubOAuthProvider(
clientId: "<your client id>",
clientSecret: "<your client secret>",
redirectURI: URL(string: "<your chosen redirect URI>")!
)
)
}
}
Don't forget to add your app's URL scheme to your Info.plist.
Handle redirects:
// AppDelegate.swift
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool {
return Superb.handleAuthenticationRedirect(url, options: options)
}
}
Use RequestAuthorizer
to perform authorized API requests:
// GitHubAPIClient.swift
struct GitHubAPIClient {
static let oauthClient = GitHubAPIClient(
requestAuthorizer: RequestAuthorizer(
authorizationProvider: GitHubOAuthProvider.shared
)
)
// ...
func getProfile(completionHandler: @escaping (Result<Profile, SuperbError>) -> Void) {
let request = URLRequest(url: URL(string: "https://api.github.com/user")!)
authorizer.performAuthorized(request) { result in
switch result {
case let .success(data, _):
let profile = parseProfile(from: data)
completionHandler(.success(profile))
case let .failure(error):
completionHandler(.failure(error))
}
}
}
}
// later
let api = GitHubAPIClient.oauthClient
api.getProfile { result in
// ...
}
GitHubBasicAuthProvider
implements the GitHub OAuth non-web application flow
using a simple UIAlertController
to prompt the user for their credentials.
The credentials are then used to create a personal access token,
and the user's credentials are discarded.
Register your provider:
// GitHub+Providers.swift
import Superb
import SuperbGitHub
extension GitHubBasicAuthProvider {
static var shared: GitHubBasicAuthProvider {
return Superb.register(
GitHubBasicAuthProvider()
)
}
}
Use RequestAuthorizer
to perform authorized API requests:
// GitHubAPIClient.swift
extension GitHubAPIClient {
static let basicAuthClient = GitHubAPIClient(
requestAuthorizer: RequestAuthorizer(
authorizationProvider: GitHubBasicAuthProvider.shared
)
)
}
// later
let api = GitHubAPIClient.basicAuthClient
api.getProfile { result in
// ...
}
See the CONTRIBUTING document. Thank you, contributors!
SuperbGitHub is Copyright (c) 2017 thoughtbot, inc. It is free software, and may be redistributed under the terms specified in the LICENSE file.
SuperbGitHub is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc.
We love open source software! See our other projects or hire us to help build your product.