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

Closed
opened 2026-03-03 16:49:29 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/OAuthSwift/OAuthSwift/pull/209
Author: @fabiomassimo
Created: 3/28/2016
Status: Merged
Merged: 3/28/2016
Merged by: @phimage

Base: masterHead: master


📝 Commits (10+)

  • 57f8636 Added expire date for an access token by computing "expires_in" response (see: https://tools.ietf.org/html/rfc6749#appendix-A.14)
  • edd0e62 Implemented parsing of "expires_in" field from the oAuth Access Token' response.
  • 7a6e8d4 Added NSDate helper for comparing NSDate objects with common comparison symbols.
  • 0cff610 Improved error handling by introducing an enum struct that gathers all error used codes.
  • add5697 Updated project file with new helper for NSDate.
  • fe29ed7 Implement token expiration check when crating an authorised request.
  • 1d44095 Implemented convenience method for OAuth 2 that makes possible to fire authorised request by automatically manage the renewal of expired access tokens
  • 2ff16ff Renamed variable that stores the expiration date of an access token
  • b497079 Fixed issue that prevented to store an access token when it is requested
  • d230747 Fixed typo in naming for access token's expiration date variable

📊 Changes

6 files changed (+136 additions, -14 deletions)

View changed files

📝 OAuthSwift.xcodeproj/project.pbxproj (+10 -0)
OAuthSwift/NSDate+OAuthSwift.swift (+23 -0)
📝 OAuthSwift/OAuth2Swift.swift (+64 -9)
📝 OAuthSwift/OAuthSwift.swift (+11 -1)
📝 OAuthSwift/OAuthSwiftClient.swift (+15 -4)
📝 OAuthSwift/OAuthSwiftCredential.swift (+13 -0)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/OAuthSwift/OAuthSwift/pull/209 **Author:** [@fabiomassimo](https://github.com/fabiomassimo) **Created:** 3/28/2016 **Status:** ✅ Merged **Merged:** 3/28/2016 **Merged by:** [@phimage](https://github.com/phimage) **Base:** `master` ← **Head:** `master` --- ### 📝 Commits (10+) - [`57f8636`](https://github.com/OAuthSwift/OAuthSwift/commit/57f863657eb20123c96c82762b61587391a67ac1) Added expire date for an access token by computing "expires_in" response (see: https://tools.ietf.org/html/rfc6749#appendix-A.14) - [`edd0e62`](https://github.com/OAuthSwift/OAuthSwift/commit/edd0e621a32f35db00a4db405ecf681d21f6ea92) Implemented parsing of "expires_in" field from the oAuth Access Token' response. - [`7a6e8d4`](https://github.com/OAuthSwift/OAuthSwift/commit/7a6e8d4e8ae47a87bed5ddf9e294c15a541069c4) Added NSDate helper for comparing NSDate objects with common comparison symbols. - [`0cff610`](https://github.com/OAuthSwift/OAuthSwift/commit/0cff6104e3f8092ffc7968419b7f2f58a01f4165) Improved error handling by introducing an enum struct that gathers all error used codes. - [`add5697`](https://github.com/OAuthSwift/OAuthSwift/commit/add5697086c38617afe184932d1975ef059416d9) Updated project file with new helper for NSDate. - [`fe29ed7`](https://github.com/OAuthSwift/OAuthSwift/commit/fe29ed76287afeb8a32be1707d6af3e7275c1599) Implement token expiration check when crating an authorised request. - [`1d44095`](https://github.com/OAuthSwift/OAuthSwift/commit/1d4409515a398bcd56d09faa36327dfabff05bfe) Implemented convenience method for OAuth 2 that makes possible to fire authorised request by automatically manage the renewal of expired access tokens - [`2ff16ff`](https://github.com/OAuthSwift/OAuthSwift/commit/2ff16ff9a5abddbefd89f300807b496c5f954539) Renamed variable that stores the expiration date of an access token - [`b497079`](https://github.com/OAuthSwift/OAuthSwift/commit/b4970799822307cb17c1fd0071887bb6886cceba) Fixed issue that prevented to store an access token when it is requested - [`d230747`](https://github.com/OAuthSwift/OAuthSwift/commit/d230747cc3e161367e72765b369bbc4349cd0d88) Fixed typo in naming for access token's expiration date variable ### 📊 Changes **6 files changed** (+136 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `OAuthSwift.xcodeproj/project.pbxproj` (+10 -0) ➕ `OAuthSwift/NSDate+OAuthSwift.swift` (+23 -0) 📝 `OAuthSwift/OAuth2Swift.swift` (+64 -9) 📝 `OAuthSwift/OAuthSwift.swift` (+11 -1) 📝 `OAuthSwift/OAuthSwiftClient.swift` (+15 -4) 📝 `OAuthSwift/OAuthSwiftCredential.swift` (+13 -0) </details> ### 📄 Description # 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](https://www.github.com/fabiomassimo/GoogleAuthenticator) 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: ``` swift 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) ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-03 16:49:29 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#570
No description provided.