Authenticating with a Credentials File

The 2.0 version of NATS server introduced the idea of decentralized authentication based on JSON Web Tokens (JWT). Clients interact with this new scheme using a user JWT and corresponding NKey private key. To help make connecting with a JWT easier, the client libraries support the concept of a credentials file. This file contains both the private key and the JWT and can be generated with the nsc tool. The contents will look like the following and should be protected because it contains a private key. This credentials file is unused and only for example purposes.

  1. -----BEGIN NATS USER JWT-----
  2. eyJ0eXAiOiJqd3QiLCJhbGciOiJlZDI1NTE5In0.eyJqdGkiOiJUVlNNTEtTWkJBN01VWDNYQUxNUVQzTjRISUw1UkZGQU9YNUtaUFhEU0oyWlAzNkVMNVJBIiwiaWF0IjoxNTU4MDQ1NTYyLCJpc3MiOiJBQlZTQk0zVTQ1REdZRVVFQ0tYUVM3QkVOSFdHN0tGUVVEUlRFSEFKQVNPUlBWV0JaNEhPSUtDSCIsIm5hbWUiOiJvbWVnYSIsInN1YiI6IlVEWEIyVk1MWFBBU0FKN1pEVEtZTlE3UU9DRldTR0I0Rk9NWVFRMjVIUVdTQUY3WlFKRUJTUVNXIiwidHlwZSI6InVzZXIiLCJuYXRzIjp7InB1YiI6e30sInN1YiI6e319fQ.6TQ2ilCDb6m2ZDiJuj_D_OePGXFyN3Ap2DEm3ipcU5AhrWrNvneJryWrpgi_yuVWKo1UoD5s8bxlmwypWVGFAA
  3. ------END NATS USER JWT------
  4. ************************* IMPORTANT *************************
  5. NKEY Seed printed below can be used to sign and prove identity.
  6. NKEYs are sensitive and should be treated as secrets.
  7. -----BEGIN USER NKEY SEED-----
  8. SUAOY5JZ2WJKVR4UO2KJ2P3SW6FZFNWEOIMAXF4WZEUNVQXXUOKGM55CYE
  9. ------END USER NKEY SEED------
  10. *************************************************************

Given a creds file, a client can authenticate as a specific user belonging to a specific account:

Go

  1. nc, err := nats.Connect("127.0.0.1", nats.UserCredentials("path_to_creds_file"))
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. defer nc.Close()
  6. // Do something with the connection

Java

  1. Options options = new Options.Builder().
  2. server("nats://localhost:4222").
  3. authHandler(Nats.credentials("path_to_creds_file")).
  4. build();
  5. Connection nc = Nats.connect(options);
  6. // Do something with the connection
  7. nc.close();

JavaScript

  1. // credentials file contains the JWT and the secret signing key
  2. const authenticator = credsAuthenticator(creds);
  3. const nc = await connect({
  4. port: ns.port,
  5. authenticator: authenticator,
  6. });

Python

  1. nc = NATS()
  2. async def error_cb(e):
  3. print("Error:", e)
  4. await nc.connect("nats://localhost:4222",
  5. user_credentials="path_to_creds_file",
  6. error_cb=error_cb,
  7. )
  8. # Do something with the connection
  9. await nc.close()

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. // Pass the credential file this way if the file contains both user JWT and seed.
  7. // Otherwise, if the content is split, the first file is the user JWT, the second
  8. // contains the seed.
  9. s = natsOptions_SetUserCredentialsFromFiles(opts, "path_to_creds_file", NULL);
  10. if (s == NATS_OK)
  11. s = natsConnection_Connect(&conn, opts);
  12. (...)
  13. // Destroy objects that were created
  14. natsConnection_Destroy(conn);
  15. natsOptions_Destroy(opts);

{% endtabs %}