Search in sources :

Example 76 with OAuthSystemException

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());
    }
}
Also used : OAuthBearerClientRequest(org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) Response(okhttp3.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) AuthenticationRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder) Builder(okhttp3.Request.Builder) TokenRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder) Request(okhttp3.Request) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) OAuthBearerClientRequest(org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest) IOException(java.io.IOException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) Map(java.util.Map)

Example 77 with OAuthSystemException

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());
    }
}
Also used : OAuthBearerClientRequest(org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) Response(okhttp3.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) Request(okhttp3.Request) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) OAuthBearerClientRequest(org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest) IOException(java.io.IOException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) IOException(java.io.IOException) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException)

Example 78 with OAuthSystemException

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);
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthTokenRequest(org.apache.oltu.oauth2.as.request.OAuthTokenRequest) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse)

Example 79 with OAuthSystemException

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);
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthAccessResourceRequest(org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse)

Example 80 with OAuthSystemException

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);
    }
}
Also used : OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) ApiClient(io.swagger.client.ApiClient) OAuth(io.swagger.client.auth.OAuth) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) Authentication(io.swagger.client.auth.Authentication) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Aggregations

OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)100 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)47 IOException (java.io.IOException)37 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)36 Request (okhttp3.Request)27 Response (okhttp3.Response)27 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)20 Builder (okhttp3.Request.Builder)17 OAuthBearerClientRequest (org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest)17 Map (java.util.Map)15 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)15 OAuthClientResponse (org.apache.oltu.oauth2.client.response.OAuthClientResponse)14 MediaType (okhttp3.MediaType)13 RequestBody (okhttp3.RequestBody)13 TokenRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder)12 AuthenticationRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder)11 Path (javax.ws.rs.Path)10 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)9 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)9 HashMap (java.util.HashMap)8