Search in sources :

Example 11 with OAuthClientService

use of org.openhab.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.

the class OAuthAuthorizationHandlerImplTest method getClientService.

private OAuthClientService getClientService() {
    final OAuthClientService clientService = this.clientService;
    assertNotNull(clientService);
    return Objects.requireNonNull(clientService);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService)

Example 12 with OAuthClientService

use of org.openhab.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.

the class OpenHabOAuthTokenRefresher method unsetRefreshListener.

@Override
public void unsetRefreshListener(String serviceHandle) {
    final AccessTokenRefreshListener refreshListener = listenerByServiceHandle.get(serviceHandle);
    if (refreshListener != null) {
        try {
            OAuthClientService clientService = getOAuthClientService(serviceHandle);
            clientService.removeAccessTokenRefreshListener(refreshListener);
        } catch (OAuthException e) {
            logger.warn("Failed to remove refresh listener: OAuth client service is unavailable. Cause: {}", e.getMessage());
        }
    }
    listenerByServiceHandle.remove(serviceHandle);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) AccessTokenRefreshListener(org.openhab.core.auth.client.oauth2.AccessTokenRefreshListener)

Example 13 with OAuthClientService

use of org.openhab.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.

the class OpenHabOAuthTokenRefresher method refreshToken.

@Override
public void refreshToken(String serviceHandle) {
    if (listenerByServiceHandle.get(serviceHandle) == null) {
        logger.warn("Token refreshing was requested but there is no token refresh listener registered!");
        return;
    }
    OAuthClientService clientService = getOAuthClientService(serviceHandle);
    refreshAccessToken(clientService);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService)

Example 14 with OAuthClientService

use of org.openhab.core.auth.client.oauth2.OAuthClientService 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;
    }
}
Also used : OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthException(org.openhab.binding.mielecloud.internal.auth.OAuthException) IOException(java.io.IOException) NoOngoingAuthorizationException(org.openhab.binding.mielecloud.internal.config.exception.NoOngoingAuthorizationException)

Example 15 with OAuthClientService

use of org.openhab.core.auth.client.oauth2.OAuthClientService in project openhab-addons by openhab.

the class MieleHandlerFactoryTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    registerVolatileStorageService();
    thingRegistry = getService(ThingRegistry.class, ThingRegistry.class);
    assertNotNull(thingRegistry, "Thing registry is missing");
    // Ensure the MieleWebservice is not initialized.
    MieleHandlerFactory factory = getService(ThingHandlerFactory.class, MieleHandlerFactory.class);
    assertNotNull(factory);
    // Assume an access token has already been stored
    AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
    accessTokenResponse.setAccessToken(ACCESS_TOKEN);
    OAuthClientService oAuthClientService = mock(OAuthClientService.class);
    when(oAuthClientService.getAccessTokenResponse()).thenReturn(accessTokenResponse);
    OAuthFactory oAuthFactory = mock(OAuthFactory.class);
    when(oAuthFactory.getOAuthClientService(MieleCloudBindingIntegrationTestConstants.EMAIL)).thenReturn(oAuthClientService);
    OpenHabOAuthTokenRefresher tokenRefresher = getService(OAuthTokenRefresher.class, OpenHabOAuthTokenRefresher.class);
    assertNotNull(tokenRefresher);
    setPrivate(Objects.requireNonNull(tokenRefresher), "oauthFactory", oAuthFactory);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) OpenHabOAuthTokenRefresher(org.openhab.binding.mielecloud.internal.auth.OpenHabOAuthTokenRefresher) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) ThingRegistry(org.openhab.core.thing.ThingRegistry) BeforeEach(org.junit.jupiter.api.BeforeEach)

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