OAuth2 frameworks for OS X and iOS written in Swift. This is more of an academic exercise and very much WiP. Here is a very nice explanation of OAuth's basics: The OAuth Bible.
The code in this repo requires Xcode 6 to compile and will run on OS X 10.9+ or iOS 7+.
For a full OAuth 2 code grant flow you want to use the OAuth2CodeGrant class.
This flow is typically used by applications that can guard their secrets, like server-side apps, and not in distributed binaries.
In case an application cannot guard its secret, such as a distributed iOS app, you would use the implicit grant or, in some cases, still a code grant but omitting the client secret.
For a typical code grant flow you want to perform the following steps:
-
Create a settings dictionary.
settings = [ "client_id" = "my_swift_app", "client_secret" = "C7447242-A0CF-47C5-BAC7-B38BA91970A9", "authorize_uri" = "https://authorize.smartplatforms.org/authorize", "token_uri" = "https://authorize.smartplatforms.org/token", ]
-
Create an
OAuth2CodeGrantinstance, setting the delegate if you want to receive thedidAuthorizecallback.let oauth = OAuth2CodeGrant(settings: settings) oauth.delegate = self
-
Open the authorize URL in the browser (or an embedded web view).
let redir = "myapp://callback" // don't forget to register this scheme let scope = "profile email" let url = oauth.authorizeURLWithRedirect(redir, scope: scope, params: nil) UIApplication.sharedApplication().openURL(url)
-
When the callback is called (intercept in your app delegate or in your web view), let the OAuth2 instance handle the full URL.
oauth.handleRedirectURL(<redirectURL>) { error in if error { // something went wrong } else { // we now have `accessToken`!! } }
-
You can now obtain a
OAuth2Request, which is an already signedNSMutableURLRequest, to retrieve data from your server.let req = oauth.request(forURL: <a resource URL>) let session = NSURLSession() let task = session.dataTaskWithRequest(req) { data, response, error in if error { // something went wrong } else { // check the response and the data // you have just received data with an OAuth2-signed request! } } task.resume()
An implicit grant is suitable for apps that are not capable of guarding their secret, such as distributed binaries or client-side web apps.
Use the OAuth2ImplicitGrant class to receive a token and perform requests.
Would be nice to add another code example here, but it's pretty much the same as for the code grant.
The idea is to add a Playground to see OAuth2 in use (instead of a sample app). However, it's not currently possible to import custom code into playgrounds, so currently there is no Playground.