Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt to mongodb v5 #4

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ type saslCmd struct {
Payload []byte
}

type saslStartCmd struct {
Start int `bson:"saslStart,omitempty"`
Mechanism string `bson:"mechanism,omitempty"`
Payload []byte
}

type saslContinueCmd struct {
Continue int `bson:"saslContinue,omitempty"`
ConversationId int `bson:"conversationId,omitempty"`
Payload []byte
}

type saslResult struct {
Ok bool `bson:"ok"`
NotOk bool `bson:"code"` // Server <= 2.3.2 returns ok=1 & code>0 on errors (WTF?)
Expand Down Expand Up @@ -309,9 +321,30 @@ func (socket *mongoSocket) loginSASL(cred Credential) error {
lock(true)
defer lock(false)

start := 1
cmd := saslCmd{}
startStep, _, err := sasl.Step([]byte{})
if err != nil {
return err
}

lock(false)
res := saslResult{}
startCmd := saslStartCmd{
Start: 1,
Mechanism: cred.Mechanism,
Payload: startStep,
}
err = socket.loginRun(cred.Source, &startCmd, &res, func() error {
// See the comment on lock for why this is necessary.
lock(true)
if !res.Ok || res.NotOk {
return fmt.Errorf("server returned error on SASL authentication step: %s", res.ErrMsg)
}
return nil
})
if err != nil {
return err
}
cmd := saslContinueCmd{}
for {
payload, done, err := sasl.Step(res.Payload)
if err != nil {
Expand All @@ -324,14 +357,11 @@ func (socket *mongoSocket) loginSASL(cred Credential) error {
}
lock(false)

cmd = saslCmd{
Start: start,
Continue: 1 - start,
cmd = saslContinueCmd{
Continue: 1,
ConversationId: res.ConversationId,
Mechanism: cred.Mechanism,
Payload: payload,
}
start = 0
err = socket.loginRun(cred.Source, &cmd, &res, func() error {
// See the comment on lock for why this is necessary.
lock(true)
Expand Down