-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
68 lines (59 loc) · 1.71 KB
/
account.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
package shared
import (
"crypto/subtle"
"encoding/base64"
"errors"
"github.com/golang-jwt/jwt/v4"
)
type Account struct {
User string `json:"user"`
Email string `json:"email"`
pass string
realm string
}
// FakeAccountDb should implement BasicAuthAccountCenter interface
type FakeAccountDb struct {
accounts []Account
creds map[string]*Account
}
func (a *FakeAccountDb) InitFakeDb() []Account {
a.accounts = []Account{
{"john", "john@fake.com", "abc", ""},
{"wayne", "wayne@fake.com", "abc", ""},
}
a.creds = map[string]*Account{}
for i := range a.accounts {
account := &a.accounts[i]
base := account.User + ":" + account.pass
cred := "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
a.creds[cred] = account
}
return a.accounts
}
// GetRealm implements one function of BasicAuthAccountCenter interface.
func (a *FakeAccountDb) GetRealm() string {
return "Authorization Required"
}
// SearchCredential implements one function of BasicAuthAccountCenter interface.
func (a *FakeAccountDb) SearchCredential(credential string) (account any, found bool) {
for key, value := range a.creds {
if subtle.ConstantTimeCompare([]byte(key), []byte(credential)) == 1 {
return value, true
}
}
return nil, false
}
// BearerSecret implements one function of BearerValidator interface.
func (a *FakeAccountDb) BearerSecret() string {
return "my_secret"
}
// ValidateClaims implements one function of BearerValidator interface.
func (a *FakeAccountDb) ValidateClaims(claims any) (userData any, err error) {
registeredClams := claims.(jwt.MapClaims)
for _, account := range a.accounts {
if account.User == registeredClams["jti"] {
return &account, nil
}
}
return nil, errors.New("invalid claims")
}