-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
112 lines (88 loc) · 2.41 KB
/
memory.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
package memory
import (
"flag"
"fmt"
"strconv"
"strings"
"github.com/ViBiOh/auth/v2/pkg/auth"
"github.com/ViBiOh/auth/v2/pkg/ident/basic"
"github.com/ViBiOh/auth/v2/pkg/model"
"github.com/ViBiOh/flags"
)
var (
_ auth.Provider = Service{}
_ basic.Provider = Service{}
)
type Service struct {
ident map[string]basicUser
auth map[uint64][]string
}
type Config struct {
Idents []string
Auths []string
}
func Flags(fs *flag.FlagSet, prefix string, overrides ...flags.Override) *Config {
var config Config
flags.New("Users", "Users credentials in the form 'id:login:password'").Prefix(prefix).DocPrefix("memory").EnvSeparator("|").StringSliceVar(fs, &config.Idents, nil, overrides)
flags.New("Profiles", "Users profiles in the form 'id:profile1|profile2'").Prefix(prefix).DocPrefix("memory").StringSliceVar(fs, &config.Auths, nil, overrides)
return &config
}
func New(config *Config) (Service, error) {
identService, err := loadIdent(config.Idents)
if err != nil {
return Service{}, fmt.Errorf("load ident: %w", err)
}
authService, err := loadAuth(config.Auths)
if err != nil {
return Service{}, fmt.Errorf("load auth: %w", err)
}
return Service{
ident: identService,
auth: authService,
}, nil
}
func loadIdent(idents []string) (map[string]basicUser, error) {
if len(idents) == 0 {
return nil, nil
}
users := make(map[string]basicUser)
ids := make(map[uint64]bool)
for _, identUser := range idents {
parts := strings.Split(identUser, ":")
if len(parts) != 3 {
return nil, fmt.Errorf("invalid format for user ident `%s`", identUser)
}
userID, err := strconv.ParseUint(parts[0], 10, 64)
if err != nil {
return nil, err
}
if ids[userID] {
return nil, fmt.Errorf("id already exists for user ident `%s`", identUser)
}
ids[userID] = true
user := basicUser{
User: model.NewUser(userID, strings.ToLower(parts[1])),
password: []byte(parts[2]),
}
users[user.Login] = user
}
return users, nil
}
func loadAuth(auths []string) (map[uint64][]string, error) {
if len(auths) == 0 {
return nil, nil
}
users := make(map[uint64][]string)
for _, authUser := range auths {
parts := strings.Split(authUser, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid format of user auth `%s`", authUser)
}
userID, err := strconv.ParseUint(parts[0], 10, 64)
if err != nil {
return nil, err
}
users[userID] = strings.Split(parts[1], "|")
}
return users, nil
}