github.com/micro/go-micro/v2@v2.9.1/auth/options.go (about)

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