2020-02-03 08:16:02 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
type Options struct {
|
2020-02-25 22:15:44 +00:00
|
|
|
// Token is an auth token
|
|
|
|
Token string
|
|
|
|
// Public key base64 encoded
|
|
|
|
PublicKey string
|
|
|
|
// Private key base64 encoded
|
|
|
|
PrivateKey string
|
|
|
|
// Endpoints to exclude
|
|
|
|
Exclude []string
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(o *Options)
|
|
|
|
|
2020-02-25 22:15:44 +00:00
|
|
|
// Exclude ecludes a set of endpoints from authorization
|
|
|
|
func Exclude(e ...string) Option {
|
2020-02-10 08:26:28 +00:00
|
|
|
return func(o *Options) {
|
2020-02-25 22:15:44 +00:00
|
|
|
o.Exclude = e
|
2020-02-10 08:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:16:02 +00:00
|
|
|
// PublicKey is the JWT public key
|
|
|
|
func PublicKey(key string) Option {
|
|
|
|
return func(o *Options) {
|
2020-02-25 22:15:44 +00:00
|
|
|
o.PublicKey = key
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrivateKey is the JWT private key
|
|
|
|
func PrivateKey(key string) Option {
|
|
|
|
return func(o *Options) {
|
2020-02-25 22:15:44 +00:00
|
|
|
o.PrivateKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token sets an auth token
|
|
|
|
func Token(t string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Token = t
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type GenerateOptions struct {
|
2020-02-25 22:15:44 +00:00
|
|
|
// Metadata associated with the account
|
2020-02-03 08:16:02 +00:00
|
|
|
Metadata map[string]string
|
2020-02-25 22:15:44 +00:00
|
|
|
// Roles/scopes associated with the account
|
|
|
|
Roles []*Role
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GenerateOption func(o *GenerateOptions)
|
|
|
|
|
|
|
|
// Metadata for the generated account
|
|
|
|
func Metadata(md map[string]string) func(o *GenerateOptions) {
|
|
|
|
return func(o *GenerateOptions) {
|
|
|
|
o.Metadata = md
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Roles for the generated account
|
|
|
|
func Roles(rs []*Role) func(o *GenerateOptions) {
|
|
|
|
return func(o *GenerateOptions) {
|
|
|
|
o.Roles = rs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGenerateOptions from a slice of options
|
|
|
|
func NewGenerateOptions(opts ...GenerateOption) GenerateOptions {
|
|
|
|
var options GenerateOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|