Skip to content

Commit 28bfdb7

Browse files
committed
Fix for refresh token not being cleared after it expires, preventing re-authorization. [Issue p2#367](p2#367)
1 parent 2934f35 commit 28bfdb7

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Sources/Flows/OAuth2.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ open class OAuth2: OAuth2Base {
9898

9999
- parameter params: Optional key/value pairs to pass during authorization and token refresh
100100
- parameter callback: The callback to call when authorization finishes (parameters will be non-nil but may be an empty dict), fails or
101-
is canceled (error will be non-nil, e.g. `.requestCancelled` if auth was aborted)
101+
is canceled (error will be non-nil, e.g. `.requestCancelled` if auth was aborted)
102102
*/
103103
public final func authorize(params: OAuth2StringDict? = nil, callback: @escaping ((OAuth2JSON?, OAuth2Error?) -> Void)) {
104104
if isAuthorizing {
@@ -142,7 +142,7 @@ open class OAuth2: OAuth2Base {
142142
- parameter from: The context to start authorization from, depends on platform (UIViewController or NSWindow, see `authorizeContext`)
143143
- parameter params: Optional key/value pairs to pass during authorization
144144
- parameter callback: The callback to call when authorization finishes (parameters will be non-nil but may be an empty dict), fails or
145-
is canceled (error will be non-nil, e.g. `.requestCancelled` if auth was aborted)
145+
is canceled (error will be non-nil, e.g. `.requestCancelled` if auth was aborted)
146146
*/
147147
open func authorizeEmbedded(from context: AnyObject, params: OAuth2StringDict? = nil, callback: @escaping ((_ authParameters: OAuth2JSON?, _ error: OAuth2Error?) -> Void)) {
148148
if isAuthorizing { // `authorize()` will check this, but we want to exit before changing `authConfig`
@@ -181,7 +181,7 @@ open class OAuth2: OAuth2Base {
181181

182182
- parameter params: Optional key/value pairs to pass during authorization
183183
- parameter callback: The callback to call once the client knows whether it has an access token or not; if `success` is true an
184-
access token is present
184+
access token is present
185185
*/
186186
open func tryToObtainAccessTokenIfNeeded(params: OAuth2StringDict? = nil, callback: @escaping ((OAuth2JSON?, OAuth2Error?) -> Void)) {
187187
if hasUnexpiredAccessToken() {
@@ -268,7 +268,7 @@ open class OAuth2: OAuth2Base {
268268
Method that creates the OAuth2AuthRequest instance used to create the authorize URL
269269

270270
- parameter redirect: The redirect URI string to supply. If it is nil, the first value of the settings' `redirect_uris` entries is
271-
used. Must be present in the end!
271+
used. Must be present in the end!
272272
- parameter scope: The scope to request
273273
- parameter params: Any additional parameters as dictionary with string keys and values that will be added to the query part
274274
- returns: OAuth2AuthRequest to be used to call to the authorize endpoint
@@ -318,7 +318,7 @@ open class OAuth2: OAuth2Base {
318318
Convenience method to be overridden by and used from subclasses.
319319

320320
- parameter redirect: The redirect URI string to supply. If it is nil, the first value of the settings' `redirect_uris` entries is
321-
used. Must be present in the end!
321+
used. Must be present in the end!
322322
- parameter scope: The scope to request
323323
- parameter params: Any additional parameters as dictionary with string keys and values that will be added to the query part
324324
- returns: NSURL to be used to start the OAuth dance
@@ -391,6 +391,9 @@ open class OAuth2: OAuth2Base {
391391
callback(json, nil)
392392
}
393393
catch let error {
394+
// Fixes [Issue #367](https://github.com/p2/OAuth2/issues/367)
395+
// Refresh token needs to be cleared out upon error, otherwise re-authorizing will not ocurr because the library thinks it has a valid refresh token and tries to fetch a new access token with an expired refresh token.
396+
self.clientConfig.refreshToken = nil
394397
self.logger?.debug("OAuth2", msg: "Error refreshing access token: \(error)")
395398
callback(nil, error.asOAuth2Error)
396399
}
@@ -412,7 +415,7 @@ open class OAuth2: OAuth2Base {
412415
If both are nil, instantiates a blank `OAuth2DynReg` instead, then attempts client registration.
413416

414417
- parameter callback: The callback to call on the main thread; if both json and error is nil no registration was attempted; error is nil
415-
on success
418+
on success
416419
*/
417420
public func registerClientIfNeeded(callback: @escaping ((OAuth2JSON?, OAuth2Error?) -> Void)) {
418421
if nil != clientId || !type(of: self).clientIdMandatory {

0 commit comments

Comments
 (0)