forked from hashicorp/cap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
85 lines (78 loc) · 2.06 KB
/
options.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
package oidc
import (
"time"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/hashicorp/cap/oidc/internal/strutils"
)
// Option defines a common functional options type which can be used in a
// variadic parameter pattern.
type Option func(interface{})
// ApplyOpts takes a pointer to the options struct as a set of default options
// and applies the slice of opts as overrides.
func ApplyOpts(opts interface{}, opt ...Option) {
for _, o := range opt {
if o == nil { // ignore any nil Options
continue
}
o(opts)
}
}
// WithNow provides an optional func for determining what the current time it
// is.
//
// Valid for: Config, Tk and Request
func WithNow(now func() time.Time) Option {
return func(o interface{}) {
if now == nil {
return
}
switch v := o.(type) {
case *configOptions:
v.withNowFunc = now
case *tokenOptions:
v.withNowFunc = now
case *reqOptions:
v.withNowFunc = now
}
}
}
// WithScopes provides an optional list of scopes.
//
// Valid for: Config and Request
func WithScopes(scopes ...string) Option {
return func(o interface{}) {
if len(scopes) == 0 {
return
}
switch v := o.(type) {
case *configOptions:
// configOptions already has the oidc.ScopeOpenID in its defaults.
scopes = strutils.RemoveDuplicatesStable(scopes, false)
v.withScopes = append(v.withScopes, scopes...)
case *reqOptions:
// need to prepend the oidc.ScopeOpenID
ts := append([]string{oidc.ScopeOpenID}, scopes...)
scopes = strutils.RemoveDuplicatesStable(ts, false)
v.withScopes = append(v.withScopes, scopes...)
}
}
}
// WithAudiences provides an optional list of audiences.
//
//Valid for: Config and Request
func WithAudiences(auds ...string) Option {
return func(o interface{}) {
if len(auds) == 0 {
return
}
auds := strutils.RemoveDuplicatesStable(auds, false)
switch v := o.(type) {
case *configOptions:
v.withAudiences = append(v.withAudiences, auds...)
case *reqOptions:
v.withAudiences = append(v.withAudiences, auds...)
case *userInfoOptions:
v.withAudiences = append(v.withAudiences, auds...)
}
}
}