Search in sources :

Example 1 with OAuthClientService

use of org.openhab.core.auth.client.oauth2.OAuthClientService in project smarthome by eclipse.

the class OAuthFactoryImpl method createOAuthClientService.

@Override
public OAuthClientService createOAuthClientService(String handle, String tokenUrl, @Nullable String authorizationUrl, String clientId, @Nullable String clientSecret, @Nullable String scope, @Nullable Boolean supportsBasicAuth) {
    PersistedParams params = oAuthStoreHandler.loadPersistedParams(handle);
    PersistedParams newParams = new PersistedParams(handle, tokenUrl, authorizationUrl, clientId, clientSecret, scope, supportsBasicAuth, tokenExpiresInBuffer);
    OAuthClientService clientImpl = null;
    // If parameters in storage and parameters are the same as arguments passed get the client from storage
    if (params != null && params.equals(newParams)) {
        clientImpl = getOAuthClientService(handle);
    }
    // client in storage.
    if (clientImpl == null) {
        clientImpl = OAuthClientServiceImpl.createInstance(handle, oAuthStoreHandler, httpClientFactory, newParams);
        oauthClientServiceCache.put(handle, clientImpl);
    }
    return clientImpl;
}
Also used : OAuthClientService(org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService)

Example 2 with OAuthClientService

use of org.openhab.core.auth.client.oauth2.OAuthClientService in project smarthome by eclipse.

the class AbstractTestAgent method testCreateClient.

@Override
public OAuthClientService testCreateClient() {
    logger.debug("test createClient");
    handle = UUID.randomUUID().toString();
    OAuthClientService service = oauthFactory.createOAuthClientService(handle, tokenUrl, authUrl, clientId, clientSecret, scope, false);
    logger.info("new client handle: {}", handle);
    return service;
}
Also used : OAuthClientService(org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService)

Example 3 with OAuthClientService

use of org.openhab.core.auth.client.oauth2.OAuthClientService in project smarthome by eclipse.

the class ConsoleOAuthCommandExtension method execute.

@Override
public void execute(String[] args, Console console) {
    this.console = console;
    if (args.length < 2) {
        console.println("Argument expected.  Please check usage.");
        return;
    }
    AbstractTestAgent agent = getTestAgent(args[0]);
    if (agent == null) {
        console.println("Unexpected test agent:" + args[0]);
        return;
    }
    AccessTokenResponse response;
    try {
        switch(args[1]) {
            case "create":
                OAuthClientService newService = agent.testCreateClient();
                console.println("handle: " + agent.handle + ", service: " + newService);
                break;
            case "getAccessTokenByResourceOwnerPassword":
                response = agent.testGetAccessTokenByResourceOwnerPasswordCredentials();
                consolePrintAccessToken(response);
                break;
            case "getClient":
                OAuthClientService service = agent.testGetClient(args[2]);
                console.println("OAuthClientService: " + service);
                break;
            case "refresh":
                response = agent.testRefreshToken();
                consolePrintAccessToken(response);
                break;
            case "getAccessTokenByCode":
                console.println("using authorization code: " + args[2]);
                response = agent.testGetAccessTokenByAuthorizationCode(args[2]);
                consolePrintAccessToken(response);
                break;
            case "getAuthorizationUrl":
                String authURL;
                if (args.length >= 3) {
                    authURL = agent.testGetAuthorizationUrl(args[2]);
                    console.println("Authorization URL: " + authURL + " state: " + args[2]);
                } else {
                    authURL = agent.testGetAuthorizationUrl(null);
                    console.println("Authorization URL: " + authURL + " state: null");
                }
                break;
            case "getCachedAccessToken":
                response = agent.testGetCachedAccessToken();
                consolePrintAccessToken(response);
                break;
            case "close":
                console.println("Closing test agent client service...");
                agent.close();
                break;
            case "delete":
                console.println("Delete by handle: " + args[2]);
                agent.delete(args[2]);
                break;
            default:
                console.println("Commands are case-sensitive.  Unknown command: " + args[1]);
                break;
        }
    } catch (OAuthException | IOException | OAuthResponseException e) {
        console.print(String.format("%s %s, cause %s", e.getClass(), e.getMessage(), e.getCause()));
    }
}
Also used : OAuthResponseException(org.eclipse.smarthome.core.auth.client.oauth2.OAuthResponseException) OAuthClientService(org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService) AbstractTestAgent(org.eclipse.smarthome.auth.oauth2client.test.internal.AbstractTestAgent) OAuthException(org.eclipse.smarthome.core.auth.client.oauth2.OAuthException) IOException(java.io.IOException) AccessTokenResponse(org.eclipse.smarthome.core.auth.client.oauth2.AccessTokenResponse)

Example 4 with OAuthClientService

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

the class InnogyBridgeHandlerTest method before.

@BeforeEach
public void before() throws Exception {
    bridgeMock = mock(Bridge.class);
    when(bridgeMock.getUID()).thenReturn(new ThingUID("innogysmarthome", "bridge"));
    webSocketMock = mock(InnogyWebSocket.class);
    OAuthClientService oAuthService = mock(OAuthClientService.class);
    OAuthFactory oAuthFactoryMock = mock(OAuthFactory.class);
    when(oAuthFactoryMock.createOAuthClientService(any(), any(), any(), any(), any(), any(), any())).thenReturn(oAuthService);
    HttpClient httpClientMock = mock(HttpClient.class);
    bridgeHandler = new InnogyBridgeHandlerAccessible(bridgeMock, oAuthFactoryMock, httpClientMock);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) ThingUID(org.openhab.core.thing.ThingUID) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) HttpClient(org.eclipse.jetty.client.HttpClient) InnogyWebSocket(org.openhab.binding.innogysmarthome.internal.InnogyWebSocket) Bridge(org.openhab.core.thing.Bridge) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with OAuthClientService

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

the class HomeConnectBridgeHandler method handleConfigurationUpdate.

@Override
public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
    if (isModifyingCurrentConfig(configurationParameters)) {
        List<String> parameters = configurationParameters.entrySet().stream().map((entry) -> {
            if (CLIENT_ID.equals(entry.getKey()) || CLIENT_SECRET.equals(entry.getKey())) {
                return entry.getKey() + ": ***";
            }
            return entry.getKey() + ": " + entry.getValue();
        }).collect(Collectors.toList());
        logger.debug("Update bridge configuration. bridge={}, parameters={}", getThing().getLabel(), parameters);
        validateConfigurationParameters(configurationParameters);
        Configuration configuration = editConfiguration();
        for (Entry<String, Object> configurationParameter : configurationParameters.entrySet()) {
            configuration.put(configurationParameter.getKey(), configurationParameter.getValue());
        }
        // invalidate oAuth credentials
        try {
            logger.debug("Clear oAuth credential store. bridge={}", getThing().getLabel());
            var oAuthClientService = this.oAuthClientService;
            if (oAuthClientService != null) {
                oAuthClientService.remove();
            }
        } catch (OAuthException e) {
            logger.error("Could not clear oAuth credentials. bridge={}", getThing().getLabel(), e);
        }
        if (isInitialized()) {
            // persist new configuration and reinitialize handler
            dispose();
            updateConfiguration(configuration);
            initialize();
        } else {
            // persist new configuration and notify Thing Manager
            updateConfiguration(configuration);
            @Nullable ThingHandlerCallback callback = getCallback();
            if (callback != null) {
                callback.configurationUpdated(this.getThing());
            } else {
                logger.warn("Handler {} tried updating its configuration although the handler was already disposed.", this.getClass().getSimpleName());
            }
        }
    }
}
Also used : ApiRequest(org.openhab.binding.homeconnect.internal.client.model.ApiRequest) ScheduledFuture(java.util.concurrent.ScheduledFuture) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) ZonedDateTime(java.time.ZonedDateTime) AuthorizationException(org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException) LoggerFactory(org.slf4j.LoggerFactory) ThingHandlerService(org.openhab.core.thing.binding.ThingHandlerService) ArrayList(java.util.ArrayList) HttpClient(org.eclipse.jetty.client.HttpClient) ClientBuilder(javax.ws.rs.client.ClientBuilder) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Map(java.util.Map) HomeConnectServlet(org.openhab.binding.homeconnect.internal.servlet.HomeConnectServlet) ChannelUID(org.openhab.core.thing.ChannelUID) CommunicationException(org.openhab.binding.homeconnect.internal.client.exception.CommunicationException) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) HomeConnectApiClient(org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) Collection(java.util.Collection) Event(org.openhab.binding.homeconnect.internal.client.model.Event) IOException(java.io.IOException) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) HomeConnectEventSourceClient(org.openhab.binding.homeconnect.internal.client.HomeConnectEventSourceClient) HomeConnectBindingConstants(org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants) Collectors(java.util.stream.Collectors) SseEventSourceFactory(org.osgi.service.jaxrs.client.SseEventSourceFactory) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ApiBridgeConfiguration(org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) Entry(java.util.Map.Entry) HomeConnectDiscoveryService(org.openhab.binding.homeconnect.internal.discovery.HomeConnectDiscoveryService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) Collections(java.util.Collections) Bridge(org.openhab.core.thing.Bridge) Configuration(org.openhab.core.config.core.Configuration) ApiBridgeConfiguration(org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Nullable(org.eclipse.jdt.annotation.Nullable)

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