Skip to content

Commit 69a2ca5

Browse files
author
Catalina Turlea
committed
Added specific client error and removed setting the token to nil
1 parent 6713930 commit 69a2ca5

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Sources/Base/OAuth2Error.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public enum OAuth2Error: Error, CustomStringConvertible, Equatable {
8181

8282
/// There is no delegate associated with the password grant flow instance.
8383
case noPasswordGrantDelegate
84+
85+
case clientError(Int)
8486

8587

8688
// MARK: - Request errors

Sources/Flows/OAuth2.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ open class OAuth2: OAuth2Base {
352352
/**
353353
If there is a refresh token, use it to receive a fresh access token.
354354

355-
If the request returns an client error, the refresh token is thrown away.
355+
Does not remove the refresh_token in case of a failure. For client errors (400..<500), the callback will provide the status code in the .clientError(Int)
356356

357357
- parameter params: Optional key/value pairs to pass during token refresh
358358
- parameter callback: The callback to call after the refresh token exchange has finished
@@ -367,13 +367,12 @@ open class OAuth2: OAuth2Base {
367367
let data = try response.responseData()
368368
let json = try self.parseRefreshTokenResponseData(data)
369369
switch response.response.statusCode {
370+
case 500:
371+
throw OAuth2Error.serverError
370372
case 400..<500:
371-
self.clientConfig.refreshToken = nil
372-
throw OAuth2Error.generic("Failed with status \(response.response.statusCode)")
373-
case 500...599:
374-
throw OAuth2Error.generic("Failed with status \(response.response.statusCode)")
373+
throw OAuth2Error.clientError(response.response.statusCode)
375374
default:
376-
break
375+
throw OAuth2Error.generic("Failed with status \(response.response.statusCode)")
377376
}
378377
self.logger?.debug("OAuth2", msg: "Did use refresh token for access token [\(nil != self.clientConfig.accessToken)]")
379378
callback(json, nil)

0 commit comments

Comments
 (0)