Skip to content

Commit 476bea7

Browse files
committed
add UserInfoHandler for dynamically fetching username+password
1 parent 33316cd commit 476bea7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

nats.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ var (
131131
ErrNkeysNotSupported = errors.New("nats: nkeys not supported by the server")
132132
ErrStaleConnection = errors.New("nats: " + STALE_CONNECTION)
133133
ErrTokenAlreadySet = errors.New("nats: token and token handler both set")
134+
ErrUserInfoAlreadySet = errors.New("nats: user info and user info handler both set")
134135
ErrMsgNotBound = errors.New("nats: message is not bound to subscription/connection")
135136
ErrMsgNoReply = errors.New("nats: message does not have a reply")
136137
ErrClientIPNotSupported = errors.New("nats: client IP not supported by this server")
@@ -230,6 +231,9 @@ type SignatureHandler func([]byte) ([]byte, error)
230231
// AuthTokenHandler is used to generate a new token.
231232
type AuthTokenHandler func() string
232233

234+
// AuthUserInfoHandler is used to fetch username and password.
235+
type AuthUserInfoHandler func() (string, string)
236+
233237
// ReconnectDelayHandler is used to get from the user the desired
234238
// delay the library should pause before attempting to reconnect
235239
// again. Note that this is invoked after the library tried the
@@ -443,6 +447,10 @@ type Options struct {
443447
// Password sets the password to be used when connecting to a server.
444448
Password string
445449

450+
// UserInfoHandler designates the function used to fetch the username and
451+
// password to be used when connecting to a server.
452+
UserInfoHandler AuthUserInfoHandler
453+
446454
// Token sets the token to be used when connecting to a server.
447455
Token string
448456

@@ -1166,6 +1174,15 @@ func UserInfo(user, password string) Option {
11661174
}
11671175
}
11681176

1177+
// UserInfoHandler is an Option to set the user info handler to use when a
1178+
// username and password is not otherwise specified.
1179+
func UserInfoHandler(cb AuthUserInfoHandler) Option {
1180+
return func(o *Options) error {
1181+
o.UserInfoHandler = cb
1182+
return nil
1183+
}
1184+
}
1185+
11691186
// Token is an Option to set the token to use
11701187
// when a token is not included directly in the URLs
11711188
// and when a token handler is not provided.
@@ -2557,6 +2574,13 @@ func (nc *Conn) connectProto() (string, error) {
25572574
token = nc.Opts.TokenHandler()
25582575
}
25592576

2577+
if nc.Opts.UserInfoHandler != nil {
2578+
if user != _EMPTY_ || pass != _EMPTY_ {
2579+
return _EMPTY_, ErrUserInfoAlreadySet
2580+
}
2581+
user, pass = nc.Opts.UserInfoHandler()
2582+
}
2583+
25602584
// If our server does not support headers then we can't do them or no responders.
25612585
hdrs := nc.info.Headers
25622586
cinfo := connectInfo{o.Verbose, o.Pedantic, ujwt, nkey, sig, user, pass, token,

0 commit comments

Comments
 (0)