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