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;
}
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;
}
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());
}
}
Aggregations