-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.go
80 lines (66 loc) · 1.43 KB
/
basic.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
package model
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
)
type BasicAuth struct {
inner map[string]string
innerHash map[string]bool
}
func NewBasicAuth(username []string, password []string) *BasicAuth {
m := make(map[string]string)
mHash := make(map[string]bool)
for i := range username {
m[username[i]] = password[i]
baseEnc := base64.StdEncoding.EncodeToString([]byte(username[i] + ":" + password[i]))
sha := sha256.Sum256([]byte(baseEnc))
hex := hex.EncodeToString(sha[:])
mHash[hex] = true
}
return &BasicAuth{
inner: m,
innerHash: mHash,
}
}
func (b *BasicAuth) Len() int {
return len(b.inner)
}
func (b *BasicAuth) Get(username string) (string, bool) {
v, ok := b.inner[username]
return v, ok
}
func (b *BasicAuth) Auth(auth []byte) bool {
if b.Len() == 0 {
return true
}
i := bytes.IndexByte(auth, ' ')
if i == -1 {
return false
}
if !bytes.EqualFold(auth[:i], []byte("basic")) {
return false
}
decoded, err := base64.StdEncoding.DecodeString(string(auth[i+1:]))
if err != nil {
return false
}
credentials := bytes.Split(decoded, []byte(":"))
if len(credentials) <= 1 {
return false
}
user := credentials[0]
pass := credentials[1]
if p, ok := b.Get(string(user)); ok {
return p == string(pass)
}
return false
}
func (b *BasicAuth) AuthToken(token []byte) bool {
if b.Len() == 0 {
return true
}
_, ok := b.innerHash[string(token)]
return ok
}