mirror of
https://github.com/go-micro/go-micro.git
synced 2025-11-23 21:44:41 +02:00
* feat: more plugins * chore(ci): split out benchmarks Attempt to resolve too many open files in ci * chore(ci): split out benchmarks * fix(ci): Attempt to resolve too many open files in ci * fix: set DefaultX for cli flag and service option * fix: restore http broker * fix: default http broker * feat: full nats profile * chore: still ugly, not ready * fix: better initialization for profiles * fix(tests): comment out flaky listen tests * fix: disable benchmarks on gha * chore: cleanup, comments * chore: add nats config source
79 lines
1.5 KiB
Go
79 lines
1.5 KiB
Go
package token
|
|
|
|
import (
|
|
"time"
|
|
|
|
"go-micro.dev/v5/store"
|
|
)
|
|
|
|
type Options struct {
|
|
// Store to persist the tokens
|
|
Store store.Store
|
|
// PublicKey base64 encoded, used by JWT
|
|
PublicKey string
|
|
// PrivateKey base64 encoded, used by JWT
|
|
PrivateKey string
|
|
}
|
|
|
|
type Option func(o *Options)
|
|
|
|
// WithStore sets the token providers store.
|
|
func WithStore(s store.Store) Option {
|
|
return func(o *Options) {
|
|
o.Store = s
|
|
}
|
|
}
|
|
|
|
// WithPublicKey sets the JWT public key.
|
|
func WithPublicKey(key string) Option {
|
|
return func(o *Options) {
|
|
o.PublicKey = key
|
|
}
|
|
}
|
|
|
|
// WithPrivateKey sets the JWT private key.
|
|
func WithPrivateKey(key string) Option {
|
|
return func(o *Options) {
|
|
o.PrivateKey = key
|
|
}
|
|
}
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
var options Options
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
// set default store
|
|
if options.Store == nil {
|
|
options.Store = store.DefaultStore
|
|
}
|
|
return options
|
|
}
|
|
|
|
type GenerateOptions struct {
|
|
// Expiry for the token
|
|
Expiry time.Duration
|
|
}
|
|
|
|
type GenerateOption func(o *GenerateOptions)
|
|
|
|
// WithExpiry for the generated account's token expires.
|
|
func WithExpiry(d time.Duration) GenerateOption {
|
|
return func(o *GenerateOptions) {
|
|
o.Expiry = d
|
|
}
|
|
}
|
|
|
|
// NewGenerateOptions from a slice of options.
|
|
func NewGenerateOptions(opts ...GenerateOption) GenerateOptions {
|
|
var options GenerateOptions
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
// set default Expiry of token
|
|
if options.Expiry == 0 {
|
|
options.Expiry = time.Minute * 15
|
|
}
|
|
return options
|
|
}
|