-
Notifications
You must be signed in to change notification settings - Fork 0
/
claim.go
63 lines (50 loc) · 909 Bytes
/
claim.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
package auth
type Claims map[string]any
const (
claimUser = "user"
claimIsAdmin = "admin"
claimIsSystem = "system"
)
func NewClaims(user string, isAdmin, isSystem bool) Claims {
return Claims{
claimUser: user,
claimIsAdmin: isAdmin,
claimIsSystem: isSystem,
}
}
func (c Claims) User() string {
userAny, ok := c[claimUser]
if !ok {
return ""
}
user, ok := userAny.(string)
if !ok {
return ""
}
return user
}
func (c Claims) IsAdmin() bool {
adminUserAny, ok := c[claimIsAdmin]
if !ok {
return false
}
adminUser, ok := adminUserAny.(bool)
if !ok {
return false
}
return adminUser
}
func (c Claims) IsSystem() bool {
serviceUserAny, ok := c[claimIsSystem]
if !ok {
return false
}
serviceUser, ok := serviceUserAny.(bool)
if !ok {
return false
}
return serviceUser
}
func (c Claims) IsAdminOrSystem() bool {
return c.IsAdmin() || c.IsSystem()
}