aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core/auth/options.go
blob: 00c6e3ec2090de35d6ce239b5d5396690a008878 (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
57
58
59
60
61
package auth

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

type ListOption func(opts *listOptions)

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

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

	_, has := opts.kind[cred.Kind()]
	if len(opts.kind) > 0 && !has {
		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) ListOption {
	return func(opts *listOptions) {
		opts.target = target
	}
}

// WithKind match credentials with the given kind. Can be specified multiple times.
func WithKind(kind CredentialKind) ListOption {
	return func(opts *listOptions) {
		if opts.kind == nil {
			opts.kind = make(map[CredentialKind]interface{})
		}
		opts.kind[kind] = nil
	}
}

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