blob: 3f66aee294095dd197e9b4d7dcacb1af6c902c01 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package auth
8
9import (
10 "context"
11
12 "github.com/mongodb/mongo-go-driver/x/network/description"
13 "github.com/mongodb/mongo-go-driver/x/network/wiremessage"
14)
15
16// PLAIN is the mechanism name for PLAIN.
17const PLAIN = "PLAIN"
18
19func newPlainAuthenticator(cred *Cred) (Authenticator, error) {
20 return &PlainAuthenticator{
21 Username: cred.Username,
22 Password: cred.Password,
23 }, nil
24}
25
26// PlainAuthenticator uses the PLAIN algorithm over SASL to authenticate a connection.
27type PlainAuthenticator struct {
28 Username string
29 Password string
30}
31
32// Auth authenticates the connection.
33func (a *PlainAuthenticator) Auth(ctx context.Context, desc description.Server, rw wiremessage.ReadWriter) error {
34 return ConductSaslConversation(ctx, desc, rw, "$external", &plainSaslClient{
35 username: a.Username,
36 password: a.Password,
37 })
38}
39
40type plainSaslClient struct {
41 username string
42 password string
43}
44
45func (c *plainSaslClient) Start() (string, []byte, error) {
46 b := []byte("\x00" + c.username + "\x00" + c.password)
47 return PLAIN, b, nil
48}
49
50func (c *plainSaslClient) Next(challenge []byte) ([]byte, error) {
51 return nil, newAuthError("unexpected server challenge", nil)
52}
53
54func (c *plainSaslClient) Completed() bool {
55 return true
56}