Search in sources :

Example 21 with OAuthProblemException

use of org.apache.oltu.oauth2.common.exception.OAuthProblemException in project BIMserver by opensourceBIM.

the class URLConnectionClient method execute.

public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers, String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {
    InputStream responseBody = null;
    URLConnection c;
    Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
    int responseCode;
    try {
        URL url = new URL(request.getLocationUri());
        c = url.openConnection();
        responseCode = -1;
        if (c instanceof HttpURLConnection) {
            HttpURLConnection httpURLConnection = (HttpURLConnection) c;
            if (headers != null && !headers.isEmpty()) {
                for (Map.Entry<String, String> header : headers.entrySet()) {
                    httpURLConnection.addRequestProperty(header.getKey(), header.getValue());
                }
            }
            if (request.getHeaders() != null) {
                for (Map.Entry<String, String> header : request.getHeaders().entrySet()) {
                    httpURLConnection.addRequestProperty(header.getKey(), header.getValue());
                }
            }
            if (OAuthUtils.isEmpty(requestMethod)) {
                httpURLConnection.setRequestMethod(OAuth.HttpMethod.GET);
            } else {
                httpURLConnection.setRequestMethod(requestMethod);
                setRequestBody(request, requestMethod, httpURLConnection);
            }
            httpURLConnection.connect();
            InputStream inputStream;
            responseCode = httpURLConnection.getResponseCode();
            if (responseCode == SC_BAD_REQUEST || responseCode == SC_UNAUTHORIZED) {
                inputStream = httpURLConnection.getErrorStream();
            } else {
                inputStream = httpURLConnection.getInputStream();
            }
            responseHeaders = httpURLConnection.getHeaderFields();
            responseBody = inputStream;
        }
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
    return OAuthClientResponseFactory.createCustomResponse(responseBody, c.getContentType(), responseCode, responseHeaders, responseClass);
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with OAuthProblemException

use of org.apache.oltu.oauth2.common.exception.OAuthProblemException in project tesla by linking12.

the class OauthTokenController method authorize.

@RequestMapping("token")
public void authorize(HttpServletRequest request, HttpServletResponse response) throws OAuthSystemException {
    try {
        OAuthTokenxRequest tokenRequest = new OAuthTokenxRequest(request);
        OAuthTokenHandleDispatcher tokenHandleDispatcher = new OAuthTokenHandleDispatcher(tokenRequest, response);
        tokenHandleDispatcher.dispatch();
    } catch (OAuthProblemException e) {
        LOG.debug(e.getMessage(), e);
        OAuthResponse oAuthResponse = OAuthASResponse.errorResponse(HttpServletResponse.SC_FOUND).location(e.getRedirectUri()).error(e).buildJSONMessage();
        WebUtils.writeOAuthJsonResponse(response, oAuthResponse);
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthTokenxRequest(io.github.tesla.authz.controller.oauth2.OAuthTokenxRequest) OAuthTokenHandleDispatcher(io.github.tesla.authz.controller.oauth2.token.OAuthTokenHandleDispatcher) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with OAuthProblemException

use of org.apache.oltu.oauth2.common.exception.OAuthProblemException in project tesla by linking12.

the class ClientCredentialsTokenHandler method handleAfterValidation.

@Override
public void handleAfterValidation() throws OAuthProblemException, OAuthSystemException {
    AccessToken accessToken = oauthService.retrieveClientCredentialsAccessToken(clientDetails(), tokenRequest.getScopes());
    final OAuthResponse tokenResponse = createTokenResponse(accessToken, false);
    LOG.debug("'client_credentials' response: {}", tokenResponse);
    WebUtils.writeOAuthJsonResponse(response, tokenResponse);
}
Also used : AccessToken(io.github.tesla.authz.domain.AccessToken) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse)

Example 24 with OAuthProblemException

use of org.apache.oltu.oauth2.common.exception.OAuthProblemException in project tesla by linking12.

the class PasswordTokenHandler method handleAfterValidation.

@Override
public void handleAfterValidation() throws OAuthProblemException, OAuthSystemException {
    AccessToken accessToken = oauthService.retrievePasswordAccessToken(clientDetails(), tokenRequest.getScopes(), tokenRequest.getUsername());
    final OAuthResponse tokenResponse = createTokenResponse(accessToken, false);
    LOG.debug("'password' response: {}", tokenResponse);
    WebUtils.writeOAuthJsonResponse(response, tokenResponse);
}
Also used : AccessToken(io.github.tesla.authz.domain.AccessToken) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse)

Example 25 with OAuthProblemException

use of org.apache.oltu.oauth2.common.exception.OAuthProblemException in project components by Talend.

the class Oauth2ImplicitClient method getToken.

public <T extends OAuthAccessTokenResponse> T getToken(Class<T> tokenResponseClass) {
    try {
        TokenRequestBuilder builder = // 
        OAuthClientRequest.tokenLocation(// 
        tokenLocation.toString()).setGrantType(// 
        grantType).setClientId(// 
        clientID).setClientSecret(clientSecret);
        if (GrantType.AUTHORIZATION_CODE == grantType) {
            builder = // 
            builder.setRedirectURI(callbackURL.toString()).setCode(getAuthorizationCode());
        } else if (GrantType.REFRESH_TOKEN == grantType) {
            builder = builder.setRefreshToken(refreshToken);
        }
        OAuthClientRequest request = builder.buildQueryMessage();
        OAuthClient oauthClient = new OAuthClient(new URLConnectionClient());
        return oauthClient.accessToken(request, tokenResponseClass);
    } catch (OAuthSystemException e) {
        throw new RuntimeException(e);
    } catch (OAuthProblemException e) {
        throw new RuntimeException(e);
    }
}
Also used : TokenRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Aggregations

OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)24 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)20 IOException (java.io.IOException)15 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)15 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)12 MediaType (okhttp3.MediaType)9 Request (okhttp3.Request)9 RequestBody (okhttp3.RequestBody)9 Response (okhttp3.Response)9 OAuthClientResponse (org.apache.oltu.oauth2.client.response.OAuthClientResponse)9 Builder (okhttp3.Request.Builder)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 URI (java.net.URI)6 MD5Generator (org.apache.oltu.oauth2.as.issuer.MD5Generator)5 OAuthAccessResourceRequest (org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest)5 OAuthIssuerImpl (org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl)4 OAuthAuthzResponse (org.apache.oltu.oauth2.client.response.OAuthAuthzResponse)4 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)4 AccessToken (io.github.tesla.authz.domain.AccessToken)3 ServletException (javax.servlet.ServletException)3