// Copyright 2016 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package sasl

import (
	
)

var plainSep = []byte{0}

var plain = Mechanism{
	Name: "PLAIN",
	Start: func( *Negotiator) ( bool,  []byte,  interface{},  error) {
		, ,  := .credentials()
		 := make([]byte, 0, len()+len()+len()+2)
		 = append(, ...)
		 = append(, '\x00')
		 = append(, ...)
		 = append(, '\x00')
		 = append(, ...)
		return false, , nil, nil
	},
	Next: func( *Negotiator,  []byte,  interface{}) ( bool,  []byte,  interface{},  error) {
		// If we're a client, or we're a server that's past the AuthTextSent step,
		// we should never actually hit this step.
		if .State()&Receiving != Receiving || .State()&StepMask != AuthTextSent {
			 = ErrTooManySteps
			return
		}

		// If we're a server, validate that the challenge looks like:
		// "Identity\x00Username\x00Password"
		 := bytes.Split(, plainSep)
		if len() != 3 {
			 = ErrInvalidChallenge
			return
		}

		if .Permissions(Credentials(func() (, ,  []byte) {
			return [1], [2], [0]
		})) {
			// Everything checks out as far as we know and the server should continue
			// to authenticate the user.
			return
		}

		 = ErrAuthn
		return
	},
}