@@ -6,12 +6,7 @@ import (
6
6
"net/http"
7
7
)
8
8
9
- type ConjunctionKind int
10
-
11
- const (
12
- AND ConjunctionKind = iota
13
- OR
14
- )
9
+ type ScopesCheckerFunc func (ctx context.Context , required []string , candidates []string ) error
15
10
16
11
type withScopesOpts struct {
17
12
// TokenCtxKey is the context key of an authenticated token value, typically set by the JWT middleware.
@@ -21,24 +16,15 @@ type withScopesOpts struct {
21
16
// default implementation is naive as r.Context().Value(TokenCtxKey).(*jwt.Token)
22
17
TokenFromContext func (ctx context.Context ) (* jwt.Token , error )
23
18
ClaimsFromToken claimFromTokenFunc
24
- // Conjunction defines whether all scopes (AND) are required or one (OR)
25
- // of the provided scopes is sufficient for the request to be accepted.
26
- Conjunction ConjunctionKind
27
19
// ErrorWriter writes an error into w
28
20
ErrorWriter errorWriter
29
21
// Logger logs various messages
30
22
Logger logger
31
- scopesChecker func ( required [] string , candidates [] string ) error
23
+ ScopesChecker ScopesCheckerFunc
32
24
}
33
25
34
26
type WithScopesOpt func (* withScopesOpts )
35
27
36
- func WithConjunction (c ConjunctionKind ) WithScopesOpt {
37
- return func (o * withScopesOpts ) {
38
- o .Conjunction = c
39
- }
40
- }
41
-
42
28
func WithErrorWriter (w errorWriter ) WithScopesOpt {
43
29
return func (o * withScopesOpts ) {
44
30
o .ErrorWriter = w
@@ -57,6 +43,12 @@ func WithClaimsFromToken(f claimFromTokenFunc) WithScopesOpt {
57
43
}
58
44
}
59
45
46
+ func WithScopesChecker (f ScopesCheckerFunc ) WithScopesOpt {
47
+ return func (o * withScopesOpts ) {
48
+ o .ScopesChecker = f
49
+ }
50
+ }
51
+
60
52
func newWithScopesOpts (opts []WithScopesOpt ) * withScopesOpts {
61
53
o := new (withScopesOpts )
62
54
for _ , oo := range opts {
@@ -89,10 +81,8 @@ func newWithScopesOpts(opts []WithScopesOpt) *withScopesOpts {
89
81
o .TokenCtxKey = TokenCtxKey
90
82
}
91
83
92
- if o .Conjunction == OR {
93
- o .scopesChecker = scopesCheckerOR
94
- } else {
95
- o .scopesChecker = scopesCheckerAND
84
+ if o .ScopesChecker == nil {
85
+ o .ScopesChecker = scopesCheckerAND
96
86
}
97
87
98
88
if o .ClaimsFromToken == nil {
0 commit comments