-
Notifications
You must be signed in to change notification settings - Fork 14
/
auth.go
130 lines (108 loc) · 2.93 KB
/
auth.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package tarantool
import (
"crypto/sha1"
"encoding/base64"
"fmt"
"github.com/tinylib/msgp/msgp"
)
type Auth struct {
User string
Password string
GreetingAuth []byte
}
var _ Query = (*Auth)(nil)
const authHash = "chap-sha1"
const scrambleSize = sha1.Size // == 20
// copy-paste from go-tarantool
func scramble(encodedSalt []byte, pass string) (scramble []byte, err error) {
/* ==================================================================
According to: http://tarantool.org/doc/dev_guide/box-protocol.html
salt = base64_decode(encoded_salt);
step_1 = sha1(password);
step_2 = sha1(step_1);
step_3 = sha1(salt, step_2);
scramble = xor(step_1, step_3);
return scramble;
===================================================================== */
salt, err := base64.StdEncoding.DecodeString(string(encodedSalt))
if err != nil {
return
}
step1 := sha1.Sum([]byte(pass))
step2 := sha1.Sum(step1[0:])
hash := sha1.New() // may be create it once per connection ?
hash.Write(salt[0:scrambleSize])
hash.Write(step2[0:])
step3 := hash.Sum(nil)
return xor(step1[0:], step3[0:], scrambleSize), nil
}
func xor(left, right []byte, size int) []byte {
result := make([]byte, size)
for i := 0; i < size; i++ {
result[i] = left[i] ^ right[i]
}
return result
}
func (auth *Auth) GetCommandID() uint {
return AuthCommand
}
// MarshalMsg implements msgp.Marshaler
func (auth *Auth) MarshalMsg(b []byte) (o []byte, err error) {
scr, err := scramble(auth.GreetingAuth, auth.Password)
if err != nil {
return nil, fmt.Errorf("auth: scrambling failure: %s", err.Error())
}
o = b
o = msgp.AppendMapHeader(o, 2)
o = msgp.AppendUint(o, KeyUserName)
o = msgp.AppendString(o, auth.User)
o = msgp.AppendUint(o, KeyTuple)
o = msgp.AppendArrayHeader(o, 2)
o = msgp.AppendString(o, authHash)
o = msgp.AppendBytes(o, scr)
return o, nil
}
// UnmarshalMsg implements msgp.Unmarshaler
func (auth *Auth) UnmarshalMsg(data []byte) (buf []byte, err error) {
var i, l uint32
var k uint
buf = data
if i, buf, err = msgp.ReadMapHeaderBytes(buf); err != nil {
return
}
for ; i > 0; i-- {
if k, buf, err = msgp.ReadUintBytes(buf); err != nil {
return
}
switch k {
case KeyUserName:
if auth.User, buf, err = msgp.ReadStringBytes(buf); err != nil {
return
}
case KeyTuple:
if l, buf, err = msgp.ReadArrayHeaderBytes(buf); err != nil {
return
}
if l == 2 {
var obuf []byte
if buf, err = msgp.Skip(buf); err != nil {
return
}
obuf = buf
if auth.GreetingAuth, buf, err = msgp.ReadBytesBytes(buf, nil); err != nil {
if _, ok := err.(msgp.TypeError); ok {
buf = obuf
var greetingStr string
if greetingStr, buf, err = msgp.ReadStringBytes(buf); err != nil {
return
}
auth.GreetingAuth = []byte(greetingStr)
}
}
}
default:
return buf, fmt.Errorf("Auth.Unpack: Expected KeyUserName or KeyTuple")
}
}
return
}