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