Authenticating with a User and Password

For this example, start the server using:

  1. > nats-server --user myname --pass password

You can encrypt passwords to pass to nats-server using a simple tool:

  1. > nats server passwd
  2. ? Enter password [? for help] **********************
  3. ? Reenter password [? for help] **********************
  4. $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.

Connecting with a User/Password

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

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

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();

JavaScript

  1. let nc = NATS.connect({url: server.nats, user: "myname", pass: "password"});

Python

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

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

TypeScript

  1. let nc = await connect({url: server.nats, user: "myname", pass: "password"});

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);

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:

nats://user:password@server:port

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

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

Java

  1. Connection nc = Nats.connect("nats://myname:password@localhost:4222");
  2. // Do something with the connection
  3. nc.close();

JavaScript

  1. let url = `nats://myname:password@127.0.0.1:${port}`;
  2. let nc = NATS.connect(url);

Python

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

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

TypeScript

  1. let url = `nats://myname:password@127.0.0.1:${port}`;
  2. let nc = await connect({url: url});

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. natsConnection_Destroy(conn);
  12. natsOptions_Destroy(opts);