github.com/annwntech/go-micro/v2@v2.9.5/auth/token/options.go (about)

     1  package token
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/annwntech/go-micro/v2/store"
     7  )
     8  
     9  type Options struct {
    10  	// Store to persist the tokens
    11  	Store store.Store
    12  	// PublicKey base64 encoded, used by JWT
    13  	PublicKey string
    14  	// PrivateKey base64 encoded, used by JWT
    15  	PrivateKey string
    16  }
    17  
    18  type Option func(o *Options)
    19  
    20  // WithStore sets the token providers store
    21  func WithStore(s store.Store) Option {
    22  	return func(o *Options) {
    23  		o.Store = s
    24  	}
    25  }
    26  
    27  // WithPublicKey sets the JWT public key
    28  func WithPublicKey(key string) Option {
    29  	return func(o *Options) {
    30  		o.PublicKey = key
    31  	}
    32  }
    33  
    34  // WithPrivateKey sets the JWT private key
    35  func WithPrivateKey(key string) Option {
    36  	return func(o *Options) {
    37  		o.PrivateKey = key
    38  	}
    39  }
    40  
    41  func NewOptions(opts ...Option) Options {
    42  	var options Options
    43  	for _, o := range opts {
    44  		o(&options)
    45  	}
    46  	//set default store
    47  	if options.Store == nil {
    48  		options.Store = store.DefaultStore
    49  	}
    50  	return options
    51  }
    52  
    53  type GenerateOptions struct {
    54  	// Expiry for the token
    55  	Expiry time.Duration
    56  }
    57  
    58  type GenerateOption func(o *GenerateOptions)
    59  
    60  // WithExpiry for the generated account's token expires
    61  func WithExpiry(d time.Duration) GenerateOption {
    62  	return func(o *GenerateOptions) {
    63  		o.Expiry = d
    64  	}
    65  }
    66  
    67  // NewGenerateOptions from a slice of options
    68  func NewGenerateOptions(opts ...GenerateOption) GenerateOptions {
    69  	var options GenerateOptions
    70  	for _, o := range opts {
    71  		o(&options)
    72  	}
    73  	//set default Expiry of token
    74  	if options.Expiry == 0 {
    75  		options.Expiry = time.Minute * 15
    76  	}
    77  	return options
    78  }