How to use node-opcua-secure-channel - 10 common examples

To help you get started, we’ve selected a few node-opcua-secure-channel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github node-opcua / node-opcua / packages / node-opcua-server / source / register_server_manager.ts View on Github external
assert(this.server!.serverType !== undefined,
          " must have a valid server Type");

        // construct connection
        const server = this.server!;
        const selectedEndpoint = this.selectedEndpoint;

        if (!selectedEndpoint) {
            console.log("Warning : cannot register server - no endpoint available");
            return outer_callback(new Error("Cannot registerServer"));
        }

        const options = {
            securityMode: selectedEndpoint.securityMode,
            securityPolicy: coerceSecurityPolicy(selectedEndpoint.securityPolicyUri),
            serverCertificate: selectedEndpoint.serverCertificate,

            certificateFile: server.certificateFile,
            privateKeyFile: server.privateKeyFile,

            clientName: "RegistrationClient-2",
            connectionStrategy: no_reconnect_connectivity_strategy
        };

        const client = OPCUAClientBase.create(options);

        const tmp = this._serverEndpoints;
        (client as any)._serverEndpoints = tmp;
        (server as any)._registration_client = client;

        const theStatus = isOnline
github node-opcua / node-opcua / packages / node-opcua-server / src / register_server_manager.js View on Github external
assert(self.selectedEndpoint || "must have a selected endpoint => please call _establish_initial_connection");
    assert(self.server.serverType !== undefined, " must have a valid server Type");
    assert(self.discoveryServerEndpointUrl);

    // construct connection
    const server = this.server;
    const selectedEndpoint = self.selectedEndpoint;

    if (!selectedEndpoint) {
        console.log("Warning : cannot register server - no endpoint avaialble");
        return outer_callback(new Error("Cannot registerServer"));
    }

    const options = {
        securityMode: selectedEndpoint.securityMode,
        securityPolicy: coerceSecurityPolicy(selectedEndpoint.securityPolicyUri),
        serverCertificate: selectedEndpoint.serverCertificate,
        certificateFile: server.certificateFile,
        privateKeyFile: server.privateKeyFile,
        connectionStrategy: no_reconnect_connectivity_strategy,
        clientName: "RegistrationClient-2"
    };

    const tmp = self._serverEndpoints;

    let client = OPCUAClientBase.create(options);
    client._serverEndpoints = tmp;
    server._registration_client = client;

    const theStatus =isOnline ? RegisterServerManagerStatus.REGISTERING :RegisterServerManagerStatus.UNREGISTERING;
    self._setState(theStatus);
github node-opcua / node-opcua / packages / node-opcua-server / source / server_end_point.ts View on Github external
const establish_connection = () => {

            const nbConnections = Object.keys(this._channels).length;
            debugLog(" nbConnections ", nbConnections, " self._server.maxConnections",
              this._server!.maxConnections, this.maxConnections);
            if (nbConnections >= this.maxConnections) {
                debugLog(chalk.bgWhite.cyan("OPCUAServerEndPoint#_on_client_connection " +
                  "The maximum number of connection has been reached - Connection is refused"));
                socket.end();
                (socket as any).destroy();
                return;
            }

            debugLog("OPCUAServerEndPoint._on_client_connection successful => New Channel");

            const channel = new ServerSecureChannelLayer({
                defaultSecureTokenLifetime: this.defaultSecureTokenLifetime,
                // objectFactory: this.objectFactory,
                parent: this,
                timeout: this.timeout
            });

            socket.resume();

            this._preregisterChannel(channel);

            channel.init(socket, (err?: Error) => {
                this._un_pre_registerChannel(channel);
                debugLog(chalk.yellow.bold("Channel#init done"), err);
                if (err) {
                    socket.end();
                } else {
github node-opcua / node-opcua / packages / node-opcua-server / source / server_end_point.ts View on Github external
const establish_connection = () => {

            const nbConnections = Object.keys(this._channels).length;
            debugLog(" nbConnections ", nbConnections, " self._server.maxConnections",
              this._server!.maxConnections, this.maxConnections);
            if (nbConnections >= this.maxConnections) {
                debugLog(chalk.bgWhite.cyan("OPCUAServerEndPoint#_on_client_connection " +
                  "The maximum number of connection has been reached - Connection is refused"));
                socket.end();
                (socket as any).destroy();
                return;
            }

            debugLog("OPCUAServerEndPoint._on_client_connection successful => New Channel");

            const channel = new ServerSecureChannelLayer({
                defaultSecureTokenLifetime: this.defaultSecureTokenLifetime,
                // objectFactory: this.objectFactory,
                parent: this,
                timeout: this.timeout
            });

            socket.resume();

            this._preregisterChannel(channel);

            channel.init(socket, (err?: Error) => {
                this._un_pre_registerChannel(channel);
                debugLog(chalk.yellow.bold("Channel#init done"), err);
                if (err) {
                    socket.end();
                } else {
github node-opcua / node-opcua / packages / node-opcua-server / source / server_end_point.ts View on Github external
securityMode: MessageSecurityMode,
      securityPolicy: SecurityPolicy,
      options?: EndpointDescriptionParams
    ) {
        if (!options) {
            options = { hostname: getFullyQualifiedDomainName() };
        }

        options.allowAnonymous = (options.allowAnonymous === undefined) ? true : options.allowAnonymous;

        // istanbul ignore next
        if (securityMode === MessageSecurityMode.None && securityPolicy !== SecurityPolicy.None) {
            throw new Error(" invalid security ");
        }
        // istanbul ignore next
        if (securityMode !== MessageSecurityMode.None && securityPolicy === SecurityPolicy.None) {
            throw new Error(" invalid security ");
        }
        //

        const port = this.port;

        // resource Path is a string added at the end of the url such as "/UA/Server"
        const resourcePath = (options.resourcePath || "").replace(/\\/g, "/");

        assert(resourcePath.length === 0 || resourcePath.charAt(0) === "/");

        const hostname = options.hostname || getFullyQualifiedDomainName();
        const endpointUrl = `opc.tcp://${hostname}:${port}${resourcePath}`;

        const endpoint_desc = this.getEndpointDescription(securityMode, securityPolicy, endpointUrl);
github node-opcua / node-opcua / packages / node-opcua-server / source / server_end_point.ts View on Github external
assert(options.hasOwnProperty("serverCertificateChain"));
    assert(!options.hasOwnProperty("serverCertificate"));
    assert(!!options.securityMode); // s.MessageSecurityMode
    assert(!!options.securityPolicy);
    assert(_.isObject(options.server));
    assert(!!options.hostname && (typeof options.hostname === "string"));
    assert(_.isBoolean(options.restricted));

    options.securityLevel = (options.securityLevel === undefined) ? 3 : options.securityLevel;
    assert(_.isFinite(options.securityLevel), "expecting a valid securityLevel");

    const securityPolicyUri = toURI(options.securityPolicy);

    const userIdentityTokens = [];

    if (options.securityPolicy === SecurityPolicy.None) {

        if (options.allowUnsecurePassword) {
            userIdentityTokens.push({
                policyId: "username_unsecure",
                tokenType: UserTokenType.UserName,

                issuedTokenType: null,
                issuerEndpointUrl: null,
                securityPolicyUri: null
            });
        }

        userIdentityTokens.push({
            policyId: "username_basic256",
            tokenType: UserTokenType.UserName,
github node-opcua / node-opcua / packages / node-opcua-server / source / server_end_point.ts View on Github external
securityMode: MessageSecurityMode,
      securityPolicy: SecurityPolicy,
      options?: EndpointDescriptionParams
    ) {
        if (!options) {
            options = { hostname: getFullyQualifiedDomainName() };
        }

        options.allowAnonymous = (options.allowAnonymous === undefined) ? true : options.allowAnonymous;

        // istanbul ignore next
        if (securityMode === MessageSecurityMode.None && securityPolicy !== SecurityPolicy.None) {
            throw new Error(" invalid security ");
        }
        // istanbul ignore next
        if (securityMode !== MessageSecurityMode.None && securityPolicy === SecurityPolicy.None) {
            throw new Error(" invalid security ");
        }
        //

        const port = this.port;

        // resource Path is a string added at the end of the url such as "/UA/Server"
        const resourcePath = (options.resourcePath || "").replace(/\\/g, "/");

        assert(resourcePath.length === 0 || resourcePath.charAt(0) === "/", "resourcePath should start with /");

        const hostname = options.hostname || getFullyQualifiedDomainName();
        const endpointUrl = `opc.tcp://${hostname}:${port}${resourcePath}`;

        const endpoint_desc = this.getEndpointDescription(securityMode, securityPolicy, endpointUrl);
github node-opcua / node-opcua / packages / node-opcua-server / source / server_end_point.ts View on Github external
assert(options.hasOwnProperty("serverCertificateChain"));
    assert(!options.hasOwnProperty("serverCertificate"));
    assert(!!options.securityMode); // s.MessageSecurityMode
    assert(!!options.securityPolicy);
    assert(_.isObject(options.server));
    assert(!!options.hostname && (typeof options.hostname === "string"));
    assert(_.isBoolean(options.restricted));

    options.securityLevel = (options.securityLevel === undefined) ? 3 : options.securityLevel;
    assert(_.isFinite(options.securityLevel), "expecting a valid securityLevel");

    const securityPolicyUri = toURI(options.securityPolicy);

    const userIdentityTokens = [];

    if (options.securityPolicy === SecurityPolicy.None) {

        if (options.allowUnsecurePassword) {
            userIdentityTokens.push({
                policyId: "username_unsecure",
                tokenType: UserTokenType.UserName,

                issuedTokenType: null,
                issuerEndpointUrl: null,
                securityPolicyUri: null
            });
        }

        userIdentityTokens.push({
            policyId: "username_basic256",
            tokenType: UserTokenType.UserName,
github node-opcua / node-opcua / packages / node-opcua-server / source / server_end_point.ts View on Github external
public addEndpointDescription(
      securityMode: MessageSecurityMode,
      securityPolicy: SecurityPolicy,
      options?: EndpointDescriptionParams
    ) {
        if (!options) {
            options = { hostname: getFullyQualifiedDomainName() };
        }

        options.allowAnonymous = (options.allowAnonymous === undefined) ? true : options.allowAnonymous;

        // istanbul ignore next
        if (securityMode === MessageSecurityMode.None && securityPolicy !== SecurityPolicy.None) {
            throw new Error(" invalid security ");
        }
        // istanbul ignore next
        if (securityMode !== MessageSecurityMode.None && securityPolicy === SecurityPolicy.None) {
            throw new Error(" invalid security ");
        }
        //

        const port = this.port;

        // resource Path is a string added at the end of the url such as "/UA/Server"
        const resourcePath = (options.resourcePath || "").replace(/\\/g, "/");

        assert(resourcePath.length === 0 || resourcePath.charAt(0) === "/", "resourcePath should start with /");

        const hostname = options.hostname || getFullyQualifiedDomainName();
github node-opcua / node-opcua / packages / node-opcua-server / source / server_end_point.ts View on Github external
public addEndpointDescription(
      securityMode: MessageSecurityMode,
      securityPolicy: SecurityPolicy,
      options?: EndpointDescriptionParams
    ) {
        if (!options) {
            options = { hostname: getFullyQualifiedDomainName() };
        }

        options.allowAnonymous = (options.allowAnonymous === undefined) ? true : options.allowAnonymous;

        // istanbul ignore next
        if (securityMode === MessageSecurityMode.None && securityPolicy !== SecurityPolicy.None) {
            throw new Error(" invalid security ");
        }
        // istanbul ignore next
        if (securityMode !== MessageSecurityMode.None && securityPolicy === SecurityPolicy.None) {
            throw new Error(" invalid security ");
        }
        //

        const port = this.port;

        // resource Path is a string added at the end of the url such as "/UA/Server"
        const resourcePath = (options.resourcePath || "").replace(/\\/g, "/");

        assert(resourcePath.length === 0 || resourcePath.charAt(0) === "/");

        const hostname = options.hostname || getFullyQualifiedDomainName();