Debian Codegraph @

api/v0/auth/sasl/mech/crammd5/mechanism.go

raw symbols SCIP
1 package crammd5
2
3 import (
4 "crypto/hmac"
5 "crypto/md5"
6 "encoding/hex"
7 "errors"
8 "io"
9
10 log "github.com/golang/glog"
11 "github.com/mesos/mesos-go/api/v0/auth/callback"
12 "github.com/mesos/mesos-go/api/v0/auth/sasl/mech"
13 )
14
15 var (
16 Name = "CRAM-MD5" // name this mechanism is registered with
17
18 //TODO(jdef) is this a generic SASL error? if so, move it up to mech
19 challengeDataRequired = errors.New("challenge data may not be empty")
20 )
21
22 func init() {
23 mech.Register(Name, newInstance)
24 }
25
26 type mechanism struct {
27 handler callback.Handler
28 }
29
30 func (m *mechanism) Handler() callback.Handler {
31 return m.handler
32 }
33
34 func (m *mechanism) Discard() {
35 // noop
36 }
37
38 func newInstance(h callback.Handler) (mech.Interface, mech.StepFunc, error) {
39 m := &mechanism{
40 handler: h,
41 }
42 fn := func(m mech.Interface, data []byte) (mech.StepFunc, []byte, error) {
43 // noop: no initialization needed
44 return challengeResponse, nil, nil
45 }
46 return m, fn, nil
47 }
48
49 // algorithm lifted from wikipedia: http://en.wikipedia.org/wiki/CRAM-MD5
50 // except that the SASL mechanism used by Mesos doesn't leverage base64 encoding
51 func challengeResponse(m mech.Interface, data []byte) (mech.StepFunc, []byte, error) {
52 if len(data) == 0 {
53 return mech.IllegalState, nil, challengeDataRequired
54 }
55 decoded := string(data)
56 log.V(4).Infof("challenge(decoded): %s", decoded) // for deep debugging only
57
58 username := callback.NewName()
59 secret := callback.NewPassword()
60
61 if err := m.Handler().Handle(username, secret); err != nil {
62 return mech.IllegalState, nil, err
63 }
64 hash := hmac.New(md5.New, secret.Get())
65 if _, err := io.WriteString(hash, decoded); err != nil {
66 return mech.IllegalState, nil, err
67 }
68
69 codes := hex.EncodeToString(hash.Sum(nil))
70 msg := username.Get() + " " + codes
71 return nil, []byte(msg), nil
72 }