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