use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class HydrawiseAccountHandler method configure.
private void configure() {
HydrawiseAccountConfiguration config = getConfig().as(HydrawiseAccountConfiguration.class);
try {
if (!config.userName.isEmpty() && !config.password.isEmpty()) {
if (!config.savePassword) {
Configuration editedConfig = editConfiguration();
editedConfig.remove("password");
updateConfiguration(editedConfig);
}
oAuthService.getAccessTokenByResourceOwnerPasswordCredentials(config.userName, config.password, SCOPE);
} else if (oAuthService.getAccessTokenResponse() == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Login credentials required.");
return;
}
this.refresh = Math.max(config.refreshInterval, MIN_REFRESH_SECONDS);
initPolling(0, refresh);
} catch (OAuthException | IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
} catch (OAuthResponseException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Login credentials required.");
}
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class HydrawiseGraphQLClient method sendGraphQLRequest.
private String sendGraphQLRequest(String content) throws HydrawiseConnectionException, HydrawiseAuthenticationException {
logger.trace("Sending Request: {}", content);
ContentResponse response;
final AtomicInteger responseCode = new AtomicInteger(0);
final StringBuilder responseMessage = new StringBuilder();
try {
AccessTokenResponse token = oAuthService.getAccessTokenResponse();
if (token == null) {
throw new HydrawiseAuthenticationException("Login required");
}
response = httpClient.newRequest(GRAPH_URL).method(HttpMethod.POST).content(new StringContentProvider(content), "application/json").header("Authorization", token.getTokenType() + " " + token.getAccessToken()).onResponseFailure(new Response.FailureListener() {
@Override
public void onFailure(@Nullable Response response, @Nullable Throwable failure) {
int status = response != null ? response.getStatus() : -1;
String reason = response != null ? response.getReason() : "Null response";
logger.trace("onFailure code: {} message: {}", status, reason);
responseCode.set(status);
responseMessage.append(reason);
}
}).send();
String stringResponse = response.getContentAsString();
logger.trace("Received Response: {}", stringResponse);
return stringResponse;
} catch (InterruptedException | TimeoutException | OAuthException | IOException e) {
logger.debug("Could not send request", e);
throw new HydrawiseConnectionException(e);
} catch (OAuthResponseException e) {
throw new HydrawiseAuthenticationException(e.getMessage());
} catch (ExecutionException e) {
// this allows us to catch this in a callback and handle accordingly
switch(responseCode.get()) {
case 401:
case 403:
throw new HydrawiseAuthenticationException(responseMessage.toString());
default:
throw new HydrawiseConnectionException(e);
}
}
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTokenRefreshFailsDueToAnIllegalResponseThenTheListenerIsNotNotified.
@Test
public void whenTokenRefreshFailsDueToAnIllegalResponseThenTheListenerIsNotNotified() throws org.openhab.core.auth.client.oauth2.OAuthException, IOException, OAuthResponseException {
// given:
OAuthClientService oauthClientService = mock(OAuthClientService.class);
when(oauthClientService.refreshToken()).thenThrow(new OAuthResponseException());
OAuthFactory oauthFactory = mock(OAuthFactory.class);
when(oauthFactory.getOAuthClientService(MieleCloudBindingTestConstants.SERVICE_HANDLE)).thenReturn(oauthClientService);
OpenHabOAuthTokenRefresher refresher = new OpenHabOAuthTokenRefresher(oauthFactory);
OAuthTokenRefreshListener listener = mock(OAuthTokenRefreshListener.class);
refresher.setRefreshListener(listener, MieleCloudBindingTestConstants.SERVICE_HANDLE);
// when:
assertThrows(OAuthException.class, () -> {
try {
refresher.refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
} catch (OAuthException e) {
verifyNoInteractions(listener);
throw e;
}
});
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTokenIsRefreshedAndNoAccessTokenIsProvidedThenTheListenerIsNotNotified.
@Test
public void whenTokenIsRefreshedAndNoAccessTokenIsProvidedThenTheListenerIsNotNotified() throws org.openhab.core.auth.client.oauth2.OAuthException, IOException, OAuthResponseException {
// given:
AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
OAuthClientService oauthClientService = mock(OAuthClientService.class);
OAuthFactory oauthFactory = mock(OAuthFactory.class);
when(oauthFactory.getOAuthClientService(MieleCloudBindingTestConstants.SERVICE_HANDLE)).thenReturn(oauthClientService);
OpenHabOAuthTokenRefresher refresher = new OpenHabOAuthTokenRefresher(oauthFactory);
when(oauthClientService.refreshToken()).thenAnswer(new Answer<@Nullable AccessTokenResponse>() {
@Override
@Nullable
public AccessTokenResponse answer(@Nullable InvocationOnMock invocation) throws Throwable {
getAccessTokenRefreshListenerByServiceHandle(refresher, MieleCloudBindingTestConstants.SERVICE_HANDLE).onAccessTokenResponse(accessTokenResponse);
return accessTokenResponse;
}
});
OAuthTokenRefreshListener listener = mock(OAuthTokenRefreshListener.class);
refresher.setRefreshListener(listener, MieleCloudBindingTestConstants.SERVICE_HANDLE);
// when:
assertThrows(OAuthException.class, () -> {
try {
refresher.refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
} catch (OAuthException e) {
verifyNoInteractions(listener);
throw e;
}
});
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTokenRefreshFailsWithOAuthExceptionThenTheListenerIsNotNotified.
@Test
public void whenTokenRefreshFailsWithOAuthExceptionThenTheListenerIsNotNotified() throws org.openhab.core.auth.client.oauth2.OAuthException, IOException, OAuthResponseException {
// given:
OAuthClientService oauthClientService = mock(OAuthClientService.class);
when(oauthClientService.refreshToken()).thenThrow(new org.openhab.core.auth.client.oauth2.OAuthException());
OAuthFactory oauthFactory = mock(OAuthFactory.class);
when(oauthFactory.getOAuthClientService(MieleCloudBindingTestConstants.SERVICE_HANDLE)).thenReturn(oauthClientService);
OpenHabOAuthTokenRefresher refresher = new OpenHabOAuthTokenRefresher(oauthFactory);
OAuthTokenRefreshListener listener = mock(OAuthTokenRefreshListener.class);
refresher.setRefreshListener(listener, MieleCloudBindingTestConstants.SERVICE_HANDLE);
// when:
assertThrows(OAuthException.class, () -> {
try {
refresher.refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
} catch (OAuthException e) {
verifyNoInteractions(listener);
throw e;
}
});
}
Aggregations