use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project mbed-cloud-sdk-java by ARMmbed.
the class OAuth method retryingIntercept.
private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAuthorizationFailure) throws IOException {
Request request = chain.request();
// If the request already have an authorization (eg. Basic auth), do nothing
if (request.header("Authorization") != null) {
return chain.proceed(request);
}
// If first time, get the token
OAuthClientRequest oAuthRequest;
if (getAccessToken() == null) {
updateAccessToken(null);
}
if (getAccessToken() != null) {
// Build the request
Builder rb = request.newBuilder();
String requestAccessToken = new String(getAccessToken());
try {
oAuthRequest = new OAuthBearerClientRequest(request.url().toString()).setAccessToken(requestAccessToken).buildHeaderMessage();
} catch (OAuthSystemException e) {
throw new IOException(e);
}
for (Map.Entry<String, String> header : oAuthRequest.getHeaders().entrySet()) {
rb.addHeader(header.getKey(), header.getValue());
}
rb.url(oAuthRequest.getLocationUri());
// Execute the request
Response response = chain.proceed(rb.build());
// 401/403 most likely indicates that access token has expired. Unless it happens two times in a row.
if (response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure) {
if (updateAccessToken(requestAccessToken)) {
return retryingIntercept(chain, false);
}
}
return response;
} else {
return chain.proceed(chain.request());
}
}
use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project lusid-sdk-java by finbourne.
the class RetryingOAuth method retryingIntercept.
private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAuthorizationFailure) throws IOException {
Request request = chain.request();
// If the request already has an authorization (e.g. Basic auth), proceed with the request as is
if (request.header("Authorization") != null) {
return chain.proceed(request);
}
// Get the token if it has not yet been acquired
if (getAccessToken() == null) {
updateAccessToken(null);
}
OAuthClientRequest oAuthRequest;
if (getAccessToken() != null) {
// Build the request
Request.Builder requestBuilder = request.newBuilder();
String requestAccessToken = getAccessToken();
try {
oAuthRequest = new OAuthBearerClientRequest(request.url().toString()).setAccessToken(requestAccessToken).buildHeaderMessage();
} catch (OAuthSystemException e) {
throw new IOException(e);
}
Map<String, String> headers = oAuthRequest.getHeaders();
for (String headerName : headers.keySet()) {
requestBuilder.addHeader(headerName, headers.get(headerName));
}
requestBuilder.url(oAuthRequest.getLocationUri());
// Execute the request
Response response = chain.proceed(requestBuilder.build());
// 401/403 response codes most likely indicate an expired access token, unless it happens two times in a row
if (response != null && (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED || response.code() == HttpURLConnection.HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure) {
try {
if (updateAccessToken(requestAccessToken)) {
response.body().close();
return retryingIntercept(chain, false);
}
} catch (Exception e) {
response.body().close();
throw e;
}
}
return response;
} else {
return chain.proceed(chain.request());
}
}
use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project hermes by allegro.
the class OAuthAccessTokenServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
OAuthTokenRequest request = new OAuthTokenRequest(req);
validateClientCredentials(request);
String token;
if ("password".equals(request.getGrantType())) {
validateResourceOwnerCredentials(request);
token = storage.issueToken(request.getUsername());
} else {
token = storage.issueToken(request.getClientId());
}
OAuthResponse response = OAuthASResponse.tokenResponse(200).setAccessToken(token).setTokenType(TokenType.BEARER.toString()).buildJSONMessage();
sendResponse(resp, response.getBody(), response.getResponseStatus());
} catch (OAuthProblemException e) {
OAuthResponse response = getOAuthJsonErrorResponse(e, HttpServletResponse.SC_BAD_REQUEST);
resp.setHeader(Headers.CONTENT_TYPE.toString(), "application/json");
sendResponse(resp, response.getBody(), response.getResponseStatus());
} catch (OAuthSystemException e) {
sendResponse(resp, e.getMessage(), 500);
}
}
use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project hermes by allegro.
the class OAuthResourceServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
OAuthAccessResourceRequest request = new OAuthAccessResourceRequest(req);
String owner = getResourceOwner(req);
validateAccessToken(owner, request.getAccessToken());
storage.incrementResourceAccessCount(owner);
sendResponse(resp, "this is the secret of " + owner, 200);
} catch (OAuthProblemException e) {
OAuthResponse response = getOAuthJsonErrorResponse(e, HttpServletResponse.SC_UNAUTHORIZED);
resp.setHeader(Headers.CONTENT_TYPE.toString(), "application/json");
resp.setHeader(Headers.WWW_AUTHENTICATE.toString(), "Token");
sendResponse(resp, response.getBody(), response.getResponseStatus());
} catch (OAuthSystemException e) {
sendResponse(resp, e.getMessage(), 500);
}
}
use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project openhab-addons by openhab.
the class NetatmoBridgeHandler method initializeApiClient.
private void initializeApiClient() {
try {
ApiClient apiClient = new ApiClient();
OAuthClientRequest oAuthRequest = OAuthClientRequest.tokenLocation("https://api.netatmo.net/oauth2/token").setClientId(configuration.clientId).setClientSecret(configuration.clientSecret).setUsername(configuration.username).setPassword(configuration.password).setScope(getApiScope()).setGrantType(GrantType.PASSWORD).buildBodyMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse accessTokenResponse = oAuthClient.accessToken(oAuthRequest, OAuthJSONAccessTokenResponse.class);
String accessToken = accessTokenResponse.getAccessToken();
for (Authentication authentication : apiClient.getAuthentications().values()) {
if (authentication instanceof OAuth) {
((OAuth) authentication).setAccessToken(accessToken);
}
}
apiCreator = new APICreator(apiClient);
} catch (OAuthSystemException | OAuthProblemException e) {
throw new RuntimeException("Error on trying to get an access token!", e);
}
}
Aggregations