use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project smarthome by eclipse.
the class OAuthStoreHandlerImpl method decryptToken.
private AccessTokenResponse decryptToken(AccessTokenResponse accessTokenResponse) throws GeneralSecurityException {
AccessTokenResponse decryptedToken = (AccessTokenResponse) accessTokenResponse.clone();
if (!storageCipher.isPresent()) {
// do nothing if no cipher
return decryptedToken;
}
logger.debug("Decrypting token: {}", accessTokenResponse);
decryptedToken.setAccessToken(storageCipher.get().decrypt(accessTokenResponse.getAccessToken()));
decryptedToken.setRefreshToken(storageCipher.get().decrypt(accessTokenResponse.getRefreshToken()));
return decryptedToken;
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project smarthome by eclipse.
the class OAuthStoreHandlerImpl method saveAccessTokenResponse.
@Override
public void saveAccessTokenResponse(@NonNull String handle, @Nullable AccessTokenResponse pAccessTokenResponse) {
AccessTokenResponse accessTokenResponse = pAccessTokenResponse;
if (accessTokenResponse == null) {
// put empty
accessTokenResponse = new AccessTokenResponse();
}
AccessTokenResponse encryptedToken;
try {
encryptedToken = encryptToken(accessTokenResponse);
} catch (GeneralSecurityException e) {
logger.warn("Unable to encrypt token, storing as-is", e);
encryptedToken = accessTokenResponse;
}
storageFacade.put(handle, encryptedToken);
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-core by openhab.
the class OAuthClientServiceImpl method getAccessTokenByClientCredentials.
@Override
public AccessTokenResponse getAccessTokenByClientCredentials(@Nullable String scope) throws OAuthException, IOException, OAuthResponseException {
if (isClosed()) {
throw new OAuthException(EXCEPTION_MESSAGE_CLOSED);
}
String tokenUrl = persistedParams.tokenUrl;
if (tokenUrl == null) {
throw new OAuthException("Missing token url");
}
String clientId = persistedParams.clientId;
if (clientId == null) {
throw new OAuthException("Missing client ID");
}
OAuthConnector connector = new OAuthConnector(httpClientFactory, persistedParams.deserializerClassName);
// depending on usage, cannot guarantee every parameter is not null at the beginning
AccessTokenResponse accessTokenResponse = connector.grantTypeClientCredentials(tokenUrl, clientId, persistedParams.clientSecret, scope, Boolean.TRUE.equals(persistedParams.supportsBasicAuth));
// store it
storeHandler.saveAccessTokenResponse(handle, accessTokenResponse);
return accessTokenResponse;
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-core by openhab.
the class OAuthConnector method doRequest.
private AccessTokenResponse doRequest(final String grantType, HttpClient httpClient, final Request request, Fields fields) throws OAuthResponseException, OAuthException, IOException {
int statusCode = 0;
String content = "";
try {
final FormContentProvider entity = new FormContentProvider(fields);
final ContentResponse response = AccessController.doPrivileged((PrivilegedExceptionAction<ContentResponse>) () -> {
Request requestWithContent = request.content(entity);
return requestWithContent.send();
});
statusCode = response.getStatus();
content = response.getContentAsString();
if (statusCode == HttpStatus.OK_200) {
AccessTokenResponse jsonResponse = gson.fromJson(content, AccessTokenResponse.class);
// this is not supplied by the response
jsonResponse.setCreatedOn(LocalDateTime.now());
logger.debug("grant type {} to URL {} success", grantType, request.getURI());
return jsonResponse;
} else if (statusCode == HttpStatus.BAD_REQUEST_400) {
OAuthResponseException errorResponse = gson.fromJson(content, OAuthResponseException.class);
logger.error("grant type {} to URL {} failed with error code {}, description {}", grantType, request.getURI(), errorResponse.getError(), errorResponse.getErrorDescription());
throw errorResponse;
} else {
logger.error("grant type {} to URL {} failed with HTTP response code {}", grantType, request.getURI(), statusCode);
throw new OAuthException("Bad http response, http code " + statusCode);
}
} catch (PrivilegedActionException pae) {
Exception underlyingException = pae.getException();
if (underlyingException instanceof InterruptedException || underlyingException instanceof TimeoutException || underlyingException instanceof ExecutionException) {
throw new IOException("Exception in oauth communication, grant type " + grantType, underlyingException);
}
// Dont know what exception it is, wrap it up and throw it out
throw new OAuthException("Exception in oauth communication, grant type " + grantType, underlyingException);
} catch (JsonSyntaxException e) {
throw new OAuthException(String.format("Unable to deserialize json into AccessTokenResponse/ OAuthResponseException. httpCode: %i json: %s", statusCode, content), e);
}
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-core by openhab.
the class OAuthStoreHandlerImpl method loadAccessTokenResponse.
@Override
@Nullable
public AccessTokenResponse loadAccessTokenResponse(String handle) throws GeneralSecurityException {
AccessTokenResponse accessTokenResponseFromStore = (AccessTokenResponse) storageFacade.get(handle, ACCESS_TOKEN_RESPONSE);
if (accessTokenResponseFromStore == null) {
// token does not exist
return null;
}
AccessTokenResponse decryptedAccessToken = decryptToken(accessTokenResponseFromStore);
return decryptedAccessToken;
}
Aggregations