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;
}
}
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;
}
});
}
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;
}
});
}
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);
}
}
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";
}
}
Aggregations