-
Notifications
You must be signed in to change notification settings - Fork 7
/
basic.go
211 lines (180 loc) · 4.75 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package user
import (
"strings"
"azugo.io/azugo/token"
"github.com/valyala/bytebufferpool"
)
// Option represents basic user behavior option.
type Option interface {
apply(*Basic)
}
// ScopeGroupSeparator is group separator to use when parsing granted scopes.
type ScopeGroupSeparator string
func (s ScopeGroupSeparator) apply(u *Basic) {
u.scopeGroupSeparator = string(s)
}
// ScopeLevelSeparator is level separator to use when parsing granted scopes.
type ScopeLevelSeparator string
func (s ScopeLevelSeparator) apply(u *Basic) {
u.scopeLevelSeparator = string(s)
}
// ScopeClaimName is name of claim that contains user granted scopes.
type ScopeClaimName string
func (s ScopeClaimName) apply(u *Basic) {
u.scopeClaimName = []string{string(s)}
}
// Basic represents authorized user.
type Basic struct {
scopeSeparator string
scopeLevelSeparator string
scopeGroupSeparator string
scopeClaimName []string
claims map[string]token.ClaimStrings
scopes map[string][]string
}
func (u *Basic) parseScopes() {
if u == nil || u.scopes != nil {
return
}
scopes := u.Claim(u.scopeClaimName...)
if len(scopes) == 1 {
scopes = strings.Split(scopes[0], u.scopeSeparator)
}
u.scopes = make(map[string][]string, len(scopes))
for _, r := range scopes {
rr, l, _ := strings.Cut(r, u.scopeLevelSeparator)
lvls, ok := u.scopes[rr]
if !ok {
lvls = []string{l}
} else {
lvls = append(lvls, l)
}
u.scopes[rr] = lvls
}
}
// New returns new user instance with specified claims.
func New(claims map[string]token.ClaimStrings, options ...Option) *Basic {
user := &Basic{
scopeSeparator: ",",
scopeLevelSeparator: ":",
scopeGroupSeparator: "/",
scopeClaimName: []string{"scope", "rights", "http://docs.oasis-open.org/wsfed/authorization/200706/claims/action"},
claims: claims,
}
for _, opt := range options {
opt.apply(user)
}
user.parseScopes()
return user
}
// ClaimValue returns claim value.
// If multiple names are provided, claim that matches first name will be returned.
func (u *Basic) ClaimValue(name ...string) string {
if u == nil {
return ""
}
for _, n := range name {
if claims, ok := u.claims[n]; ok {
return claims.Value()
}
}
return ""
}
// Claim returns all claims with specified name.
// If multiple names are provided, claim that matches first name will be returned.
func (u *Basic) Claim(name ...string) token.ClaimStrings {
if u == nil {
return nil
}
for _, n := range name {
if claims, ok := u.claims[n]; ok {
return claims
}
}
return nil
}
// GivenName returns users given name.
func (u *Basic) GivenName() string {
return u.ClaimValue("given_name", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname")
}
// FamilyName returns users family name.
func (u *Basic) FamilyName() string {
return u.ClaimValue("family_name", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname")
}
// DisplayName returns users display name.
func (u *Basic) DisplayName() string {
if name := u.ClaimValue("name", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"); len(name) > 0 {
return name
}
displayName := bytebufferpool.Get()
defer bytebufferpool.Put(displayName)
if gn := u.GivenName(); len(gn) > 0 {
_, _ = displayName.WriteString(gn)
}
if fn := u.FamilyName(); len(fn) > 0 {
if displayName.Len() > 0 {
_ = displayName.WriteByte(' ')
}
_, _ = displayName.WriteString(fn)
}
return displayName.String()
}
// Authorized returns if user is authorized.
func (u *Basic) Authorized() bool {
return u != nil
}
// ID returns user ID.
func (u *Basic) ID() string {
if u == nil {
return ""
}
return u.ClaimValue("sub", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")
}
// HasScopeGroup checks if user has any granted scopes in specified group.
func (u *Basic) HasScopeGroup(name string) bool {
if u == nil {
return false
}
for scope := range u.scopes {
if scope == name || strings.HasPrefix(scope, name+u.scopeGroupSeparator) {
return true
}
}
return false
}
// HasScope checks if user has granted scope with any level.
func (u *Basic) HasScope(name string) bool {
if u == nil {
return false
}
_, ok := u.scopes[name]
return ok
}
// HasScopeLevel checks if user has granted scope with exact level.
func (u *Basic) HasScopeLevel(name string, level string) bool {
if u == nil {
return false
}
levels, ok := u.scopes[name]
if !ok {
return false
}
for _, l := range levels {
if level == l {
return true
}
}
return false
}
// HasScopeAnyLevel checks if user has granted scope with one of levels.
func (u *Basic) HasScopeAnyLevel(name string, levels ...string) bool {
if u == nil {
return false
}
for _, l := range levels {
if u.HasScopeLevel(name, l) {
return true
}
}
return false
}