use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTokensAreRemovedThenTheRuntimeIsRequestedToDeleteServiceAndAccessToken.
@Test
public void whenTokensAreRemovedThenTheRuntimeIsRequestedToDeleteServiceAndAccessToken() throws org.openhab.core.auth.client.oauth2.OAuthException, IOException, OAuthResponseException {
// given:
OAuthClientService oauthClientService = mock(OAuthClientService.class);
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:
refresher.removeTokensFromStorage(MieleCloudBindingTestConstants.SERVICE_HANDLE);
// then:
verify(oauthFactory).deleteServiceAndAccessToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresherTest method whenTokenIsRefreshedThenTheListenerIsCalledWithTheNewAccessToken.
@Test
public void whenTokenIsRefreshedThenTheListenerIsCalledWithTheNewAccessToken() throws org.openhab.core.auth.client.oauth2.OAuthException, IOException, OAuthResponseException {
// given:
AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
accessTokenResponse.setAccessToken(ACCESS_TOKEN);
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:
refresher.refreshToken(MieleCloudBindingTestConstants.SERVICE_HANDLE);
// then:
verify(listener).onNewAccessToken(ACCESS_TOKEN);
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException 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.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class OpenHabOAuthTokenRefresher method refreshAccessToken.
private void refreshAccessToken(OAuthClientService clientService) {
try {
final AccessTokenResponse accessTokenResponse = clientService.refreshToken();
final String accessToken = accessTokenResponse.getAccessToken();
if (accessToken == null) {
throw new OAuthException("Access token is not available.");
}
} catch (org.openhab.core.auth.client.oauth2.OAuthException e) {
throw new OAuthException("An error occured during token refresh: " + e.getMessage(), e);
} catch (IOException e) {
throw new OAuthException("A network error occured during token refresh: " + e.getMessage(), e);
} catch (OAuthResponseException e) {
throw new OAuthException("Miele cloud service returned an illegal response: " + e.getMessage(), e);
}
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class SpotifyApi method request.
/**
* Calls the Spotify Web Api with the given method and given url as parameters of the call to Spotify.
*
* @param method Http method to perform
* @param url url path to call to Spotify
* @param requestData data to pass along with the call as content
* @param clazz data type of return data, if null no data is expected to be returned.
* @return the response give by Spotify
*/
@Nullable
private <T> T request(HttpMethod method, String url, String requestData, Class<T> clazz) {
logger.debug("Request: ({}) {} - {}", method, url, requestData);
final Function<HttpClient, Request> call = httpClient -> httpClient.newRequest(url).method(method).header("Accept", CONTENT_TYPE).content(new StringContentProvider(requestData), CONTENT_TYPE);
try {
final AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse();
final String accessToken = accessTokenResponse == null ? null : accessTokenResponse.getAccessToken();
if (accessToken == null || accessToken.isEmpty()) {
throw new SpotifyAuthorizationException("No Spotify accesstoken. Did you authorize Spotify via /connectspotify ?");
} else {
final String response = requestWithRetry(call, accessToken).getContentAsString();
return clazz == String.class ? (@Nullable T) response : fromJson(response, clazz);
}
} catch (final IOException e) {
throw new SpotifyException(e.getMessage(), e);
} catch (OAuthException | OAuthResponseException e) {
throw new SpotifyAuthorizationException(e.getMessage(), e);
}
}
Aggregations