diff --git a/client.go b/client.go index 45b28c7..5f821e0 100644 --- a/client.go +++ b/client.go @@ -178,6 +178,8 @@ func toClientOptions(c *Client, o *clientOptions) *mqtt.ClientOptions { opts.SetClientID(o.clientID) } + setCredentials(o, opts) + opts.AddBroker(formatAddressWithProtocol(o)). SetUsername(o.username). SetPassword(o.password). @@ -195,6 +197,23 @@ func toClientOptions(c *Client, o *clientOptions) *mqtt.ClientOptions { return opts } +func setCredentials(o *clientOptions, opts *mqtt.ClientOptions) { + if o.credentialFetcher != nil { + ctx, cancel := context.WithTimeout(context.Background(), o.credentialFetchTimeout) + defer cancel() + + if c, err := o.credentialFetcher.Credentials(ctx); err == nil { + opts.SetUsername(c.Username) + opts.SetPassword(c.Password) + + return + } + } + + opts.SetUsername(o.username) + opts.SetPassword(o.password) +} + func formatAddressWithProtocol(opts *clientOptions) string { if opts.tlsConfig != nil { return fmt.Sprintf("tls://%s", opts.brokerAddress)