Search in sources :

Example 1 with OAuthException

use of org.openhab.binding.mielecloud.internal.auth.OAuthException in project openhab-addons by openhab.

the class OAuthAuthorizationHandlerImpl method completeAuthorization.

@Override
public synchronized void completeAuthorization(String redirectUrlWithParameters) {
    abortTimer();
    final OAuthClientService oauthClientService = this.oauthClientService;
    if (oauthClientService == null) {
        throw new NoOngoingAuthorizationException("There is no ongoing authorization.");
    }
    try {
        String authorizationCode = oauthClientService.extractAuthCodeFromAuthResponse(redirectUrlWithParameters);
        // Although this method is called "get" it actually fetches and stores the token response as a side effect.
        oauthClientService.getAccessTokenResponseByAuthorizationCode(authorizationCode, redirectUri);
    } catch (IOException e) {
        throw new OAuthException("Network error while retrieving token response: " + e.getMessage(), e);
    } catch (OAuthResponseException e) {
        throw new OAuthException("Failed to retrieve token response: " + e.getMessage(), e);
    } catch (org.openhab.core.auth.client.oauth2.OAuthException e) {
        throw new OAuthException("Error while processing Miele service response: " + e.getMessage(), e);
    } finally {
        this.oauthClientService = null;
        this.bridgeUid = null;
        this.email = null;
        this.redirectUri = null;
    }
}
Also used : OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthException(org.openhab.binding.mielecloud.internal.auth.OAuthException) IOException(java.io.IOException) NoOngoingAuthorizationException(org.openhab.binding.mielecloud.internal.config.exception.NoOngoingAuthorizationException)

Example 2 with OAuthException

use of org.openhab.binding.mielecloud.internal.auth.OAuthException in project openhab-addons by openhab.

the class OAuthAuthorizationHandlerImplTest method whenRetrievingTheAccessTokenFailsDueToAnIllegalAnswerFromTheMieleServiceThenAnOAuthExceptionIsThrownAndAllResourcesAreCleanedUp.

@Test
public void whenRetrievingTheAccessTokenFailsDueToAnIllegalAnswerFromTheMieleServiceThenAnOAuthExceptionIsThrownAndAllResourcesAreCleanedUp() throws Exception {
    // given:
    when(getClientService().extractAuthCodeFromAuthResponse(anyString())).thenReturn(AUTH_CODE);
    when(getClientService().getAccessTokenResponseByAuthorizationCode(anyString(), anyString())).thenThrow(new OAuthResponseException());
    getAuthorizationHandler().beginAuthorization(CLIENT_ID, CLIENT_SECRET, BRIDGE_UID, EMAIL);
    getAuthorizationHandler().getAuthorizationUrl(REDIRECT_URL);
    // when:
    assertThrows(OAuthException.class, () -> {
        try {
            getAuthorizationHandler().completeAuthorization("http://127.0.0.1:8080/mielecloud/result?code=abc&state=def");
        } catch (OAuthException e) {
            assertNull(getPrivate(getAuthorizationHandler(), "timer"));
            assertNull(getPrivate(getAuthorizationHandler(), "oauthClientService"));
            assertNull(getPrivate(getAuthorizationHandler(), "bridgeUid"));
            assertNull(getPrivate(getAuthorizationHandler(), "email"));
            assertNull(getPrivate(getAuthorizationHandler(), "redirectUri"));
            throw e;
        }
    });
}
Also used : OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) OAuthException(org.openhab.binding.mielecloud.internal.auth.OAuthException) Test(org.junit.jupiter.api.Test)

Example 3 with OAuthException

use of org.openhab.binding.mielecloud.internal.auth.OAuthException in project openhab-addons by openhab.

the class OAuthAuthorizationHandlerImplTest method whenRetrievingTheAccessTokenFailsDueToANetworkErrorThenAnOAuthExceptionIsThrownAndAllResourcesAreCleanedUp.

@Test
public void whenRetrievingTheAccessTokenFailsDueToANetworkErrorThenAnOAuthExceptionIsThrownAndAllResourcesAreCleanedUp() throws Exception {
    // given:
    when(getClientService().extractAuthCodeFromAuthResponse(anyString())).thenReturn(AUTH_CODE);
    when(getClientService().getAccessTokenResponseByAuthorizationCode(anyString(), anyString())).thenThrow(new IOException());
    getAuthorizationHandler().beginAuthorization(CLIENT_ID, CLIENT_SECRET, BRIDGE_UID, EMAIL);
    getAuthorizationHandler().getAuthorizationUrl(REDIRECT_URL);
    // when:
    assertThrows(OAuthException.class, () -> {
        try {
            getAuthorizationHandler().completeAuthorization("http://127.0.0.1:8080/mielecloud/result?code=abc&state=def");
        } catch (OAuthException e) {
            assertNull(getPrivate(getAuthorizationHandler(), "timer"));
            assertNull(getPrivate(getAuthorizationHandler(), "oauthClientService"));
            assertNull(getPrivate(getAuthorizationHandler(), "bridgeUid"));
            assertNull(getPrivate(getAuthorizationHandler(), "email"));
            assertNull(getPrivate(getAuthorizationHandler(), "redirectUri"));
            throw e;
        }
    });
}
Also used : OAuthException(org.openhab.binding.mielecloud.internal.auth.OAuthException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 4 with OAuthException

use of org.openhab.binding.mielecloud.internal.auth.OAuthException in project openhab-addons by openhab.

the class OAuthAuthorizationHandlerImpl method getAuthorizationUrl.

@Override
public synchronized String getAuthorizationUrl(String redirectUri) {
    final OAuthClientService oauthClientService = this.oauthClientService;
    if (oauthClientService == null) {
        throw new NoOngoingAuthorizationException("There is no ongoing authorization!");
    }
    this.redirectUri = redirectUri;
    try {
        timer = scheduler.schedule(this::cancelAuthorization, AUTHORIZATION_TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
        timerExpiryTimestamp = LocalDateTime.now().plusMinutes(AUTHORIZATION_TIMEOUT_IN_MINUTES);
        return oauthClientService.getAuthorizationUrl(redirectUri, null, null);
    } catch (org.openhab.core.auth.client.oauth2.OAuthException e) {
        abortTimer();
        cancelAuthorization();
        throw new OAuthException("Failed to determine authorization URL: " + e.getMessage(), e);
    }
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthException(org.openhab.binding.mielecloud.internal.auth.OAuthException) NoOngoingAuthorizationException(org.openhab.binding.mielecloud.internal.config.exception.NoOngoingAuthorizationException)

Example 5 with OAuthException

use of org.openhab.binding.mielecloud.internal.auth.OAuthException in project openhab-addons by openhab.

the class ResultServlet method getRedirectionDestination.

@Override
protected String getRedirectionDestination(HttpServletRequest request) {
    String error = request.getParameter(ERROR_PARAMETER_NAME);
    if (error != null) {
        logger.warn("Received error response: {}", error);
        return "/mielecloud/failure?" + FailureServlet.OAUTH2_ERROR_PARAMETER_NAME + "=" + error;
    }
    String code = request.getParameter(CODE_PARAMETER_NAME);
    if (code == null) {
        logger.warn("Code is null");
        return "/mielecloud/failure?" + FailureServlet.ILLEGAL_RESPONSE_PARAMETER_NAME + "=true";
    }
    String state = request.getParameter(STATE_PARAMETER_NAME);
    if (state == null) {
        logger.warn("State is null");
        return "/mielecloud/failure?" + FailureServlet.ILLEGAL_RESPONSE_PARAMETER_NAME + "=true";
    }
    try {
        ThingUID bridgeId = authorizationHandler.getBridgeUid();
        String email = authorizationHandler.getEmail();
        StringBuffer requestUrl = request.getRequestURL();
        if (requestUrl == null) {
            return "/mielecloud/failure?" + FailureServlet.MISSING_REQUEST_URL_PARAMETER_NAME + "=true";
        }
        try {
            authorizationHandler.completeAuthorization(requestUrl.toString() + "?" + request.getQueryString());
        } catch (OAuthException e) {
            logger.warn("Failed to complete authorization.", e);
            return "/mielecloud/failure?" + FailureServlet.FAILED_TO_COMPLETE_AUTHORIZATION_PARAMETER_NAME + "=true";
        }
        return "/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME + "=" + bridgeId.getAsString() + "&" + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + email;
    } catch (NoOngoingAuthorizationException e) {
        logger.warn("Failed to complete authorization: There is no ongoing authorization or it timed out");
        return "/mielecloud/failure?" + FailureServlet.NO_ONGOING_AUTHORIZATION_PARAMETER_NAME + "=true";
    }
}
Also used : ThingUID(org.openhab.core.thing.ThingUID) OAuthException(org.openhab.binding.mielecloud.internal.auth.OAuthException) NoOngoingAuthorizationException(org.openhab.binding.mielecloud.internal.config.exception.NoOngoingAuthorizationException)

Aggregations

OAuthException (org.openhab.binding.mielecloud.internal.auth.OAuthException)10 Test (org.junit.jupiter.api.Test)4 NoOngoingAuthorizationException (org.openhab.binding.mielecloud.internal.config.exception.NoOngoingAuthorizationException)4 IOException (java.io.IOException)2 OAuthAuthorizationHandler (org.openhab.binding.mielecloud.internal.config.OAuthAuthorizationHandler)2 AbstractConfigFlowTest (org.openhab.binding.mielecloud.internal.util.AbstractConfigFlowTest)2 Website (org.openhab.binding.mielecloud.internal.util.Website)2 OAuthClientService (org.openhab.core.auth.client.oauth2.OAuthClientService)2 OAuthResponseException (org.openhab.core.auth.client.oauth2.OAuthResponseException)2 ThingUID (org.openhab.core.thing.ThingUID)2 OngoingAuthorizationException (org.openhab.binding.mielecloud.internal.config.exception.OngoingAuthorizationException)1 MieleWebservice (org.openhab.binding.mielecloud.internal.webservice.MieleWebservice)1 UnavailableMieleWebservice (org.openhab.binding.mielecloud.internal.webservice.UnavailableMieleWebservice)1 MieleWebserviceException (org.openhab.binding.mielecloud.internal.webservice.exception.MieleWebserviceException)1 MieleWebserviceInitializationException (org.openhab.binding.mielecloud.internal.webservice.exception.MieleWebserviceInitializationException)1