Authenticating with a Token

Tokens are basically random strings, much like a password, and can provide a simple authentication mechanism in some situations. However, tokens are only as safe as they are secret so other authentication schemes can provide more security in large installations. It is highly recommended to use one of the other NATS authentication mechanisms.

For this example, start the server using:

  1. nats-server --auth mytoken

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

Connecting with a Token

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

  1. // Set a token
  2. nc, err := nats.Connect("127.0.0.1", nats.Name("API Token Example"), nats.Token("mytoken"))
  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. token("mytoken"). // Set a token
  4. build();
  5. Connection nc = Nats.connect(options);
  6. // Do something with the connection
  7. nc.close();

{% endtab %}

{% tab title=”JavaScript” %}

  1. const nc = await connect({
  2. port: ns.port,
  3. token: "aToK3n",
  4. });

{% endtab %}

{% tab title=”Python” %}

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

{% endtab %}

{% tab title=”Ruby” %}

  1. NATS.start(token: "mytoken") do |nc|
  2. puts "Connected using token"
  3. 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_SetToken(opts, "mytoken");
  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 Token in the URL

Some client libraries will allow you to pass the token as part of the server URL using the form:

nats://token@server:port

Again, once you construct this URL you can connect as if this was a normal URL.

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

  1. // Token in URL
  2. nc, err := nats.Connect("mytoken@localhost")
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. defer nc.Close()
  7. // Do something with the connection

{% endtab %}

{% tab title=”Java” %}

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

{% endtab %}

{% tab title=”JavaScript” %}

  1. // JavaScript doesn't support tokens in urls use the `token` option

{% endtab %}

{% tab title=”Python” %}

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

{% endtab %}

{% tab title=”Ruby” %}

  1. NATS.start("mytoken@127.0.0.1:4222") do |nc|
  2. puts "Connected using token!"
  3. 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://mytoken@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);

{% endtab %} {% endtabs %}