Search in sources :

Example 36 with OAuthClientRequest

use of org.apache.amber.oauth2.client.request.OAuthClientRequest in project structr by structr.

the class StructrOAuthClient method getAccessTokenResponse.

private OAuthAccessTokenResponse getAccessTokenResponse(final HttpServletRequest request) {
    if (tokenResponse != null) {
        return tokenResponse;
    }
    try {
        String code = getCode(request);
        if (code == null) {
            logger.error("Could not get code from request, cancelling authorization process");
            return null;
        }
        OAuthClientRequest clientReq = OAuthClientRequest.tokenLocation(tokenLocation).setGrantType(getGrantType()).setClientId(clientId).setClientSecret(clientSecret).setRedirectURI(getAbsoluteUrl(request, redirectUri)).setCode(getCode(request)).buildBodyMessage();
        logger.info("Request body: {}", clientReq.getBody());
        OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
        tokenResponse = oAuthClient.accessToken(clientReq, tokenResponseClass);
        logger.info("Access token response: {}", tokenResponse.getBody());
        return tokenResponse;
    } catch (Throwable t) {
        logger.error("Could not get access token response", t);
    }
    return null;
}
Also used : URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 37 with OAuthClientRequest

use of org.apache.amber.oauth2.client.request.OAuthClientRequest in project dq-easy-cloud by dq-open-cloud.

the class EcBaseOauthToken method doGetOauthResource.

/**
 * <p>
 * 执行获取授权资源信息
 * </p>
 * <pre>
 *     子类可以通过重写该方法实现自己的获取授权资源数据传输对象的数据
 * </pre>
 *
 * @param oAuthResponseParam
 * @return com.easy.cloud.core.oauth.client.base.pojo.dto.EcBaseOauthResourceDTO
 * @author daiqi
 * @date 2018/7/18 11:54
 */
public EcBaseOauthResourceDTO doGetOauthResource(Map<String, Object> oAuthResponseParam) throws Exception {
    // 获取资源请求构建者
    EcBaseResourceRequestBuilder resourceRequestBuilder = getResourceRequestBuilder();
    // 构建客户端请求参数
    OAuthClientRequest resourceRequest = resourceRequestBuilder.buildClientRequest(oAuthResponseParam);
    // 执行请求资源
    EcBaseOauthResourceResponse resourceResponse = oAuthClient.resource(resourceRequest, OAuth.HttpMethod.GET, resourceRequestBuilder.getResourceResponseClass());
    // 从资源响应对象中获取资源数据传输对象
    EcBaseOauthResourceDTO resourceDTO = resourceResponse.getResourceObj(resourceResponse.getResourceDTOClass());
    EcLogUtils.info("授权获取到的用户信息", resourceDTO, logger);
    return resourceDTO;
}
Also used : EcBaseOauthResourceResponse(com.easy.cloud.core.oauth.client.base.response.token.EcBaseOauthResourceResponse) EcBaseResourceRequestBuilder(com.easy.cloud.core.oauth.client.base.request.builder.EcBaseResourceRequestBuilder) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) EcBaseOauthResourceDTO(com.easy.cloud.core.oauth.client.base.pojo.dto.EcBaseOauthResourceDTO)

Example 38 with OAuthClientRequest

use of org.apache.amber.oauth2.client.request.OAuthClientRequest in project android-client by GenesisVision.

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)

Aggregations

OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)36 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)24 IOException (java.io.IOException)21 Request (okhttp3.Request)18 Response (okhttp3.Response)18 Builder (okhttp3.Request.Builder)17 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)13 OAuthBearerClientRequest (org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest)11 AuthenticationRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder)10 TokenRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder)10 Map (java.util.Map)9 MediaType (okhttp3.MediaType)9 RequestBody (okhttp3.RequestBody)9 OAuthClientResponse (org.apache.oltu.oauth2.client.response.OAuthClientResponse)9 URI (java.net.URI)6 URLConnectionClient (org.apache.oltu.oauth2.client.URLConnectionClient)6 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)5 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)5 OAuthRegistrationClient (org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient)3 OAuthClientRegistrationResponse (org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse)3