Skip to content

Commit 36d01c2

Browse files
authored
Merge pull request #192 from vaelen/master
Add a way to set the username and password via callback.
2 parents b01d90b + b3742dc commit 36d01c2

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

message.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,19 @@ func newConnectMsgFromOptions(options *ClientOptions) *packets.ConnectPacket {
8888
m.WillMessage = options.WillPayload
8989
}
9090

91-
if options.Username != "" {
91+
username := options.Username
92+
password := options.Password
93+
if options.CredentialsProvider != nil {
94+
username, password = options.CredentialsProvider()
95+
}
96+
97+
if username != "" {
9298
m.UsernameFlag = true
93-
m.Username = options.Username
99+
m.Username = username
94100
//mustn't have password without user as well
95-
if options.Password != "" {
101+
if password != "" {
96102
m.PasswordFlag = true
97-
m.Password = []byte(options.Password)
103+
m.Password = []byte(password)
98104
}
99105
}
100106

options.go

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
"time"
2121
)
2222

23+
// CredentialsProvider allows the username and password to be updated
24+
// before reconnecting. It should return the current username and password.
25+
type CredentialsProvider func() (username string, password string)
26+
2327
// MessageHandler is a callback type which can be set to be
2428
// executed upon the arrival of messages published to topics
2529
// to which the client is subscribed.
@@ -42,6 +46,7 @@ type ClientOptions struct {
4246
ClientID string
4347
Username string
4448
Password string
49+
CredentialsProvider CredentialsProvider
4550
CleanSession bool
4651
Order bool
4752
WillEnabled bool
@@ -142,6 +147,15 @@ func (o *ClientOptions) SetPassword(p string) *ClientOptions {
142147
return o
143148
}
144149

150+
// SetCredentialsProvider will set a method to be called by this client when
151+
// connecting to the MQTT broker that provide the current username and password.
152+
// Note: without the use of SSL/TLS, this information will be sent
153+
// in plaintext accross the wire.
154+
func (o *ClientOptions) SetCredentialsProvider(p CredentialsProvider) *ClientOptions {
155+
o.CredentialsProvider = p
156+
return o
157+
}
158+
145159
// SetCleanSession will set the "clean session" flag in the connect message
146160
// when this client connects to an MQTT broker. By setting this flag, you are
147161
// indicating that no messages saved by the broker for this client should be

unit_message_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2013 IBM Corp.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/legal/epl-v10.html
8+
*
9+
* Contributors:
10+
* Andrew Young
11+
*/
12+
13+
package mqtt
14+
15+
import (
16+
"testing"
17+
)
18+
19+
func Test_UsernamePassword(t *testing.T) {
20+
options := NewClientOptions()
21+
options.Username = "username"
22+
options.Password = "password"
23+
24+
m := newConnectMsgFromOptions(options)
25+
26+
if m.Username != "username" {
27+
t.Fatalf("Username not set correctly")
28+
}
29+
30+
if string(m.Password) != "password" {
31+
t.Fatalf("Password not set correctly")
32+
}
33+
}
34+
35+
func Test_CredentialsProvider(t *testing.T) {
36+
options := NewClientOptions()
37+
options.Username = "incorrect"
38+
options.Password = "incorrect"
39+
options.SetCredentialsProvider(func() (username string, password string) {
40+
return "username", "password"
41+
})
42+
43+
m := newConnectMsgFromOptions(options)
44+
45+
if m.Username != "username" {
46+
t.Fatalf("Username not set correctly")
47+
}
48+
49+
if string(m.Password) != "password" {
50+
t.Fatalf("Password not set correctly")
51+
}
52+
}

0 commit comments

Comments
 (0)