Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OAuth 2] Start an authorized request by suppling an automatic mechanism of token renewal #209

Merged
merged 10 commits into from Mar 28, 2016

Conversation

fabiomassimo
Copy link
Contributor

Summary

As described in OAuth 2 documentation, the access token has an expiration date. The duration of an access token, together with a refresh token, is provided when the user retrieves a new access token.

This PR aims to:

  • properly store the information about the expiration of a retrieved access token
  • expose, in the OAuth 2 client, a convenience mechanism to automatically renew an expired token by using the refresh token.
  • improve error handling by using a convenience enum struct to store all error codes

Why?

I'm working on a library that helps developers to easily access Google services. I found in OAuthSwift a perfect fit for implementing the OAuth 2 process but I wanted to extend its capabilities with a solid mechanism of access token automatic renewal only based on the access token expiration.

How would I use it?

To kick off a request that needs to be authorized simply call, on a OAuth 2 client, the following method:

oauthClient = OAuth2Swift(consumerKey: consumerKey, consumerSecret: consumerSecret, authorizeUrl: AuthenticationConstants.AuthorizeUrl.rawValue, accessTokenUrl: AuthenticationConstants.AccessTokensUrl.rawValue, responseType: AuthenticationConstants.ResponseType.rawValue)

oauthClient.startAuthorizedRequest(urlString, method: .GET, parameters: parameters, headers: headers, onTokenRenewal: { (credential) in
           // Convenience fallback to store new credential somewhere safe (i.e. the keychain)
            do {
                try self.credential.storeOAuthCredential(credential)
            } catch {
                failure(error: error as NSError)
            }
        }, success: success, failure: failure)

@fabiomassimo fabiomassimo changed the title [OAuth 2] Start authorised request by suppling an automatic mechanism of token renewal [OAuth 2] Start an authorized request by suppling an automatic mechanism of token renewal Mar 28, 2016
@phimage phimage merged commit fc17376 into OAuthSwift:master Mar 28, 2016
@fabiomassimo
Copy link
Contributor Author

Wow! Thank you so much!

@phimage
Copy link
Member

phimage commented Mar 28, 2016

thx, good work

@antwerpenR
Copy link

See my comment on #217. I think there may be a bug - at least when used with Meetup.com.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants