nauful / LibUA

Open-source OPC UA client and server library
Apache License 2.0
262 stars 94 forks source link

Can't connect if server require plaintext password #79

Closed ne-fas-tus closed 2 years ago

ne-fas-tus commented 2 years ago

hello, I state that I have just started to study the operation of the OPC-UA architecture and that I am not an expert programmer, what I would like to do is communicate with an OPC-UA server of a Modicon M241 PLC from an application in Unity3D. In order to activate session into the server I had to make a change to your library to make a username/password connection but with a plaintext password (without encryption). The PLC server is not otherwise configurable. Among the methods in your library there is no such choice

The new class is:

public class UserIdentityUsernamePlainPasswordToken
        {
            public string PolicyId { get; protected set; }
            public string Username { get; protected set; }
            public string Password { get; protected set; }

            public UserIdentityUsernamePlainPasswordToken(string PolicyId, string Username, string Password)
            {
                this.PolicyId = PolicyId;
                this.Username = Username;
                this.Password = Password;
            }
        }

inside ActivateSession method:

...
if (identityToken is UserIdentityUsernamePlainPasswordToken)
{
    succeeded &= sendBuf.Encode(new NodeId(UAConst.UserNameIdentityToken_Encoding_DefaultBinary));
    succeeded &= sendBuf.Encode((byte)1);
    int eoStartPos = sendBuf.Position;
    succeeded &= sendBuf.Encode((UInt32)0);
    succeeded &= sendBuf.EncodeUAString(((identityToken as UserIdentityUsernamePlainPasswordToken)).PolicyId);
    succeeded &= sendBuf.EncodeUAString(((identityToken as UserIdentityUsernamePlainPasswordToken)).Username);
    succeeded &= sendBuf.EncodeUAString(((identityToken as UserIdentityUsernamePlainPasswordToken)).Password);
    succeeded &= sendBuf.EncodeUAString((string)null);
    succeeded &= sendBuf.Encode((UInt32)(sendBuf.Position - eoStartPos - 4), eoStartPos);
}
...

and using method:

client.ActivateSession(new UserIdentityUsernamePlainPasswordToken(usernamePolicyDesc, "USER", "password"),new[] { "en" });
nauful commented 2 years ago

Great, thanks. I'll integrate this the next time I work on this area.