...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/auth/plain.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/auth

     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  
    13  // PLAIN is the mechanism name for PLAIN.
    14  const PLAIN = "PLAIN"
    15  
    16  func newPlainAuthenticator(cred *Cred) (Authenticator, error) {
    17  	return &PlainAuthenticator{
    18  		Username: cred.Username,
    19  		Password: cred.Password,
    20  	}, nil
    21  }
    22  
    23  // PlainAuthenticator uses the PLAIN algorithm over SASL to authenticate a connection.
    24  type PlainAuthenticator struct {
    25  	Username string
    26  	Password string
    27  }
    28  
    29  // Auth authenticates the connection.
    30  func (a *PlainAuthenticator) Auth(ctx context.Context, cfg *Config) error {
    31  	return ConductSaslConversation(ctx, cfg, "$external", &plainSaslClient{
    32  		username: a.Username,
    33  		password: a.Password,
    34  	})
    35  }
    36  
    37  type plainSaslClient struct {
    38  	username string
    39  	password string
    40  }
    41  
    42  var _ SaslClient = (*plainSaslClient)(nil)
    43  
    44  func (c *plainSaslClient) Start() (string, []byte, error) {
    45  	b := []byte("\x00" + c.username + "\x00" + c.password)
    46  	return PLAIN, b, nil
    47  }
    48  
    49  func (c *plainSaslClient) Next([]byte) ([]byte, error) {
    50  	return nil, newAuthError("unexpected server challenge", nil)
    51  }
    52  
    53  func (c *plainSaslClient) Completed() bool {
    54  	return true
    55  }
    56  

View as plain text