@@ -34,8 +34,8 @@ import (
3434 "fmt"
3535 "sync"
3636
37- "github.com/globalsign /mgo/bson"
38- "github.com/globalsign /mgo/internal/scram"
37+ "github.com/libi /mgo/bson"
38+ "github.com/libi /mgo/internal/scram"
3939)
4040
4141type authCmd struct {
@@ -77,6 +77,18 @@ type saslCmd struct {
7777 Payload []byte
7878}
7979
80+ type saslStartCmd struct {
81+ Start int `bson:"saslStart,omitempty"`
82+ Mechanism string `bson:"mechanism,omitempty"`
83+ Payload []byte
84+ }
85+
86+ type saslContinueCmd struct {
87+ Continue int `bson:"saslContinue,omitempty"`
88+ ConversationId int `bson:"conversationId,omitempty"`
89+ Payload []byte
90+ }
91+
8092type saslResult struct {
8193 Ok bool `bson:"ok"`
8294 NotOk bool `bson:"code"` // Server <= 2.3.2 returns ok=1 & code>0 on errors (WTF?)
@@ -309,9 +321,30 @@ func (socket *mongoSocket) loginSASL(cred Credential) error {
309321 lock (true )
310322 defer lock (false )
311323
312- start := 1
313- cmd := saslCmd {}
324+ startStep , _ , err := sasl .Step ([]byte {})
325+ if err != nil {
326+ return err
327+ }
328+
329+ lock (false )
314330 res := saslResult {}
331+ startCmd := saslStartCmd {
332+ Start : 1 ,
333+ Mechanism : cred .Mechanism ,
334+ Payload : startStep ,
335+ }
336+ err = socket .loginRun (cred .Source , & startCmd , & res , func () error {
337+ // See the comment on lock for why this is necessary.
338+ lock (true )
339+ if ! res .Ok || res .NotOk {
340+ return fmt .Errorf ("server returned error on SASL authentication step: %s" , res .ErrMsg )
341+ }
342+ return nil
343+ })
344+ if err != nil {
345+ return err
346+ }
347+ cmd := saslContinueCmd {}
315348 for {
316349 payload , done , err := sasl .Step (res .Payload )
317350 if err != nil {
@@ -324,14 +357,11 @@ func (socket *mongoSocket) loginSASL(cred Credential) error {
324357 }
325358 lock (false )
326359
327- cmd = saslCmd {
328- Start : start ,
329- Continue : 1 - start ,
360+ cmd = saslContinueCmd {
361+ Continue : 1 ,
330362 ConversationId : res .ConversationId ,
331- Mechanism : cred .Mechanism ,
332363 Payload : payload ,
333364 }
334- start = 0
335365 err = socket .loginRun (cred .Source , & cmd , & res , func () error {
336366 // See the comment on lock for why this is necessary.
337367 lock (true )
0 commit comments