Search in sources :

Example 1 with UsernameIdentityValidator

use of org.eclipse.milo.opcua.sdk.server.identity.UsernameIdentityValidator in project milo by eclipse.

the class TestServer method create.

public static OpcUaServer create(int port) throws Exception {
    File securityTempDir = new File(System.getProperty("java.io.tmpdir"), "security");
    if (!securityTempDir.exists() && !securityTempDir.mkdirs()) {
        throw new Exception("unable to create security temp dir: " + securityTempDir);
    }
    LoggerFactory.getLogger(TestServer.class).info("security temp dir: {}", securityTempDir.getAbsolutePath());
    KeyStoreLoader loader = new KeyStoreLoader().load(securityTempDir);
    DefaultCertificateManager certificateManager = new DefaultCertificateManager(loader.getServerKeyPair(), loader.getServerCertificateChain());
    File pkiDir = securityTempDir.toPath().resolve("pki").toFile();
    DefaultTrustListManager trustListManager = new DefaultTrustListManager(pkiDir);
    LoggerFactory.getLogger(TestServer.class).info("pki dir: {}", pkiDir.getAbsolutePath());
    DefaultServerCertificateValidator certificateValidator = new DefaultServerCertificateValidator(trustListManager);
    KeyPair httpsKeyPair = SelfSignedCertificateGenerator.generateRsaKeyPair(2048);
    SelfSignedHttpsCertificateBuilder httpsCertificateBuilder = new SelfSignedHttpsCertificateBuilder(httpsKeyPair);
    httpsCertificateBuilder.setCommonName(HostnameUtil.getHostname());
    HostnameUtil.getHostnames("localhost", false).forEach(httpsCertificateBuilder::addDnsName);
    X509Certificate httpsCertificate = httpsCertificateBuilder.build();
    UsernameIdentityValidator identityValidator = new UsernameIdentityValidator(true, authChallenge -> {
        String username = authChallenge.getUsername();
        String password = authChallenge.getPassword();
        boolean user1 = "user1".equals(username) && "password".equals(password);
        boolean user2 = "user2".equals(username) && "password".equals(password);
        boolean admin = "admin".equals(username) && "password".equals(password);
        return user1 || user2 || admin;
    });
    // If you need to use multiple certificates you'll have to be smarter than this.
    X509Certificate certificate = certificateManager.getCertificates().stream().findFirst().orElseThrow(() -> new UaRuntimeException(StatusCodes.Bad_ConfigurationError, "no certificate found"));
    // The configured application URI must match the one in the certificate(s)
    String applicationUri = CertificateUtil.getSanUri(certificate).orElseThrow(() -> new UaRuntimeException(StatusCodes.Bad_ConfigurationError, "certificate is missing the application URI"));
    Set<EndpointConfiguration> endpointConfigurations = createEndpointConfigurations(certificate, port);
    OpcUaServerConfig serverConfig = OpcUaServerConfig.builder().setApplicationUri(applicationUri).setApplicationName(LocalizedText.english("Eclipse Milo OPC UA Example Server")).setEndpoints(endpointConfigurations).setBuildInfo(new BuildInfo("urn:eclipse:milo:example-server", "eclipse", "eclipse milo example server", OpcUaServer.SDK_VERSION, "", DateTime.now())).setCertificateManager(certificateManager).setTrustListManager(trustListManager).setCertificateValidator(certificateValidator).setHttpsKeyPair(httpsKeyPair).setHttpsCertificate(httpsCertificate).setIdentityValidator(identityValidator).setProductUri("urn:eclipse:milo:example-server").build();
    return new OpcUaServer(serverConfig);
}
Also used : KeyPair(java.security.KeyPair) OpcUaServer(org.eclipse.milo.opcua.sdk.server.OpcUaServer) OpcUaServerConfig(org.eclipse.milo.opcua.sdk.server.api.config.OpcUaServerConfig) UaRuntimeException(org.eclipse.milo.opcua.stack.core.UaRuntimeException) X509Certificate(java.security.cert.X509Certificate) UaRuntimeException(org.eclipse.milo.opcua.stack.core.UaRuntimeException) DefaultTrustListManager(org.eclipse.milo.opcua.stack.core.security.DefaultTrustListManager) SelfSignedHttpsCertificateBuilder(org.eclipse.milo.opcua.stack.core.util.SelfSignedHttpsCertificateBuilder) UsernameIdentityValidator(org.eclipse.milo.opcua.sdk.server.identity.UsernameIdentityValidator) DefaultCertificateManager(org.eclipse.milo.opcua.stack.core.security.DefaultCertificateManager) BuildInfo(org.eclipse.milo.opcua.stack.core.types.structured.BuildInfo) EndpointConfiguration(org.eclipse.milo.opcua.stack.server.EndpointConfiguration) File(java.io.File) DefaultServerCertificateValidator(org.eclipse.milo.opcua.stack.server.security.DefaultServerCertificateValidator)

Aggregations

File (java.io.File)1 KeyPair (java.security.KeyPair)1 X509Certificate (java.security.cert.X509Certificate)1 OpcUaServer (org.eclipse.milo.opcua.sdk.server.OpcUaServer)1 OpcUaServerConfig (org.eclipse.milo.opcua.sdk.server.api.config.OpcUaServerConfig)1 UsernameIdentityValidator (org.eclipse.milo.opcua.sdk.server.identity.UsernameIdentityValidator)1 UaRuntimeException (org.eclipse.milo.opcua.stack.core.UaRuntimeException)1 DefaultCertificateManager (org.eclipse.milo.opcua.stack.core.security.DefaultCertificateManager)1 DefaultTrustListManager (org.eclipse.milo.opcua.stack.core.security.DefaultTrustListManager)1 BuildInfo (org.eclipse.milo.opcua.stack.core.types.structured.BuildInfo)1 SelfSignedHttpsCertificateBuilder (org.eclipse.milo.opcua.stack.core.util.SelfSignedHttpsCertificateBuilder)1 EndpointConfiguration (org.eclipse.milo.opcua.stack.server.EndpointConfiguration)1 DefaultServerCertificateValidator (org.eclipse.milo.opcua.stack.server.security.DefaultServerCertificateValidator)1