From ff6f34639d7b85e81f2bec429c552429bf2e886f Mon Sep 17 00:00:00 2001 From: azelcs Date: Wed, 15 Feb 2023 15:28:49 +0200 Subject: [PATCH 1/2] Connection with username and password --- ext/opcua_client/opcua_client.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ext/opcua_client/opcua_client.c b/ext/opcua_client/opcua_client.c index b3ef823..23903d7 100644 --- a/ext/opcua_client/opcua_client.c +++ b/ext/opcua_client/opcua_client.c @@ -197,6 +197,32 @@ static VALUE rb_connect(VALUE self, VALUE v_connectionString) { } } +static VALUE rb_connect_username(VALUE self, VALUE v_connectionString, VALUE v_username, VALUE v_password) { + if ( + RB_TYPE_P(v_connectionString, T_STRING) != 1 || + RB_TYPE_P(v_username, T_STRING) != 1 || + RB_TYPE_P(v_password, T_STRING) != 1 + ) { + return raise_invalid_arguments_error(); + } + + char *connectionString = StringValueCStr(v_connectionString); + char *username = StringValueCStr(v_username); + char *password = StringValueCStr(v_password); + + struct UninitializedClient * uclient; + TypedData_Get_Struct(self, struct UninitializedClient, &UA_Client_Type, uclient); + UA_Client *client = uclient->client; + + UA_StatusCode status = UA_Client_connect_username(client, connectionString, username, password); + + if (status == UA_STATUSCODE_GOOD) { + return Qnil; + } else { + return raise_ua_status_error(status); + } +} + static VALUE rb_createSubscription(VALUE self) { struct UninitializedClient * uclient; TypedData_Get_Struct(self, struct UninitializedClient, &UA_Client_Type, uclient); @@ -839,6 +865,7 @@ void Init_opcua_client() rb_define_method(cClient, "do_mon_cycle!", rb_run_single_monitoring_cycle_bang, 0); rb_define_method(cClient, "connect", rb_connect, 1); + rb_define_method(cClient, "connect_username", rb_connect_username, 3); rb_define_method(cClient, "disconnect", rb_disconnect, 0); rb_define_method(cClient, "state", rb_state, 0); From 6c69fdb40928979cfc65505aa9bc7e74a4fa2c94 Mon Sep 17 00:00:00 2001 From: azelcs Date: Wed, 15 Feb 2023 15:31:10 +0200 Subject: [PATCH 2/2] Update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9b9e5e2..13ea885 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ end ### Available methods - connection: * ```client.connect(String url)``` - raises OPCUAClient::Error if unsuccessful +* ```client.connect_username(String url, String username, String password)``` - raises OPCUAClient::Error if unsuccessful * ```client.disconnect => Fixnum``` - returns status ### Available methods - reads and writes: