[PR #596] [MERGED] Automatically refresh access tokens in OAuthSwiftClient #687

Closed
opened 2026-03-03 17:29:39 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/OAuthSwift/OAuthSwift/pull/596
Author: @Timac
Created: 5/12/2020
Status: Merged
Merged: 5/24/2020
Merged by: @phimage

Base: masterHead: feature/AutomaticallyRefreshAccessTokens


📝 Commits (2)

📊 Changes

5 files changed (+146 additions, -93 deletions)

View changed files

📝 Sources/OAuth2Swift.swift (+5 -77)
📝 Sources/OAuthLogProtocol.swift (+12 -12)
📝 Sources/OAuthSwift.swift (+3 -3)
📝 Sources/OAuthSwiftClient.swift (+125 -0)
📝 Sources/OAuthSwiftCredential.swift (+1 -1)

📄 Description

Summary

Access tokens have an expiration date but OAuthSwift doesn't automatically refresh access tokens by default.

This PR proposes a solution:

  • that makes OAuthSwift automatically refresh access tokens when they expire
  • which is opt-in by providing one simple convenient method
  • containing the minimum amount of changes needed to avoid to break existing apps

Related links:

Changes:

  • Add a new function requestWithAutomaticAccessTokenRenewal in OAuthSwiftClient. When used, it automatically refreshes the access token if it expired and restart the query.
  • Move the implementation for renewAccessToken and requestOAuthAccessToken from OAuth2Swift to OAuthSwiftClient. The methods renewAccessToken and requestOAuthAccessToken are still available in OAuth2Swift but they simply forward the call to OAuthSwiftClient to ensure that old apps don't need to be changed.

Why?

I developed an application, called Clatters, that needs to access various services relying on OAuth. Rather that creating my own OAuth library, I decided to use OAuthSwift as it perfectly fit my needs.

However I believe that OAuthSwift should automatically refresh access tokens when they expire. The app itself shouldn't have to implement the logic to handle expired token.

Clatters has been available in the iOS App Store since February 2020 and relies on the code written in this PR.

Usage

You can call the convenient method requestWithAutomaticAccessTokenRenewal in OAuthSwiftClient with the correct parameters. If the access token expired, requestWithAutomaticAccessTokenRenewal will automatically renew the access token and retry the query. Here is an example to perform a query on Reddit:

static func requestIdentity(client: OAuthSwiftClient) {
		client.requestWithAutomaticAccessTokenRenewal(url: URL(string: "https://oauth.reddit.com/api/v1/me")!, method: .GET, headers: nil, contentType: nil, accessTokenBasicAuthentification: true, accessTokenUrl: "https://www.reddit.com/api/v1/access_token", onTokenRenewal: nil) { (result) in
			switch result {
				case .success(let response):
					// Do something
					debugPrint("Received \(response)")
					break
				case .failure:
					// Fail nicely
					break
			}
		}
    }

Notes

I wanted to keep the changes proposed in this PR really simple. As previously mentioned:

  • this new feature is opt-in by using a convenient method.
  • apps already using OAuthSwift shouldn't be affected and don't need to adopt the new API.
  • no code has been removed or deprecated.

🔄 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/596 **Author:** [@Timac](https://github.com/Timac) **Created:** 5/12/2020 **Status:** ✅ Merged **Merged:** 5/24/2020 **Merged by:** [@phimage](https://github.com/phimage) **Base:** `master` ← **Head:** `feature/AutomaticallyRefreshAccessTokens` --- ### 📝 Commits (2) - [`d530930`](https://github.com/OAuthSwift/OAuthSwift/commit/d530930d42b94b4c53d41b08f7d7a9b1cc52b534) Automatically refresh access tokens in OAuthSwiftClient - [`6ce5deb`](https://github.com/OAuthSwift/OAuthSwift/commit/6ce5debaef77ad9a6c4355ab4eb6b87df4cbbb93) Fix swiftlint warnings introduced in https://github.com/OAuthSwift/OAuthSwift/commit/94f98f745f8d00824174b0d289f683c7043b2d2d ### 📊 Changes **5 files changed** (+146 additions, -93 deletions) <details> <summary>View changed files</summary> 📝 `Sources/OAuth2Swift.swift` (+5 -77) 📝 `Sources/OAuthLogProtocol.swift` (+12 -12) 📝 `Sources/OAuthSwift.swift` (+3 -3) 📝 `Sources/OAuthSwiftClient.swift` (+125 -0) 📝 `Sources/OAuthSwiftCredential.swift` (+1 -1) </details> ### 📄 Description # Summary Access tokens have an expiration date but OAuthSwift doesn't automatically refresh access tokens by default. This PR proposes a solution: - that makes OAuthSwift automatically refresh access tokens when they expire - which is opt-in by providing one simple convenient method - containing the minimum amount of changes needed to avoid to break existing apps Related links: - Issue #217 - PR #209 Changes: - Add a new function `requestWithAutomaticAccessTokenRenewal` in OAuthSwiftClient. When used, it automatically refreshes the access token if it expired and restart the query. - Move the implementation for `renewAccessToken` and `requestOAuthAccessToken` from OAuth2Swift to OAuthSwiftClient. The methods `renewAccessToken` and `requestOAuthAccessToken` are still available in OAuth2Swift but they simply forward the call to OAuthSwiftClient to ensure that old apps don't need to be changed. # Why? I developed an application, called [Clatters](https://apps.apple.com/app/clatters/id1480930237), that needs to access various services relying on OAuth. Rather that creating my own OAuth library, I decided to use OAuthSwift as it perfectly fit my needs. However I believe that OAuthSwift should automatically refresh access tokens when they expire. The app itself shouldn't have to implement the logic to handle expired token. [Clatters](https://apps.apple.com/app/clatters/id1480930237) has been available in the iOS App Store since February 2020 and relies on the code written in this PR. # Usage You can call the convenient method `requestWithAutomaticAccessTokenRenewal` in OAuthSwiftClient with the correct parameters. If the access token expired, `requestWithAutomaticAccessTokenRenewal` will automatically renew the access token and retry the query. Here is an example to perform a query on Reddit: ``` static func requestIdentity(client: OAuthSwiftClient) { client.requestWithAutomaticAccessTokenRenewal(url: URL(string: "https://oauth.reddit.com/api/v1/me")!, method: .GET, headers: nil, contentType: nil, accessTokenBasicAuthentification: true, accessTokenUrl: "https://www.reddit.com/api/v1/access_token", onTokenRenewal: nil) { (result) in switch result { case .success(let response): // Do something debugPrint("Received \(response)") break case .failure: // Fail nicely break } } } ``` # Notes I wanted to keep the changes proposed in this PR really simple. As previously mentioned: - this new feature is opt-in by using a convenient method. - apps already using OAuthSwift shouldn't be affected and don't need to adopt the new API. - no code has been removed or deprecated. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-03 17:29:39 +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#687
No description provided.