Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UserJWTAndSeed helper function #1046

Merged
merged 1 commit into from Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions nats.go
Expand Up @@ -1083,6 +1083,28 @@ func UserCredentials(userOrChainedFile string, seedFiles ...string) Option {
return UserJWT(userCB, sigCB)
}

// UserJWTAndSeed is a convenience function that takes the JWT and seed
// values as strings.
func UserJWTAndSeed(jwt string, seed string) Option {
userCB := func() (string, error) {
return jwt, nil
}

sigCB := func(nonce []byte) ([]byte, error) {
kp, err := nkeys.FromSeed([]byte(seed))
if err != nil {
return nil, fmt.Errorf("unable to extract key pair from seed: %v", err)
}
// Wipe our key on exit.
defer kp.Wipe()

sig, _ := kp.Sign(nonce)
return sig, nil
}

return UserJWT(userCB, sigCB)
}

// UserJWT will set the callbacks to retrieve the user's JWT and
// the signature callback to sign the server nonce. This an the Nkey
// option are mutually exclusive.
Expand Down
15 changes: 15 additions & 0 deletions nats_test.go
Expand Up @@ -1576,6 +1576,21 @@ func TestUserCredentialsChainedFile(t *testing.T) {
}
}

func TestUserJWTAndSeed(t *testing.T) {
if server.VERSION[0] == '1' {
t.Skip()
}
ts := runTrustServer()
defer ts.Shutdown()

url := fmt.Sprintf("nats://127.0.0.1:%d", TEST_PORT)
nc, err := Connect(url, UserJWTAndSeed(uJWT, string(uSeed)))
if err != nil {
t.Fatalf("Expected to connect, got %v", err)
}
nc.Close()
}

func TestExpiredAuthentication(t *testing.T) {
// The goal of these tests was to check how a client with an expiring JWT
// behaves. It should receive an async -ERR indicating that the auth
Expand Down