Search in sources :

Example 36 with OAuthClientService

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;
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) CommunicationException(org.openhab.binding.homeconnect.internal.client.exception.CommunicationException) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) Collection(java.util.Collection) Client(javax.ws.rs.client.Client) AuthorizationException(org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException) Event(org.openhab.binding.homeconnect.internal.client.model.Event) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) HomeConnectBindingConstants(org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants) Collectors(java.util.stream.Collectors) SseEventSourceFactory(org.osgi.service.jaxrs.client.SseEventSourceFactory) TimeUnit(java.util.concurrent.TimeUnit) ClientBuilder(javax.ws.rs.client.ClientBuilder) HomeConnectEventListener(org.openhab.binding.homeconnect.internal.client.listener.HomeConnectEventListener) List(java.util.List) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Client(javax.ws.rs.client.Client)

Example 37 with OAuthClientService

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);
    }
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) InnogyClient(org.openhab.binding.innogysmarthome.internal.client.InnogyClient) DeviceStructureManager(org.openhab.binding.innogysmarthome.internal.manager.DeviceStructureManager)

Example 38 with OAuthClientService

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());
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService)

Example 39 with OAuthClientService

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());
}
Also used : SmartherApi(org.openhab.binding.bticinosmarther.internal.api.SmartherApi) ExpiringCache(org.openhab.core.cache.ExpiringCache) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) List(java.util.List) ArrayList(java.util.ArrayList) BridgeStatus(org.openhab.binding.bticinosmarther.internal.model.BridgeStatus) SmartherBridgeConfiguration(org.openhab.binding.bticinosmarther.internal.config.SmartherBridgeConfiguration)

Example 40 with OAuthClientService

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);
    }
}
Also used : OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) SmartherGatewayException(org.openhab.binding.bticinosmarther.internal.api.exception.SmartherGatewayException) Configuration(org.openhab.core.config.core.Configuration) SmartherBridgeConfiguration(org.openhab.binding.bticinosmarther.internal.config.SmartherBridgeConfiguration) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) IOException(java.io.IOException) SmartherAuthorizationException(org.openhab.binding.bticinosmarther.internal.api.exception.SmartherAuthorizationException)

Aggregations

OAuthClientService (org.openhab.core.auth.client.oauth2.OAuthClientService)36 OAuthFactory (org.openhab.core.auth.client.oauth2.OAuthFactory)15 AccessTokenResponse (org.openhab.core.auth.client.oauth2.AccessTokenResponse)12 IOException (java.io.IOException)10 Test (org.junit.jupiter.api.Test)10 OAuthResponseException (org.openhab.core.auth.client.oauth2.OAuthResponseException)10 Nullable (org.eclipse.jdt.annotation.Nullable)8 OAuthException (org.openhab.core.auth.client.oauth2.OAuthException)7 OAuthClientService (org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService)6 AuthorizationException (org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException)4 CommunicationException (org.openhab.binding.homeconnect.internal.client.exception.CommunicationException)4 List (java.util.List)3 Map (java.util.Map)3 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2