/
Auther.go
178 lines (120 loc) · 3.36 KB
/
Auther.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package gowebapi
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/jameskeane/bcrypt"
"strings"
"time"
)
type Auther interface {
Authenticate(request *Request, response *Response) (*Response)
Signin(userdata string, expiryMinutes int64) string
Hash(password string) string
Match(password string, hash string) bool
}
type defaultAuther struct {
key []byte
}
func NewDefaultAuther(key []byte) Auther {
return &defaultAuther{key}
}
func (self *defaultAuther) Authenticate(request *Request, response *Response) (*Response) {
if nil != response {
return nil
}
authHeaders, authExists := request.Http.Header["Authorization"]
if authExists {
header := strings.Trim(authHeaders[0], " ")
parts := strings.Split(header, " ")
if 2 == len(parts) {
auth, decodeErr := base64.StdEncoding.DecodeString(parts[1])
if nil == decodeErr && "basic" == strings.ToLower(parts[0][:5]) {
authParts := strings.Split(string(auth), ":")
userData := self.decodeTicket(authParts[0])
if "" != userData {
request.UserData = userData
return nil
}
}
}
}
return &Response{
Status: 401,
Header: map[string][]string{"Www-Authenticate": []string{"Basic/WebAPI"}},
}
}
func (self *defaultAuther) Signin(userdata string, expiryMinutes int64) string {
return self.encodeTicket(userdata, expiryMinutes)
}
func (self *defaultAuther) Hash(password string) string {
hash, _ := bcrypt.Hash(password)
return hash
}
func (self *defaultAuther) Match(password string, hash string) bool {
return bcrypt.Match(password, hash)
}
func (self *defaultAuther) encodeTicket(userdata string, expiryMinutes int64) string {
// generate the token uuid|userdata|expiry
uuid := Uuid()
time := time.Now().Add(time.Duration(expiryMinutes) * time.Minute).Format(time.RFC3339)
token := fmt.Sprintf("%s|%s|%s", uuid, userdata, time)
// encrypt the token
block, blockError := aes.NewCipher(self.key)
if nil != blockError {
return ""
}
bytes := []byte(token)
encrypted := make([]byte, len(bytes))
encrypter := cipher.NewCFBEncrypter(block, self.key[:aes.BlockSize])
encrypter.XORKeyStream(encrypted, bytes)
return hex.EncodeToString(encrypted)
}
func (self *defaultAuther) decodeTicket(token string) string {
if "" != token {
// decrypt the token
bytes, decodeError := hex.DecodeString(token)
if nil != decodeError {
return ""
}
block, blockError := aes.NewCipher(self.key)
if nil != blockError {
return ""
}
decrypted := make([]byte, len(bytes))
decrypter := cipher.NewCFBDecrypter(block, self.key[:aes.BlockSize])
decrypter.XORKeyStream(decrypted, bytes)
// split the decrypted string into uuid|userdata|expiry
parts := strings.Split(string(decrypted), "|")
if 3 != len(parts) {
return ""
}
// TODO: validate the expiry
// TODO: handle 0 (infinite) expiry
expiry, expiryError := time.Parse(time.RFC3339, parts[2])
if nil != expiryError {
return ""
}
if time.Now().Sub(expiry) > 0 {
return ""
}
// return userdata
return parts[1]
} else {
return ""
}
}
func Uuid() string {
// http://stackoverflow.com/questions/15130321
bytes := make([]byte, 16)
_, err := rand.Read(bytes)
if err != nil {
return ""
}
bytes[8] = (bytes[8] | 0x80) & 0xBF // identify UUID V4
bytes[6] = (bytes[6] | 0x40) & 0x4F //
return hex.EncodeToString(bytes)
}