use of org.apache.oltu.oauth2.client.request.OAuthClientRequest 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);
}
use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project BIMserver by opensourceBIM.
the class OAuthServiceImpl method generateForwardUrl.
public String generateForwardUrl(String registrationEndpoint, String authorizeUrl, String returnUrl) throws ServerException, UserException {
try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_RegistrationEndpoint(), registrationEndpoint);
if (oAuthServer == null) {
throw new UserException("Application not registered");
}
OAuthClientRequest request2 = OAuthClientRequest.authorizationLocation(authorizeUrl).setParameter("auth_type", "service").setClientId(oAuthServer.getClientId()).setRedirectURI(returnUrl).setResponseType(ResponseType.CODE.toString()).setState("state").buildQueryMessage();
return request2.getLocationUri();
} catch (Exception e) {
return handleException(e);
}
}
use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project incubator-gobblin by apache.
the class SalesforceRestWriter method onConnect.
/**
* Retrieve access token, if needed, retrieve instance url, and set server host URL
* {@inheritDoc}
* @see org.apache.gobblin.writer.http.HttpWriter#onConnect(org.apache.http.HttpHost)
*/
@Override
public void onConnect(URI serverHost) throws IOException {
if (!StringUtils.isEmpty(accessToken)) {
// No need to be called if accessToken is active.
return;
}
try {
getLog().info("Getting Oauth2 access token.");
OAuthClientRequest request = OAuthClientRequest.tokenLocation(serverHost.toString()).setGrantType(GrantType.PASSWORD).setClientId(clientId).setClientSecret(clientSecret).setUsername(userId).setPassword(password + securityToken).buildQueryMessage();
OAuthClient client = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse response = client.accessToken(request, OAuth.HttpMethod.POST);
accessToken = response.getAccessToken();
setCurServerHost(new URI(response.getParam("instance_url")));
} catch (OAuthProblemException e) {
throw new NonTransientException("Error while authenticating with Oauth2", e);
} catch (OAuthSystemException e) {
throw new RuntimeException("Failed getting access token", e);
} catch (URISyntaxException e) {
throw new RuntimeException("Failed due to invalid instance url", e);
}
}
use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project android-client by GenesisVision.
the class OAuthOkHttpClient method execute.
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers, String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {
MediaType mediaType = MediaType.parse("application/json");
Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
mediaType = MediaType.parse(entry.getValue());
} else {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
}
RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
requestBuilder.method(requestMethod, body);
try {
Response response = client.newCall(requestBuilder.build()).execute();
return OAuthClientResponseFactory.createCustomResponse(response.body().string(), response.body().contentType().toString(), response.code(), responseClass);
} catch (IOException e) {
throw new OAuthSystemException(e);
}
}
use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project airavata by apache.
the class AuthResponse method authenticate.
public static AuthResponse authenticate(String username, String password) throws AuthenticationException {
try {
OAuthClientRequest request = OAuthClientRequest.tokenLocation(hostName + "/oauth2/token").setClientId(clientId).setClientSecret(clientSecret).setGrantType(GrantType.PASSWORD).setRedirectURI("").setUsername(username).setPassword(password).setScope("openid").buildBodyMessage();
URLConnectionClient ucc = new URLConnectionClient();
org.apache.oltu.oauth2.client.OAuthClient oAuthClient = new org.apache.oltu.oauth2.client.OAuthClient(ucc);
OAuthResourceResponse resp = oAuthClient.resource(request, OAuth.HttpMethod.POST, OAuthResourceResponse.class);
// converting JSON to object
ObjectMapper mapper = new ObjectMapper();
AuthResponse authResponse;
try {
authResponse = mapper.readValue(resp.getBody(), AuthResponse.class);
} catch (Exception e) {
return null;
}
String accessToken = authResponse.getAccess_token();
if (accessToken != null && !accessToken.isEmpty()) {
request = new OAuthBearerClientRequest(hostName + "/oauth2/userinfo?schema=openid").buildQueryMessage();
ucc = new URLConnectionClient();
request.setHeader("Authorization", "Bearer " + accessToken);
oAuthClient = new org.apache.oltu.oauth2.client.OAuthClient(ucc);
resp = oAuthClient.resource(request, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
Map<String, String> profile = mapper.readValue(resp.getBody(), Map.class);
return authResponse;
}
} catch (Exception ex) {
throw new AuthenticationException(ex.getMessage());
}
return null;
}
Aggregations