Authenticating with an NKey

The 2.0 version of NATS server introduces a new challenge response authentication option. This challenge response is based on a wrapper we call NKeys. The server can use these keys in several ways for authentication. The simplest is for the server to be configured with a list of known public keys and for the clients to respond to the challenge by signing it with its private key. (A printable private NKey is referred to as seed). This challenge-response ensures security by ensuring that the client has the private key, but also protects the private key from the server, which never has access to it!

Handling challenge response may require more than just a setting in the connection options, depending on the client library.

Go

  1. opt, err := nats.NkeyOptionFromSeed("seed.txt")
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. nc, err := nats.Connect("127.0.0.1", opt)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. defer nc.Close()
  10. // Do something with the connection

Java

  1. NKey theNKey = NKey.createUser(null); // really should load from somewhere
  2. Options options = new Options.Builder().
  3. server("nats://localhost:4222").
  4. authHandler(new AuthHandler(){
  5. public char[] getID() {
  6. try {
  7. return theNKey.getPublicKey();
  8. } catch (GeneralSecurityException|IOException|NullPointerException ex) {
  9. return null;
  10. }
  11. }
  12. public byte[] sign(byte[] nonce) {
  13. try {
  14. return theNKey.sign(nonce);
  15. } catch (GeneralSecurityException|IOException|NullPointerException ex) {
  16. return null;
  17. }
  18. }
  19. public char[] getJWT() {
  20. return null;
  21. }
  22. }).
  23. build();
  24. Connection nc = Nats.connect(options);
  25. // Do something with the connection
  26. nc.close();

JavaScript

  1. // seed should be stored and treated like a secret
  2. const seed = new TextEncoder().encode(
  3. "SUAEL6GG2L2HIF7DUGZJGMRUFKXELGGYFMHF76UO2AYBG3K4YLWR3FKC2Q",
  4. );
  5. const nc = await connect({
  6. port: ns.port,
  7. authenticator: nkeyAuthenticator(seed),
  8. });

Python

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

C

  1. static natsStatus
  2. sigHandler(
  3. char **customErrTxt,
  4. unsigned char **signature,
  5. int *signatureLength,
  6. const char *nonce,
  7. void *closure)
  8. {
  9. // Sign the given `nonce` and return the signature as `signature`.
  10. // This needs to allocate memory. The length of the signature is
  11. // returned as `signatureLength`.
  12. // If an error occurs the user can return specific error text through
  13. // `customErrTxt`. The library will free this pointer.
  14. return NATS_OK;
  15. }
  16. (...)
  17. natsConnection *conn = NULL;
  18. natsOptions *opts = NULL;
  19. natsStatus s = NATS_OK;
  20. const char *pubKey = "my public key......";
  21. s = natsOptions_Create(&opts);
  22. if (s == NATS_OK)
  23. s = natsOptions_SetNKey(opts, pubKey, sigHandler, NULL);
  24. if (s == NATS_OK)
  25. s = natsConnection_Connect(&conn, opts);
  26. (...)
  27. // Destroy objects that were created
  28. natsConnection_Destroy(conn);
  29. natsOptions_Destroy(opts);

{% endtabs %}