Authenticating with a User and Password

    You can encrypt passwords to pass to using a simple tool:

    1. nats server passwd

    Output

    1. ? Enter password [? for help] **********************
    2. ? Reenter password [? for help] **********************
    3. $2a$11$qbtrnb0mSG2eV55xoyPqHOZx/lLBlryHRhU3LK2oOPFRwGF/5rtGK

    and use the hashed password in the server config. The client still uses the plain text version.

    The code uses localhost:4222 so that you can start the server on your machine to try them out.

    When logging in with a password nats-server will take either a plain text password or an encrypted password.

    {% tabs %} {% tab title=”Go” %}

    1. // Set a user and plain text password
    2. nc, err := nats.Connect("127.0.0.1", nats.UserInfo("myname", "password"))
    3. if err != nil {
    4. log.Fatal(err)
    5. }
    6. defer nc.Close()
    7. // Do something with the connection

    {% endtab %}

    {% tab title=”Java” %}

    1. Options options = new Options.Builder().
    2. server("nats://localhost:4222").
    3. userInfo("myname","password"). // Set a user and plain text password
    4. build();
    5. Connection nc = Nats.connect(options);
    6. // Do something with the connection
    7. nc.close();

    {% endtab %}

    {% tab title=”JavaScript” %}

    {% tab title=”Python” %}

    1. nc = NATS()
    2. await nc.connect(servers=["nats://myname:password@demo.nats.io:4222"])
    3. # Do something with the connection.

    {% endtab %}

    {% tab title=”Ruby” %}

    1. require 'nats/client'
    2. NATS.start(servers:["nats://myname:password@127.0.0.1:4222"], name: "my-connection") do |nc|
    3. nc.on_error do |e|
    4. puts "Error: #{e}"
    5. end
    6. nc.on_reconnect do
    7. puts "Got reconnected to #{nc.connected_server}"
    8. end
    9. nc.on_disconnect do |reason|
    10. puts "Got disconnected! #{reason}"
    11. end
    12. nc.close
    13. end

    {% endtab %}

    {% tab title=”C” %}

    1. natsConnection *conn = NULL;
    2. natsOptions *opts = NULL;
    3. natsStatus s = NATS_OK;
    4. s = natsOptions_Create(&opts);
    5. if (s == NATS_OK)
    6. s = natsOptions_SetUserInfo(opts, "myname", "password");
    7. if (s == NATS_OK)
    8. s = natsConnection_Connect(&conn, opts);
    9. (...)
    10. // Destroy objects that were created
    11. natsConnection_Destroy(conn);
    12. natsOptions_Destroy(opts);

    {% endtab %} {% endtabs %}

    Connecting with a User/Password in the URL

    Most clients make it easy to pass the user name and password by accepting them in the URL for the server. This standard format is:

    Using this format, you can connect to a server using authentication as easily as you connected with a URL:

    {% tabs %} {% tab title=”Go” %}

    1. // Set a user and plain text password
    2. nc, err := nats.Connect("myname:password@127.0.0.1")
    3. if err != nil {
    4. log.Fatal(err)
    5. }
    6. defer nc.Close()
    7. // Do something with the connection

    {% tab title=”Java” %}

    {% endtab %}

    {% tab title=”JavaScript” %}

    1. // JavaScript clients don't support username/password in urls use `user` and `pass` options.

    {% endtab %}

    {% tab title=”Python” %}

    1. await nc.connect(servers=["nats://myname:password@demo.nats.io:4222"])
    2. # Do something with the connection.

    {% endtab %}

    {% tab title=”Ruby” %}

    1. require 'nats/client'
    2. NATS.start(servers:["nats://myname:password@127.0.0.1:4222"], name: "my-connection") do |nc|
    3. nc.on_error do |e|
    4. puts "Error: #{e}"
    5. end
    6. nc.on_reconnect do
    7. puts "Got reconnected to #{nc.connected_server}"
    8. end
    9. nc.on_disconnect do |reason|
    10. puts "Got disconnected! #{reason}"
    11. end
    12. nc.close
    13. end

    {% endtab %}

    {% tab title=”C” %}

    1. natsConnection *conn = NULL;
    2. natsOptions *opts = NULL;
    3. natsStatus s = NATS_OK;
    4. s = natsOptions_Create(&opts);
    5. if (s == NATS_OK)
    6. s = natsOptions_SetURL(opts, "nats://myname:password@127.0.0.1:4222");
    7. if (s == NATS_OK)
    8. s = natsConnection_Connect(&conn, opts);
    9. (...)
    10. // Destroy objects that were created
    11. natsOptions_Destroy(opts);

    {% endtab %} {% endtabs %}