-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
77 lines (65 loc) · 1.42 KB
/
util.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
package scope
import (
"sort"
"strings"
)
const (
OpenID = "openid"
Email = "email"
OfflineAccess = "offline_access"
)
func Split(scope string) []string {
return strings.Split(scope, " ")
}
func IncludeAll(scopes string, targetScopes []string) (bool, string) {
list := Split(scopes)
existanceMap := make(map[string]bool, 0)
for _, s := range list {
existanceMap[s] = true
}
for _, s := range targetScopes {
if _, exists := existanceMap[s]; !exists {
return false, s
}
}
return true, ""
}
func Remove(scopes string, targetScope string) string {
list := Split(scopes)
newList := make([]string, 0)
for _, s := range list {
if s != targetScope {
newList = append(newList, s)
}
}
return strings.Join(newList, " ")
}
func Include(scopes string, targetScope string) bool {
list := Split(scopes)
for _, s := range list {
if s == targetScope {
return true
}
}
return false
}
func Same(scope1, scope2 string) bool {
return Sort(scope1) == Sort(scope2)
}
func Sort(origin string) string {
list := Split(origin)
sort.Strings(list)
return strings.Join(list, " ")
}
func IncludeOpenID(scopes string) bool {
return Include(scopes, OpenID)
}
func IncludeOfflineAccess(scopes string) bool {
return Include(scopes, OfflineAccess)
}
func RemoveOpenID(scopes string) string {
return Remove(scopes, OpenID)
}
func RemoveOfflineAccess(scopes string) string {
return Remove(scopes, OfflineAccess)
}