-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtypes.go
117 lines (97 loc) · 2.24 KB
/
types.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
package tobab
import (
"fmt"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/asaskevich/govalidator"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/looplab/fsm"
matcher "github.com/ryanuber/go-glob"
)
type Config struct {
Hostname string `valid:"dns"`
Dev bool
Displayname string `valid:"required"`
DefaultTokenAge string
MaxTokenAge string
CookieScope string `valid:"required"`
Loglevel string
DatabasePath string `valid:"required"`
}
type User struct {
ID []byte `storm:"id"`
Name string `storm:"unique"`
RegistrationFinished bool
Created time.Time
LastSeen time.Time
Admin bool
AccessibleHosts []string
Creds []webauthn.Credential
}
func (user *User) CanAccess(h string) bool {
if user.Admin {
return true
}
return Contains(user.AccessibleHosts, h)
}
func (user *User) WebAuthnID() []byte {
return user.ID
}
func (user *User) WebAuthnName() string {
return user.Name
}
func (user *User) WebAuthnDisplayName() string {
return user.Name
}
func (user *User) WebAuthnIcon() string {
return "https://pics.com/avatar.png"
}
func (user *User) WebAuthnCredentials() []webauthn.Credential {
return user.Creds
}
type Session struct {
ID string `storm:"id"`
UserID []byte
Created time.Time
LastSeen time.Time
Expires time.Time `storm:"index"`
Vals map[string]string
Data *webauthn.SessionData
FSM *fsm.FSM
State string
}
type Glob string
func (g Glob) Match(s string) bool {
return matcher.Glob(string(g), s)
}
func (c *Config) Validate() (bool, error) {
ok, err := govalidator.ValidateStruct(c)
if !ok {
return ok, err
}
if !strings.HasSuffix(c.Hostname, c.CookieScope) {
return false, fmt.Errorf("Hostname: '%s' should be in the same domain as the cookiescope: '%s'", c.Hostname, c.CookieScope)
}
return ok, err
}
func LoadConf(path string) (Config, error) {
var cfg Config
_, err := toml.DecodeFile(path, &cfg)
if err != nil {
return cfg, err
}
ok, err := cfg.Validate()
if !ok {
return cfg, err
}
return cfg, err
}
func Contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}