use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-addons by openhab.
the class InnogyClient method request.
private ContentResponse request(final Request request) throws IOException, AuthenticationException, ApiException {
final ContentResponse response;
try {
final AccessTokenResponse accessTokenResponse = getAccessTokenResponse();
response = request.header(HttpHeader.ACCEPT, CONTENT_TYPE).header(HttpHeader.AUTHORIZATION, BEARER + accessTokenResponse.getAccessToken()).idleTimeout(HTTP_REQUEST_IDLE_TIMEOUT_SECONDS, TimeUnit.SECONDS).timeout(HTTP_REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS).send();
} catch (InterruptedException | TimeoutException | ExecutionException e) {
throw new IOException(e);
}
handleResponseErrors(response, request.getURI());
return response;
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-addons by openhab.
the class EcobeeApi method setHeaders.
private Properties setHeaders() throws EcobeeAuthException {
AccessTokenResponse atr = accessTokenResponse;
if (atr == null) {
throw new EcobeeAuthException("Can not set auth header because access token is null");
}
Properties headers = new Properties();
headers.putAll(HTTP_HEADERS);
headers.put("Authorization", "Bearer " + atr.getAccessToken());
return headers;
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-addons by openhab.
the class EcobeeAuth method getTokens.
/**
* Call the Ecobee token endpoint to get the access and refresh tokens. Once successfully retrieved,
* the access and refresh tokens will be injected into the OHC OAuth service.
* Warnings are suppressed to avoid the Gson.fromJson warnings.
*/
@SuppressWarnings({ "null", "unused" })
private void getTokens() throws EcobeeAuthException {
logger.debug("EcobeeAuth: State is {}: Executing step: 'getToken'", state);
StringBuilder url = new StringBuilder(ECOBEE_TOKEN_URL);
url.append("?grant_type=ecobeePin");
url.append("&code=").append(code);
url.append("&client_id=").append(apiKey);
logger.trace("EcobeeAuth: Posting token URL={}", url);
String response = executeUrl("POST", url.toString());
logger.trace("EcobeeAuth: Got a valid token response: {}", response);
TokenResponseDTO tokenResponse = EcobeeApi.getGson().fromJson(response, TokenResponseDTO.class);
if (tokenResponse == null) {
logger.debug("EcobeeAuth: Got null token response from Ecobee API");
updateBridgeStatus();
setState(isPinExpired() ? EcobeeAuthState.NEED_PIN : EcobeeAuthState.NEED_TOKEN);
return;
}
String error = tokenResponse.error;
if (error != null && !error.isEmpty()) {
throw new EcobeeAuthException(error + ": " + tokenResponse.errorDescription);
}
AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
accessTokenResponse.setRefreshToken(tokenResponse.refreshToken);
accessTokenResponse.setAccessToken(tokenResponse.accessToken);
accessTokenResponse.setScope(tokenResponse.scope);
accessTokenResponse.setTokenType(tokenResponse.tokenType);
accessTokenResponse.setExpiresIn(tokenResponse.expiresIn);
try {
logger.debug("EcobeeAuth: Importing AccessTokenResponse into oAuthClientService!!!");
oAuthClientService.importAccessTokenResponse(accessTokenResponse);
bridgeHandler.updateBridgeStatus(ThingStatus.ONLINE);
setState(EcobeeAuthState.COMPLETE);
return;
} catch (OAuthException e) {
logger.info("EcobeeAuth: Got OAuthException", e);
// No other processing needed here
}
updateBridgeStatus();
setState(isPinExpired() ? EcobeeAuthState.NEED_PIN : EcobeeAuthState.NEED_TOKEN);
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-addons by openhab.
the class MieleBridgeHandlerTest method whenAnAuthorizationFailedErrorIsReportedThenTheAccessTokenIsRefreshedAndTheSseConnectionRestored.
@Test
public void whenAnAuthorizationFailedErrorIsReportedThenTheAccessTokenIsRefreshedAndTheSseConnectionRestored() throws Exception {
// given:
AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
accessTokenResponse.setAccessToken(ACCESS_TOKEN);
when(getOAuthClientServiceMock().refreshToken()).thenReturn(accessTokenResponse);
initializeBridgeWithTokens();
getHandler().onConnectionAlive();
// when:
getHandler().onConnectionError(ConnectionError.AUTHORIZATION_FAILED, 0);
// then:
verify(getOAuthClientServiceMock()).refreshToken();
verify(getWebserviceMock()).connectSse();
assertThingStatusIs(ThingStatus.ONLINE);
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-addons by openhab.
the class MieleBridgeHandlerTest method whenAnAuthorizationFailedErrorIsReportedAndTokenRefreshFailsThenSseConnectionIsTerminatedAndTheStatusSetToOfflineWithDetailConfigurationError.
@Test
public void whenAnAuthorizationFailedErrorIsReportedAndTokenRefreshFailsThenSseConnectionIsTerminatedAndTheStatusSetToOfflineWithDetailConfigurationError() throws Exception {
// given:
when(getOAuthClientServiceMock().refreshToken()).thenReturn(new AccessTokenResponse());
initializeBridgeWithTokens();
getHandler().onConnectionAlive();
// when:
getHandler().onConnectionError(ConnectionError.AUTHORIZATION_FAILED, 0);
// then:
verify(getOAuthClientServiceMock()).refreshToken();
verify(getWebserviceMock()).disconnectSse();
assertThingStatusIs(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, I18NKeys.BRIDGE_STATUS_DESCRIPTION_ACCESS_TOKEN_REFRESH_FAILED);
}
Aggregations