-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
32 lines (24 loc) · 978 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package sasl
import "context"
// ServerMechFactory is used to create a server mechanism.
type ServerMechFactory func(state interface{}) ServerMech
// Server aids in the encapsulation of all the supported mechanisms.
type Server struct {
factories map[string]ServerMechFactory
}
// RegisterMechFactory registers the mechanism factory by name.
func (s *Server) RegisterMechFactory(mechName string, factory ServerMechFactory) {
if s.factories == nil {
s.factories = make(map[string]ServerMechFactory)
}
s.factories[mechName] = factory
}
// Auth authenticates/authorizes a user with the named mechanism.
func (s *Server) Auth(ctx context.Context, state interface{}, mechName string, response []byte, incoming <-chan []byte, outgoing chan<- []byte) error {
factory, ok := s.factories[mechName]
if !ok {
return newError("sasl mechanism '%' has not been registered", nil)
}
mech := factory(state)
return ConverseAsServer(ctx, mech, response, incoming, outgoing)
}