Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // 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 | |
| 7 | package auth |
| 8 | |
| 9 | import ( |
| 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. |
| 17 | const PLAIN = "PLAIN" |
| 18 | |
| 19 | func 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. |
| 27 | type PlainAuthenticator struct { |
| 28 | Username string |
| 29 | Password string |
| 30 | } |
| 31 | |
| 32 | // Auth authenticates the connection. |
| 33 | func (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 | |
| 40 | type plainSaslClient struct { |
| 41 | username string |
| 42 | password string |
| 43 | } |
| 44 | |
| 45 | func (c *plainSaslClient) Start() (string, []byte, error) { |
| 46 | b := []byte("\x00" + c.username + "\x00" + c.password) |
| 47 | return PLAIN, b, nil |
| 48 | } |
| 49 | |
| 50 | func (c *plainSaslClient) Next(challenge []byte) ([]byte, error) { |
| 51 | return nil, newAuthError("unexpected server challenge", nil) |
| 52 | } |
| 53 | |
| 54 | func (c *plainSaslClient) Completed() bool { |
| 55 | return true |
| 56 | } |