aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core/auth/options.go
blob: 741898743b08f6052a72f9cf5caeee782812a036 (plain) (blame)
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
package auth

type options struct {
	target string
	kind   CredentialKind
	meta   map[string]string
}

type Option func(opts *options)

func matcher(opts []Option) *options {
	result := &options{}
	for _, opt := range opts {
		opt(result)
	}
	return result
}

func (opts *options) Match(cred Credential) bool {
	if opts.target != "" && cred.Target() != opts.target {
		return false
	}

	if opts.kind != "" && cred.Kind() != opts.kind {
		return false
	}

	for key, val := range opts.meta {
		if v, ok := cred.GetMetadata(key); !ok || v != val {
			return false
		}
	}

	return true
}

func WithTarget(target string) Option {
	return func(opts *options) {
		opts.target = target
	}
}

func WithKind(kind CredentialKind) Option {
	return func(opts *options) {
		opts.kind = kind
	}
}

func WithMeta(key string, val string) Option {
	return func(opts *options) {
		if opts.meta == nil {
			opts.meta = make(map[string]string)
		}
		opts.meta[key] = val
	}
}