Search in sources :

Example 1 with OAuthClientCredentialsGrant

use of org.openremote.model.auth.OAuthClientCredentialsGrant in project openremote by openremote.

the class MqttConnection method setCredentials.

public void setCredentials(String realm, String username, String password) {
    this.realm = realm;
    this.username = username;
    this.password = password;
    credentials = !TextUtil.isNullOrEmpty(realm) && !TextUtil.isNullOrEmpty(username) && !TextUtil.isNullOrEmpty(password);
    if (credentials) {
        String tokenEndpointUri = identityProvider.getTokenUri(realm).toString();
        OAuthGrant grant = new OAuthClientCredentialsGrant(tokenEndpointUri, username, password, null);
        tokenSupplier = identityProvider.getAccessTokenSupplier(grant);
    } else {
        LOG.fine("MQTT connection with no credentials so will have limited capabilities: " + this);
    }
}
Also used : OAuthGrant(org.openremote.model.auth.OAuthGrant) OAuthClientCredentialsGrant(org.openremote.model.auth.OAuthClientCredentialsGrant)

Example 2 with OAuthClientCredentialsGrant

use of org.openremote.model.auth.OAuthClientCredentialsGrant in project openremote by openremote.

the class GatewayClientService method createGatewayClient.

protected WebsocketIOClient<String> createGatewayClient(GatewayConnection connection) {
    if (connection.isDisabled()) {
        LOG.info("Disabled gateway client connection so ignoring: " + connection);
        return null;
    }
    LOG.info("Creating gateway IO client: " + connection);
    try {
        WebsocketIOClient<String> client = new WebsocketIOClient<>(new URIBuilder().setScheme(connection.isSecured() ? "wss" : "ws").setHost(connection.getHost()).setPort(connection.getPort() == null ? -1 : connection.getPort()).setPath("websocket/events").setParameter(Constants.REALM_PARAM_NAME, connection.getRealm()).build(), null, new OAuthClientCredentialsGrant(new URIBuilder().setScheme(connection.isSecured() ? "https" : "http").setHost(connection.getHost()).setPort(connection.getPort() == null ? -1 : connection.getPort()).setPath("auth/realms/" + connection.getRealm() + "/protocol/openid-connect/token").build().toString(), connection.getClientId(), connection.getClientSecret(), null).setBasicAuthHeader(true));
        client.setEncoderDecoderProvider(() -> new ChannelHandler[] { new AbstractNettyIOClient.MessageToMessageDecoder<>(String.class, client) });
        client.addConnectionStatusConsumer(connectionStatus -> onGatewayClientConnectionStatusChanged(connection, connectionStatus));
        client.addMessageConsumer(message -> onCentralManagerMessage(connection, message));
        // Subscribe to Asset<?> and attribute events of local realm and pass through to connected manager
        clientEventService.addInternalSubscription(getClientSessionKey(connection) + "Asset", AssetEvent.class, new AssetFilter<AssetEvent>().setRealm(connection.getLocalRealm()), assetEvent -> sendCentralManagerMessage(connection.getLocalRealm(), messageToString(SharedEvent.MESSAGE_PREFIX, assetEvent)));
        clientEventService.addInternalSubscription(getClientSessionKey(connection) + "Attribute", AttributeEvent.class, new AssetFilter<AttributeEvent>().setRealm(connection.getLocalRealm()), attributeEvent -> sendCentralManagerMessage(connection.getLocalRealm(), messageToString(SharedEvent.MESSAGE_PREFIX, attributeEvent)));
        client.connect();
        return client;
    } catch (Exception e) {
        LOG.log(Level.WARNING, "Creating gateway IO client failed so marking connection as disabled: " + connection, e);
        connection.setDisabled(true);
        setConnection(connection);
    }
    return null;
}
Also used : WebsocketIOClient(org.openremote.agent.protocol.websocket.WebsocketIOClient) OAuthClientCredentialsGrant(org.openremote.model.auth.OAuthClientCredentialsGrant) AbstractNettyIOClient(org.openremote.agent.protocol.io.AbstractNettyIOClient) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

OAuthClientCredentialsGrant (org.openremote.model.auth.OAuthClientCredentialsGrant)2 URIBuilder (org.apache.http.client.utils.URIBuilder)1 AbstractNettyIOClient (org.openremote.agent.protocol.io.AbstractNettyIOClient)1 WebsocketIOClient (org.openremote.agent.protocol.websocket.WebsocketIOClient)1 OAuthGrant (org.openremote.model.auth.OAuthGrant)1