login.go raw

   1  package sasl
   2  
   3  import (
   4  	"bytes"
   5  )
   6  
   7  // The LOGIN mechanism name.
   8  const Login = "LOGIN"
   9  
  10  var expectedChallenge = []byte("Password:")
  11  
  12  type loginClient struct {
  13  	Username string
  14  	Password string
  15  }
  16  
  17  func (a *loginClient) Start() (mech string, ir []byte, err error) {
  18  	mech = "LOGIN"
  19  	ir = []byte(a.Username)
  20  	return
  21  }
  22  
  23  func (a *loginClient) Next(challenge []byte) (response []byte, err error) {
  24  	if bytes.Compare(challenge, expectedChallenge) != 0 {
  25  		return nil, ErrUnexpectedServerChallenge
  26  	} else {
  27  		return []byte(a.Password), nil
  28  	}
  29  }
  30  
  31  // A client implementation of the LOGIN authentication mechanism for SMTP,
  32  // as described in http://www.iana.org/go/draft-murchison-sasl-login
  33  //
  34  // It is considered obsolete, and should not be used when other mechanisms are
  35  // available. For plaintext password authentication use PLAIN mechanism.
  36  func NewLoginClient(username, password string) Client {
  37  	return &loginClient{username, password}
  38  }
  39