OAuth2 frameworks for OS X and iOS written in Swift. Still very much WiP and not feature complete.
The code in this repo requires Xcode 6, currently deployment targets have to be OS X 10.9+ or iOS 8+. The iOS 8 requirement stems from us using frameworks which are not compatible with iOS 7. When building static libraries from Swift code becomes possible it should be possible to lower the deployment target to iOS 7.
For a typical code grant flow you want to perform the following steps. The steps for other flows are mostly the same short of instantiating a different subclass and using different client settings.
-
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 theonAuthorizeandonFailureclosures to keep informed about the status.let oauth = OAuth2CodeGrant(settings: settings) oauth.onAuthorize = { parameters in println("Did authorize with parameters: \(parameters)") } oauth.onFailure = { error in println("Authorization went wrong: \(error.localizedDescription)") }
-
Now either use the built-in web view controller or manually open the authorize URL in the browser:
let redir = "myapp://callback" // don't forget to register this scheme let scope = "profile email"
Embedded:
let web = oauth.authorizeEmbedded(redir, scope: scope, params: nil, from: <# view controller #>) oauth.afterAuthorizeOrFailure = { wasFailure in web.dismissViewControllerAnimated(true, completion: nil) }
OS browser:
let url = oauth.authorizeURLWithRedirect(redir, scope: scope, params: nil) UIApplication.sharedApplication().openURL(url)
-
Since you opened the authorize URL in the browser you will need to intercept the callback. When doing so (intercept in your app delegate), let the OAuth2 instance handle the full URL.
func application(application: UIApplication!, openURL url: NSURL!, sourceApplication: String!, annotation: AnyObject!) -> Bool { // you should probably first check if this is your URL being opened if <# check #> { oauth.handleRedirectURL(url) } }
-
-
After everything completes either the
onAuthorizeor theonFailureclosure will be called. -
You can now obtain an
OAuth2Request, which is an already signedNSMutableURLRequest, to retrieve data from your server.let req = oauth.request(forURL: <# resource URL #>) let session = NSURLSession() let task = session.dataTaskWithRequest(req) { data, response, error in if nil != error { // something went wrong } else { // check the response and the data // you have just received data with an OAuth2-signed request! } } task.resume()
Based on which OAuth2 flow that you need to use you will want to use the correct subclass. For a very nice explanation of OAuth's basics: The OAuth Bible.
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.
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.