Skip to content

Commit

Permalink
Added specific client error and removed setting the token to nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Catalina Turlea committed Sep 14, 2018
1 parent 6713930 commit 69a2ca5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Sources/Base/OAuth2Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public enum OAuth2Error: Error, CustomStringConvertible, Equatable {

/// There is no delegate associated with the password grant flow instance.
case noPasswordGrantDelegate

case clientError(Int)


// MARK: - Request errors
Expand Down
11 changes: 5 additions & 6 deletions Sources/Flows/OAuth2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ open class OAuth2: OAuth2Base {
/**
If there is a refresh token, use it to receive a fresh access token.

If the request returns an client error, the refresh token is thrown away.
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)

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

0 comments on commit 69a2ca5

Please sign in to comment.