Search in sources :

Example 1 with IAuthorizator

use of io.moquette.spi.security.IAuthorizator in project smarthome by eclipse.

the class EmbeddedBrokerServiceImpl method startEmbeddedServer.

@Override
public void startEmbeddedServer(@Nullable Integer portParam, boolean secure, @Nullable String username, @Nullable String password, String persistenceFilenameParam) throws IOException {
    String persistenceFilename = persistenceFilenameParam;
    Server server = new Server();
    Properties properties = new Properties();
    // Host and port
    properties.put(BrokerConstants.HOST_PROPERTY_NAME, "0.0.0.0");
    int port;
    if (secure) {
        port = (portParam == null) ? port = 8883 : portParam;
        properties.put(BrokerConstants.SSL_PORT_PROPERTY_NAME, Integer.toString(port));
        properties.put(BrokerConstants.PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
        properties.put(BrokerConstants.KEY_MANAGER_PASSWORD_PROPERTY_NAME, "esheshesh");
    } else {
        port = (portParam == null) ? port = 1883 : portParam;
        // with SSL_PORT_PROPERTY_NAME set, netty tries to evaluate the SSL context and shuts down immediately.
        // properties.put(BrokerConstants.SSL_PORT_PROPERTY_NAME, BrokerConstants.DISABLED_PORT_BIND);
        properties.put(BrokerConstants.PORT_PROPERTY_NAME, Integer.toString(port));
    }
    // Authentication
    io.moquette.spi.security.IAuthenticator authentificator = null;
    if (username != null && password != null && username.length() > 0 && password.length() > 0) {
        properties.put(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, false);
        properties.put(BrokerConstants.AUTHENTICATOR_CLASS_NAME, MqttEmbeddedBrokerUserAuthenticator.class.getName());
        authentificator = new MqttEmbeddedBrokerUserAuthenticator(username, password.getBytes());
    } else {
        properties.put(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, true);
    }
    // Persistence: If not set, an in-memory database is used.
    if (!persistenceFilename.isEmpty()) {
        if (!Paths.get(persistenceFilename).isAbsolute()) {
            persistenceFilename = Paths.get(ConfigConstants.getUserDataFolder()).toAbsolutePath().resolve(persistenceFilename).toString();
        }
        properties.put(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME, persistenceFilename);
    }
    // We may provide ACL functionality at some point as well
    IAuthorizator authorizer = null;
    // Secure connection support
    // TODO wait for NetworkServerTls implementation
    // try {
    // final SSLContext sslContext = networkServerTls.createSSLContext("mqtt");
    // server.startServer(new MemoryConfig(properties), null, () -> sslContext, authentificator, authorizer);
    // } catch (GeneralSecurityException | IOException e) {
    // logger.error("No SSL available", e);
    server.startServer(new MemoryConfig(properties), null, null, authentificator, authorizer);
    // }
    this.server = server;
    metrics.setServer(server);
    ScheduledExecutorService s = new ScheduledThreadPoolExecutor(1);
    detectStart.startBrokerStartedDetection(port, s);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Server(io.moquette.server.Server) MemoryConfig(io.moquette.server.config.MemoryConfig) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) IAuthorizator(io.moquette.spi.security.IAuthorizator) Properties(java.util.Properties)

Aggregations

Server (io.moquette.server.Server)1 MemoryConfig (io.moquette.server.config.MemoryConfig)1 IAuthorizator (io.moquette.spi.security.IAuthorizator)1 Properties (java.util.Properties)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1