...
1
2
3
4
5
6
7 package auth
8
9 import (
10 "context"
11
12 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
13 "go.mongodb.org/mongo-driver/x/mongo/driver"
14 "go.mongodb.org/mongo-driver/x/mongo/driver/operation"
15 )
16
17
18 const MongoDBX509 = "MONGODB-X509"
19
20 func newMongoDBX509Authenticator(cred *Cred) (Authenticator, error) {
21 return &MongoDBX509Authenticator{User: cred.Username}, nil
22 }
23
24
25 type MongoDBX509Authenticator struct {
26 User string
27 }
28
29 var _ SpeculativeAuthenticator = (*MongoDBX509Authenticator)(nil)
30
31
32
33 type x509Conversation struct{}
34
35 var _ SpeculativeConversation = (*x509Conversation)(nil)
36
37
38 func (c *x509Conversation) FirstMessage() (bsoncore.Document, error) {
39 return createFirstX509Message(), nil
40 }
41
42
43 func createFirstX509Message() bsoncore.Document {
44 elements := [][]byte{
45 bsoncore.AppendInt32Element(nil, "authenticate", 1),
46 bsoncore.AppendStringElement(nil, "mechanism", MongoDBX509),
47 }
48
49 return bsoncore.BuildDocument(nil, elements...)
50 }
51
52
53
54 func (c *x509Conversation) Finish(context.Context, *Config, bsoncore.Document) error {
55 return nil
56 }
57
58
59 func (a *MongoDBX509Authenticator) CreateSpeculativeConversation() (SpeculativeConversation, error) {
60 return &x509Conversation{}, nil
61 }
62
63
64 func (a *MongoDBX509Authenticator) Auth(ctx context.Context, cfg *Config) error {
65 requestDoc := createFirstX509Message()
66 authCmd := operation.
67 NewCommand(requestDoc).
68 Database("$external").
69 Deployment(driver.SingleConnectionDeployment{cfg.Connection}).
70 ClusterClock(cfg.ClusterClock).
71 ServerAPI(cfg.ServerAPI)
72 err := authCmd.Execute(ctx)
73 if err != nil {
74 return newAuthError("round trip error", err)
75 }
76
77 return nil
78 }
79
View as plain text