use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse 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 com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse 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 com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse 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);
}
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-addons by openhab.
the class HomeConnectBridgeHandler method initialize.
@Override
public void initialize() {
// let the bridge configuration servlet know about this handler
homeConnectServlet.addBridgeHandler(this);
// create oAuth service
ApiBridgeConfiguration config = getConfiguration();
String tokenUrl = (config.isSimulator() ? API_SIMULATOR_BASE_URL : API_BASE_URL) + OAUTH_TOKEN_PATH;
String authorizeUrl = (config.isSimulator() ? API_SIMULATOR_BASE_URL : API_BASE_URL) + OAUTH_AUTHORIZE_PATH;
String oAuthServiceHandleId = thing.getUID().getAsString() + (config.isSimulator() ? "simulator" : "");
oAuthClientService = oAuthFactory.createOAuthClientService(oAuthServiceHandleId, tokenUrl, authorizeUrl, config.getClientId(), config.getClientSecret(), OAUTH_SCOPE, true);
this.oAuthServiceHandleId = oAuthServiceHandleId;
logger.debug("Initialize oAuth client service. tokenUrl={}, authorizeUrl={}, oAuthServiceHandleId={}, scope={}, oAuthClientService={}", tokenUrl, authorizeUrl, oAuthServiceHandleId, OAUTH_SCOPE, oAuthClientService);
// create api client
apiClient = new HomeConnectApiClient(httpClient, oAuthClientService, config.isSimulator(), apiRequestHistory, config);
eventSourceClient = new HomeConnectEventSourceClient(clientBuilder, eventSourceFactory, oAuthClientService, config.isSimulator(), scheduler, eventHistory);
updateStatus(ThingStatus.UNKNOWN);
scheduler.submit(() -> {
try {
@Nullable AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse();
if (accessTokenResponse == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Please authenticate your account at http(s)://[YOUROPENHAB]:[YOURPORT]/homeconnect (e.g. http://192.168.178.100:8080/homeconnect).");
} else {
apiClient.getHomeAppliances();
updateStatus(ThingStatus.ONLINE);
}
} catch (OAuthException | IOException | OAuthResponseException | CommunicationException | AuthorizationException e) {
ZonedDateTime nextReinitializeDateTime = ZonedDateTime.now().plusSeconds(REINITIALIZATION_DELAY_SEC);
String offlineMessage = String.format("Home Connect service is not reachable or a problem occurred! Retrying at %s (%s). bridge=%s", nextReinitializeDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME), e.getMessage(), getThing().getLabel());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, offlineMessage);
scheduleReinitialize();
}
});
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-addons by openhab.
the class SmartherApi method request.
/**
* Calls the API gateway with the given http method, request url and actual data.
*
* @param method
* the http method to make the call with
* @param url
* the API operation url to call
* @param requestData
* the actual data to send in the request body, may be {@code null}
*
* @return the response received from the API gateway
*
* @throws {@link SmartherGatewayException}
* in case of communication issues with the API gateway
*/
private ContentResponse request(HttpMethod method, String url, @Nullable String requestData) throws SmartherGatewayException {
logger.debug("Request: ({}) {} - {}", method, url, StringUtil.defaultString(requestData));
Function<HttpClient, Request> call = httpClient -> httpClient.newRequest(url).method(method).header(HEADER_ACCEPT, CONTENT_TYPE).content(new StringContentProvider(StringUtil.defaultString(requestData)), CONTENT_TYPE);
try {
final AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse();
final String accessToken = (accessTokenResponse == null) ? null : accessTokenResponse.getAccessToken();
if (accessToken == null || accessToken.isEmpty()) {
throw new SmartherAuthorizationException(String.format("No gateway accesstoken. Did you authorize smarther via %s ?", AUTH_SERVLET_ALIAS));
} else {
return requestWithRetry(call, accessToken);
}
} catch (SmartherGatewayException e) {
throw e;
} catch (OAuthException | OAuthResponseException e) {
throw new SmartherAuthorizationException(e.getMessage(), e);
} catch (IOException e) {
throw new SmartherGatewayException(e.getMessage(), e);
}
}
Aggregations