From ac82881f593828156c093fbd78bd9c85d69a8a75 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Fri, 15 Dec 2023 10:47:49 +0100 Subject: [PATCH] feat(events/natjs): Allow username/password authentication Signed-off-by: jkoberg --- v4/events/natsjs/nats.go | 5 +++++ v4/events/natsjs/options.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/v4/events/natsjs/nats.go b/v4/events/natsjs/nats.go index 40f0179d..3deadfde 100644 --- a/v4/events/natsjs/nats.go +++ b/v4/events/natsjs/nats.go @@ -69,6 +69,11 @@ func connectToNatsJetStream(options Options) (nats.JetStreamContext, error) { nopts.Name = options.Name } + if options.Username != "" && options.Password != "" { + nopts.User = options.Username + nopts.Password = options.Password + } + conn, err := nopts.Connect() if err != nil { tls := nopts.TLSConfig != nil diff --git a/v4/events/natsjs/options.go b/v4/events/natsjs/options.go index 2ace150b..f7e83d5f 100644 --- a/v4/events/natsjs/options.go +++ b/v4/events/natsjs/options.go @@ -17,6 +17,8 @@ type Options struct { SyncPublish bool Name string DisableDurableStreams bool + Username string + Password string } // Option is a function which configures options. @@ -84,3 +86,11 @@ func DisableDurableStreams() Option { o.DisableDurableStreams = true } } + +// Authenticate authenticates the connection with the given username and password. +func Authenticate(username, password string) Option { + return func(o *Options) { + o.Username = username + o.Password = password + } +}