Search in sources :

Example 41 with AccessTokenResponse

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;
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) TimeoutException(java.util.concurrent.TimeoutException)

Example 42 with AccessTokenResponse

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;
}
Also used : Properties(java.util.Properties) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse)

Example 43 with AccessTokenResponse

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);
}
Also used : TokenResponseDTO(org.openhab.binding.ecobee.internal.dto.oauth.TokenResponseDTO) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse)

Example 44 with AccessTokenResponse

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);
}
Also used : AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) OpenHabOsgiTest(org.openhab.binding.mielecloud.internal.util.OpenHabOsgiTest) Test(org.junit.jupiter.api.Test)

Example 45 with AccessTokenResponse

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);
}
Also used : AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) OpenHabOsgiTest(org.openhab.binding.mielecloud.internal.util.OpenHabOsgiTest) Test(org.junit.jupiter.api.Test)

Aggregations

AccessTokenResponse (org.openhab.core.auth.client.oauth2.AccessTokenResponse)36 OAuthException (org.openhab.core.auth.client.oauth2.OAuthException)17 IOException (java.io.IOException)15 AccessTokenResponse (org.eclipse.smarthome.core.auth.client.oauth2.AccessTokenResponse)12 OAuthResponseException (org.openhab.core.auth.client.oauth2.OAuthResponseException)12 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)8 OAuthClientService (org.openhab.core.auth.client.oauth2.OAuthClientService)8 Nullable (org.eclipse.jdt.annotation.Nullable)7 ExecutionException (java.util.concurrent.ExecutionException)6 OAuthException (org.eclipse.smarthome.core.auth.client.oauth2.OAuthException)6 TimeoutException (java.util.concurrent.TimeoutException)5 Request (org.eclipse.jetty.client.api.Request)5 OAuthFactory (org.openhab.core.auth.client.oauth2.OAuthFactory)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 GeneralSecurityException (java.security.GeneralSecurityException)4 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)3 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)3 Test (org.junit.jupiter.api.Test)3 PrivilegedActionException (java.security.PrivilegedActionException)2 Collections (java.util.Collections)2