gitee.com/sasukebo/go-micro/v4@v4.7.1/auth/options.go (about)

     1  package auth
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  func NewOptions(opts ...Option) Options {
     9  	var options Options
    10  	for _, o := range opts {
    11  		o(&options)
    12  	}
    13  	return options
    14  }
    15  
    16  type Options struct {
    17  	// Namespace the service belongs to
    18  	Namespace string
    19  	// ID is the services auth ID
    20  	ID string
    21  	// Secret is used to authenticate the service
    22  	Secret string
    23  	// Token is the services token used to authenticate itself
    24  	Token *Token
    25  	// PublicKey for decoding JWTs
    26  	PublicKey string
    27  	// PrivateKey for encoding JWTs
    28  	PrivateKey string
    29  	// Addrs sets the addresses of auth
    30  	Addrs []string
    31  }
    32  
    33  type Option func(o *Options)
    34  
    35  // Addrs is the auth addresses to use
    36  func Addrs(addrs ...string) Option {
    37  	return func(o *Options) {
    38  		o.Addrs = addrs
    39  	}
    40  }
    41  
    42  // Namespace the service belongs to
    43  func Namespace(n string) Option {
    44  	return func(o *Options) {
    45  		o.Namespace = n
    46  	}
    47  }
    48  
    49  // PublicKey is the JWT public key
    50  func PublicKey(key string) Option {
    51  	return func(o *Options) {
    52  		o.PublicKey = key
    53  	}
    54  }
    55  
    56  // PrivateKey is the JWT private key
    57  func PrivateKey(key string) Option {
    58  	return func(o *Options) {
    59  		o.PrivateKey = key
    60  	}
    61  }
    62  
    63  // Credentials sets the auth credentials
    64  func Credentials(id, secret string) Option {
    65  	return func(o *Options) {
    66  		o.ID = id
    67  		o.Secret = secret
    68  	}
    69  }
    70  
    71  // ClientToken sets the auth token to use when making requests
    72  func ClientToken(token *Token) Option {
    73  	return func(o *Options) {
    74  		o.Token = token
    75  	}
    76  }
    77  
    78  type GenerateOptions struct {
    79  	// Metadata associated with the account
    80  	Metadata map[string]string
    81  	// Scopes the account has access too
    82  	Scopes []string
    83  	// Provider of the account, e.g. oauth
    84  	Provider string
    85  	// Type of the account, e.g. user
    86  	Type string
    87  	// Secret used to authenticate the account
    88  	Secret string
    89  }
    90  
    91  type GenerateOption func(o *GenerateOptions)
    92  
    93  // WithSecret for the generated account
    94  func WithSecret(s string) GenerateOption {
    95  	return func(o *GenerateOptions) {
    96  		o.Secret = s
    97  	}
    98  }
    99  
   100  // WithType for the generated account
   101  func WithType(t string) GenerateOption {
   102  	return func(o *GenerateOptions) {
   103  		o.Type = t
   104  	}
   105  }
   106  
   107  // WithMetadata for the generated account
   108  func WithMetadata(md map[string]string) GenerateOption {
   109  	return func(o *GenerateOptions) {
   110  		o.Metadata = md
   111  	}
   112  }
   113  
   114  // WithProvider for the generated account
   115  func WithProvider(p string) GenerateOption {
   116  	return func(o *GenerateOptions) {
   117  		o.Provider = p
   118  	}
   119  }
   120  
   121  // WithScopes for the generated account
   122  func WithScopes(s ...string) GenerateOption {
   123  	return func(o *GenerateOptions) {
   124  		o.Scopes = s
   125  	}
   126  }
   127  
   128  // NewGenerateOptions from a slice of options
   129  func NewGenerateOptions(opts ...GenerateOption) GenerateOptions {
   130  	var options GenerateOptions
   131  	for _, o := range opts {
   132  		o(&options)
   133  	}
   134  	return options
   135  }
   136  
   137  type TokenOptions struct {
   138  	// ID for the account
   139  	ID string
   140  	// Secret for the account
   141  	Secret string
   142  	// RefreshToken is used to refesh a token
   143  	RefreshToken string
   144  	// Expiry is the time the token should live for
   145  	Expiry time.Duration
   146  }
   147  
   148  type TokenOption func(o *TokenOptions)
   149  
   150  // WithExpiry for the token
   151  func WithExpiry(ex time.Duration) TokenOption {
   152  	return func(o *TokenOptions) {
   153  		o.Expiry = ex
   154  	}
   155  }
   156  
   157  func WithCredentials(id, secret string) TokenOption {
   158  	return func(o *TokenOptions) {
   159  		o.ID = id
   160  		o.Secret = secret
   161  	}
   162  }
   163  
   164  func WithToken(rt string) TokenOption {
   165  	return func(o *TokenOptions) {
   166  		o.RefreshToken = rt
   167  	}
   168  }
   169  
   170  // NewTokenOptions from a slice of options
   171  func NewTokenOptions(opts ...TokenOption) TokenOptions {
   172  	var options TokenOptions
   173  	for _, o := range opts {
   174  		o(&options)
   175  	}
   176  
   177  	// set defualt expiry of token
   178  	if options.Expiry == 0 {
   179  		options.Expiry = time.Minute
   180  	}
   181  
   182  	return options
   183  }
   184  
   185  type VerifyOptions struct {
   186  	Context context.Context
   187  }
   188  
   189  type VerifyOption func(o *VerifyOptions)
   190  
   191  func VerifyContext(ctx context.Context) VerifyOption {
   192  	return func(o *VerifyOptions) {
   193  		o.Context = ctx
   194  	}
   195  }
   196  
   197  type ListOptions struct {
   198  	Context context.Context
   199  }
   200  
   201  type ListOption func(o *ListOptions)
   202  
   203  func RulesContext(ctx context.Context) ListOption {
   204  	return func(o *ListOptions) {
   205  		o.Context = ctx
   206  	}
   207  }