use of org.openhab.core.auth.client.oauth2.OAuthResponseException 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.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class GoogleCloudAPI method getAccessToken.
/**
* Fetches the OAuth2 tokens from Google Cloud Platform if the auth-code is set in the configuration. If successful
* the auth-code will be removed from the configuration.
*
* @throws AuthenticationException
* @throws CommunicationException
*/
@SuppressWarnings("null")
private void getAccessToken() throws AuthenticationException, CommunicationException {
String authcode = config.authcode;
if (authcode != null && !authcode.isEmpty()) {
logger.debug("Trying to get access and refresh tokens.");
try {
oAuthService.getAccessTokenResponseByAuthorizationCode(authcode, GCP_REDIRECT_URI);
} catch (OAuthException | OAuthResponseException e) {
logger.debug("Error fetching access token: {}", e.getMessage(), e);
throw new AuthenticationException("Error fetching access token. Invalid authcode? Please generate a new one.");
} catch (IOException e) {
throw new CommunicationException(String.format("An unexpected IOException occurred: %s", e.getMessage()));
}
config.authcode = null;
try {
Configuration serviceConfig = configAdmin.getConfiguration(GoogleTTSService.SERVICE_PID);
Dictionary<String, Object> configProperties = serviceConfig.getProperties();
if (configProperties != null) {
configProperties.put(GoogleTTSService.PARAM_AUTHCODE, "");
serviceConfig.update(configProperties);
}
} catch (IOException e) {
// should not happen
logger.warn("Failed to update configuration for Google Cloud TTS service. Please clear the 'authcode' configuration parameter manualy.");
}
}
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class MyQAccountHandler method sendRequest.
private synchronized ContentResponse sendRequest(String url, HttpMethod method, @Nullable ContentProvider content, @Nullable String contentType) throws InterruptedException, MyQCommunicationException, MyQAuthenticationException {
AccessTokenResponse tokenResponse = null;
// if we don't need to force a login, attempt to use the token we have
if (!needsLogin) {
try {
tokenResponse = getOAuthService().getAccessTokenResponse();
} catch (OAuthException | IOException | OAuthResponseException e) {
// ignore error, will try to login below
logger.debug("Error accessing token, will attempt to login again", e);
}
}
// if no token, or we need to login, do so now
if (tokenResponse == null) {
tokenResponse = login();
needsLogin = false;
}
Request request = httpClient.newRequest(url).method(method).agent(userAgent).timeout(10, TimeUnit.SECONDS).header("Authorization", authTokenHeader(tokenResponse));
if (content != null & contentType != null) {
request = request.content(content, contentType);
}
// use asyc jetty as the API service will response with a 401 error when credentials are wrong,
// but not a WWW-Authenticate header which causes Jetty to throw a generic execution exception which
// prevents us from knowing the response code
logger.trace("Sending {} to {}", request.getMethod(), request.getURI());
final CompletableFuture<ContentResponse> futureResult = new CompletableFuture<>();
request.send(new BufferingResponseListener() {
@NonNullByDefault({})
@Override
public void onComplete(Result result) {
Response response = result.getResponse();
futureResult.complete(new HttpContentResponse(response, getContent(), getMediaType(), getEncoding()));
}
});
try {
ContentResponse result = futureResult.get();
logger.trace("Account Response - status: {} content: {}", result.getStatus(), result.getContentAsString());
return result;
} catch (ExecutionException e) {
throw new MyQCommunicationException(e.getMessage());
}
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-addons by openhab.
the class SpotifyBridgeHandler method authorize.
@Override
public String authorize(String redirectUri, String reqCode) {
try {
logger.debug("Make call to Spotify to get access token.");
final AccessTokenResponse credentials = oAuthService.getAccessTokenResponseByAuthorizationCode(reqCode, redirectUri);
final String user = updateProperties(credentials);
logger.debug("Authorized for user: {}", user);
startPolling();
return user;
} catch (RuntimeException | OAuthException | IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
throw new SpotifyException(e.getMessage(), e);
} catch (final OAuthResponseException e) {
throw new SpotifyAuthorizationException(e.getMessage(), e);
}
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-core by openhab.
the class OAuthClientServiceImpl method getAccessTokenByResourceOwnerPasswordCredentials.
@Override
public AccessTokenResponse getAccessTokenByResourceOwnerPasswordCredentials(String username, String password, @Nullable String scope) throws OAuthException, IOException, OAuthResponseException {
if (isClosed()) {
throw new OAuthException(EXCEPTION_MESSAGE_CLOSED);
}
String tokenUrl = persistedParams.tokenUrl;
if (tokenUrl == null) {
throw new OAuthException("Missing token url");
}
OAuthConnector connector = new OAuthConnector(httpClientFactory, persistedParams.deserializerClassName);
AccessTokenResponse accessTokenResponse = connector.grantTypePassword(tokenUrl, username, password, persistedParams.clientId, persistedParams.clientSecret, scope, Boolean.TRUE.equals(persistedParams.supportsBasicAuth));
// store it
storeHandler.saveAccessTokenResponse(handle, accessTokenResponse);
return accessTokenResponse;
}
Aggregations