use of org.openhab.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.
the class HomeConnectEventSourceClient method createClient.
private Client createClient(String target) throws CommunicationException, AuthorizationException {
boolean filterRegistered = clientBuilder.getConfiguration().isRegistered(HomeConnectStreamingRequestFilter.class);
Client client;
HomeConnectStreamingRequestFilter filter;
if (filterRegistered) {
filter = clientBuilder.getConfiguration().getInstances().stream().filter(instance -> instance instanceof HomeConnectStreamingRequestFilter).map(instance -> (HomeConnectStreamingRequestFilter) instance).findAny().orElseThrow();
client = clientBuilder.readTimeout(SSE_REQUEST_READ_TIMEOUT, TimeUnit.SECONDS).build();
} else {
filter = new HomeConnectStreamingRequestFilter();
client = clientBuilder.readTimeout(SSE_REQUEST_READ_TIMEOUT, TimeUnit.SECONDS).register(filter).build();
}
filter.setAuthorizationHeader(target, HttpHelper.getAuthorizationHeader(oAuthClientService));
return client;
}
use of org.openhab.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.
the class InnogyBridgeHandler method initializeClient.
/**
* Initializes the services and InnogyClient.
*/
private void initializeClient() {
final OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), API_URL_TOKEN, API_URL_TOKEN, bridgeConfiguration.clientId, bridgeConfiguration.clientSecret, null, true);
this.oAuthService = oAuthService;
if (checkOnAuthCode()) {
final InnogyClient localClient = createInnogyClient(oAuthService, httpClient);
client = localClient;
deviceStructMan = new DeviceStructureManager(createFullDeviceManager(localClient));
oAuthService.addAccessTokenRefreshListener(this);
registerDeviceStatusListener(InnogyBridgeHandler.this);
scheduleRestartClient(false);
}
}
use of org.openhab.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.
the class SmartherBridgeHandler method dispose.
@Override
public void dispose() {
logger.debug("Bridge[{}] Dispose handler", thing.getUID());
final OAuthClientService localOAuthService = this.oAuthService;
if (localOAuthService != null) {
localOAuthService.removeAccessTokenRefreshListener(this);
}
this.oAuthFactory.ungetOAuthService(thing.getUID().getAsString());
stopPoll(true);
logger.debug("Bridge[{}] Finished disposing!", thing.getUID());
}
use of org.openhab.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.
the class SmartherBridgeHandler method initialize.
// ===========================================================================
//
// Bridge thing lifecycle management methods
//
// ===========================================================================
@Override
public void initialize() {
logger.debug("Bridge[{}] Initialize handler", thing.getUID());
this.config = getConfigAs(SmartherBridgeConfiguration.class);
if (StringUtil.isBlank(config.getSubscriptionKey())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Subscription Key' property is not set or empty. If you have an older thing please recreate it.");
return;
}
if (StringUtil.isBlank(config.getClientId())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Client Id' property is not set or empty. If you have an older thing please recreate it.");
return;
}
if (StringUtil.isBlank(config.getClientSecret())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Client Secret' property is not set or empty. If you have an older thing please recreate it.");
return;
}
// Initialize OAuth2 authentication support
final OAuthClientService localOAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), SMARTHER_API_TOKEN_URL, SMARTHER_AUTHORIZE_URL, config.getClientId(), config.getClientSecret(), SMARTHER_API_SCOPES, false);
localOAuthService.addAccessTokenRefreshListener(SmartherBridgeHandler.this);
this.oAuthService = localOAuthService;
// Initialize Smarther Api
final SmartherApi localSmartherApi = new SmartherApi(localOAuthService, config.getSubscriptionKey(), scheduler, httpClient);
this.smartherApi = localSmartherApi;
// Initialize locations (plant Ids) local cache
final ExpiringCache<List<Location>> localLocationCache = new ExpiringCache<>(Duration.ofMinutes(config.getStatusRefreshPeriod()), this::locationCacheAction);
this.locationCache = localLocationCache;
// Initialize bridge local status
final BridgeStatus localBridgeStatus = new BridgeStatus();
this.bridgeStatus = localBridgeStatus;
updateStatus(ThingStatus.UNKNOWN);
schedulePoll();
logger.debug("Bridge[{}] Finished initializing!", thing.getUID());
}
use of org.openhab.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.
the class SmartherBridgeHandler method authorize.
@Override
public String authorize(String redirectUrl, String reqCode, String notificationUrl) throws SmartherGatewayException {
try {
logger.debug("Bridge[{}] Call API gateway to get access token. RedirectUri: {}", thing.getUID(), redirectUrl);
final OAuthClientService localOAuthService = this.oAuthService;
if (localOAuthService == null) {
throw new SmartherAuthorizationException("Authorization service is null");
}
// OAuth2 call to get access token from received authorization code
localOAuthService.getAccessTokenResponseByAuthorizationCode(reqCode, redirectUrl);
// Store the notification URL in bridge configuration
Configuration configuration = editConfiguration();
configuration.put(PROPERTY_NOTIFICATION_URL, notificationUrl);
updateConfiguration(configuration);
config.setNotificationUrl(notificationUrl);
logger.debug("Bridge[{}] Store notification URL: {}", thing.getUID(), notificationUrl);
// Reschedule the polling thread
schedulePoll();
return config.getClientId();
} catch (OAuthResponseException e) {
throw new SmartherAuthorizationException(e.toString(), e);
} catch (OAuthException | IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
throw new SmartherGatewayException(e.getMessage(), e);
}
}
Aggregations