Commit 3ebc42f9 authored by Matija Obreza's avatar Matija Obreza

Throw OAuthException when refresh token no longer valid

parent 4e9423a0
...@@ -24,6 +24,7 @@ import org.apache.log4j.LogManager; ...@@ -24,6 +24,7 @@ import org.apache.log4j.LogManager;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.genesys2.client.oauth.GenesysApiException; import org.genesys2.client.oauth.GenesysApiException;
import org.scribe.builder.api.DefaultApi20; import org.scribe.builder.api.DefaultApi20;
import org.scribe.exceptions.OAuthException;
import org.scribe.extractors.AccessTokenExtractor; import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.extractors.JsonTokenExtractor; import org.scribe.extractors.JsonTokenExtractor;
import org.scribe.model.OAuthConfig; import org.scribe.model.OAuthConfig;
...@@ -37,20 +38,20 @@ import org.scribe.utils.OAuthEncoder; ...@@ -37,20 +38,20 @@ import org.scribe.utils.OAuthEncoder;
* Genesys API v1. * Genesys API v1.
*/ */
public class GenesysApi extends DefaultApi20 { public class GenesysApi extends DefaultApi20 {
/** The Constant _log. */ /** The Constant _log. */
private static final Logger _log = LogManager.getLogger(GenesysApi.class); private static final Logger _log = LogManager.getLogger(GenesysApi.class);
/** OAuth2 authorization endpoint that is appended to {@link #baseUrl}. */ /** OAuth2 authorization endpoint that is appended to {@link #baseUrl}. */
public static final String AUTHORIZE_URL = "/oauth/authorize?client_id=%s&client_secret=%s&response_type=code&redirect_uri=%s"; public static final String AUTHORIZE_URL = "/oauth/authorize?client_id=%s&client_secret=%s&response_type=code&redirect_uri=%s";
/** OAuth2 access token endpoint that is appended to {@link #baseUrl}. */ /** OAuth2 access token endpoint that is appended to {@link #baseUrl}. */
private static final String TOKEN_ENDPOINT = "/oauth/token"; private static final String TOKEN_ENDPOINT = "/oauth/token";
/** The base URL of the Genesys server (e.g. https://www.genesys-pgr.org) */ /** The base URL of the Genesys server (e.g. https://www.genesys-pgr.org) */
private String baseUrl; private String baseUrl;
/** The complete authorization url. */ /** The complete authorization url. */
private String authorizeUrl; private String authorizeUrl;
/** The scoped authorize url. */ /** The scoped authorize url. */
...@@ -162,6 +163,8 @@ public class GenesysApi extends DefaultApi20 { ...@@ -162,6 +163,8 @@ public class GenesysApi extends DefaultApi20 {
_log.debug("Redirect: " + response.getHeader(HttpHeaders.LOCATION)); _log.debug("Redirect: " + response.getHeader(HttpHeaders.LOCATION));
if (response.getCode() >= 200 && response.getCode() < 300) { if (response.getCode() >= 200 && response.getCode() < 300) {
return getAccessTokenExtractor().extract(response.getBody()); return getAccessTokenExtractor().extract(response.getBody());
} else if (response.getCode() == 400) {
throw new OAuthException("Refresh token no longer valid.");
} else { } else {
throw new GenesysApiException("Server responded with unexpected HTTP response code " + response.getCode()); throw new GenesysApiException("Server responded with unexpected HTTP response code " + response.getCode());
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment