2019-12-08 23:15:06 +03:00
|
|
|
package auth
|
|
|
|
|
2020-07-28 13:56:46 +03:00
|
|
|
type listOptions struct {
|
2019-12-08 23:15:06 +03:00
|
|
|
target string
|
2020-02-15 04:55:19 +03:00
|
|
|
kind map[CredentialKind]interface{}
|
2020-01-08 00:07:25 +03:00
|
|
|
meta map[string]string
|
2019-12-08 23:15:06 +03:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:56:46 +03:00
|
|
|
type ListOption func(opts *listOptions)
|
2019-12-08 23:15:06 +03:00
|
|
|
|
2020-07-28 13:56:46 +03:00
|
|
|
func matcher(opts []ListOption) *listOptions {
|
|
|
|
result := &listOptions{}
|
2019-12-08 23:15:06 +03:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(result)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:56:46 +03:00
|
|
|
func (opts *listOptions) Match(cred Credential) bool {
|
2019-12-08 23:15:06 +03:00
|
|
|
if opts.target != "" && cred.Target() != opts.target {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
_, has := opts.kind[cred.Kind()]
|
|
|
|
if len(opts.kind) > 0 && !has {
|
2019-12-08 23:15:06 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-08 00:07:25 +03:00
|
|
|
for key, val := range opts.meta {
|
2020-01-24 02:30:13 +03:00
|
|
|
if v, ok := cred.GetMetadata(key); !ok || v != val {
|
2020-01-08 00:07:25 +03:00
|
|
|
return false
|
|
|
|
}
|
2019-12-08 23:15:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:56:46 +03:00
|
|
|
func WithTarget(target string) ListOption {
|
|
|
|
return func(opts *listOptions) {
|
2019-12-08 23:15:06 +03:00
|
|
|
opts.target = target
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
// WithKind match credentials with the given kind. Can be specified multiple times.
|
2020-07-28 13:56:46 +03:00
|
|
|
func WithKind(kind CredentialKind) ListOption {
|
|
|
|
return func(opts *listOptions) {
|
2020-02-15 04:55:19 +03:00
|
|
|
if opts.kind == nil {
|
|
|
|
opts.kind = make(map[CredentialKind]interface{})
|
|
|
|
}
|
|
|
|
opts.kind[kind] = nil
|
2019-12-08 23:15:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:56:46 +03:00
|
|
|
func WithMeta(key string, val string) ListOption {
|
|
|
|
return func(opts *listOptions) {
|
2020-01-08 00:07:25 +03:00
|
|
|
if opts.meta == nil {
|
|
|
|
opts.meta = make(map[string]string)
|
|
|
|
}
|
|
|
|
opts.meta[key] = val
|
2019-12-08 23:15:06 +03:00
|
|
|
}
|
|
|
|
}
|