Skip to content

Commit 840d6ea

Browse files
committed
Don’t forget refresh tokens in data loader
Only throw away access token, let `OAuth2` base class throw away refresh token if it’s rejected. Concerns #184 .
1 parent c07ccc8 commit 840d6ea

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ You can also refer to commit logs to get details on what was implemented, fixed
88
### x.x.x
99

1010
- Allow more UI customization via `authConfig.UI` and making `OAuth2Authorizer` friendlier to subclassing
11+
- Implement custom authorization UIs for password grants (thanks @amaurydavid !)
1112
- Optionally allow `DataLoader` to follow 302 redirects automatically (on same host)
1213

1314

Sources/DataLoader/OAuth2DataLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ open class OAuth2DataLoader: OAuth2Requestable {
125125
catch OAuth2Error.unauthorizedClient {
126126
if retry {
127127
self.enqueue(request: request, callback: callback)
128+
self.oauth2.clientConfig.accessToken = nil
128129
self.attemptToAuthorize() { json, error in
129130

130131
// dequeue all if we're authorized, throw all away if something went wrong
@@ -159,7 +160,6 @@ open class OAuth2DataLoader: OAuth2Requestable {
159160
open func attemptToAuthorize(callback: @escaping ((OAuth2JSON?, OAuth2Error?) -> Void)) {
160161
if !isAuthorizing {
161162
isAuthorizing = true
162-
oauth2.forgetTokens()
163163
oauth2.authorize() { authParams, error in
164164
self.isAuthorizing = false
165165
callback(authParams, error)

Sources/Flows/OAuth2.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ open class OAuth2: OAuth2Base {
342342
/**
343343
If there is a refresh token, use it to receive a fresh access token.
344344

345+
If the request returns an error, the refresh token is thrown away.
346+
345347
- parameter params: Optional key/value pairs to pass during token refresh
346348
- parameter callback: The callback to call after the refresh token exchange has finished
347349
*/
@@ -355,6 +357,7 @@ open class OAuth2: OAuth2Base {
355357
let data = try response.responseData()
356358
let json = try self.parseRefreshTokenResponseData(data)
357359
if response.response.statusCode >= 400 {
360+
self.clientConfig.refreshToken = nil
358361
throw OAuth2Error.generic("Failed with status \(response.response.statusCode)")
359362
}
360363
self.logger?.debug("OAuth2", msg: "Did use refresh token for access token [\(nil != self.clientConfig.accessToken)]")

0 commit comments

Comments
 (0)