-
Notifications
You must be signed in to change notification settings - Fork 329
/
scope.go
45 lines (35 loc) · 894 Bytes
/
scope.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
package auth
import (
"strings"
"github.com/lestrrat-go/jwx/jwt"
)
const (
scopeDelimiter = " "
)
// CheckJwtScope verifies if required scope is in claim
// We're using interface{} and casting it if needed to simplify usage of the fn by directly
// using it with map[string]interface{} claims type
func CheckJwtScope(token jwt.Token, required ...string) bool {
if token == nil {
return false
}
scopeClaimRaw, has := token.Get("scope")
if !has {
return false
}
scopeClaim, ok := scopeClaimRaw.(string)
if !ok {
return false
}
return CheckScope(scopeClaim, required...)
}
func CheckScope(scope string, required ...string) bool {
scope = scopeDelimiter + strings.TrimSpace(scope) + scopeDelimiter
for _, req := range required {
req = scopeDelimiter + strings.TrimSpace(req) + scopeDelimiter
if strings.Contains(scope, req) {
return true
}
}
return false
}