-
Notifications
You must be signed in to change notification settings - Fork 28
/
authz.go
55 lines (49 loc) · 1.03 KB
/
authz.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
package oauth
import (
"time"
"github.com/authgear/authgear-server/pkg/api/model"
)
type Authorization struct {
ID string
AppID string
ClientID string
UserID string
CreatedAt time.Time
UpdatedAt time.Time
Scopes []string
}
func (z Authorization) IsAuthorized(scopes []string) bool {
scopeMap := map[string]struct{}{}
for _, s := range z.Scopes {
scopeMap[s] = struct{}{}
}
for _, s := range scopes {
if _, ok := scopeMap[s]; !ok {
return false
}
}
return true
}
func (z Authorization) WithScopesAdded(scopes []string) *Authorization {
seen := map[string]struct{}{}
var newScopes []string
for _, s := range append(z.Scopes, scopes...) {
if _, ok := seen[s]; !ok {
seen[s] = struct{}{}
newScopes = append(newScopes, s)
}
}
z.Scopes = newScopes
return &z
}
func (z Authorization) ToAPIModel() *model.Authorization {
return &model.Authorization{
Meta: model.Meta{
ID: z.ID,
CreatedAt: z.CreatedAt,
UpdatedAt: z.UpdatedAt,
},
ClientID: z.ClientID,
Scopes: z.Scopes,
}
}