github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/auth/token/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/util/token/options.go 16 17 package token 18 19 import ( 20 "time" 21 22 "github.com/tickoalcantara12/micro/v3/service/store" 23 ) 24 25 type Options struct { 26 // Store to persist the tokens 27 Store store.Store 28 // PublicKey base64 encoded, used by JWT 29 PublicKey string 30 // PrivateKey base64 encoded, used by JWT 31 PrivateKey string 32 } 33 34 type Option func(o *Options) 35 36 // WithStore sets the token providers store 37 func WithStore(s store.Store) Option { 38 return func(o *Options) { 39 o.Store = s 40 } 41 } 42 43 // WithPublicKey sets the JWT public key 44 func WithPublicKey(key string) Option { 45 return func(o *Options) { 46 o.PublicKey = key 47 } 48 } 49 50 // WithPrivateKey sets the JWT private key 51 func WithPrivateKey(key string) Option { 52 return func(o *Options) { 53 o.PrivateKey = key 54 } 55 } 56 57 func NewOptions(opts ...Option) Options { 58 var options Options 59 for _, o := range opts { 60 o(&options) 61 } 62 //set default store 63 if options.Store == nil { 64 options.Store = store.DefaultStore 65 } 66 return options 67 } 68 69 type GenerateOptions struct { 70 // Expiry for the token 71 Expiry time.Duration 72 } 73 74 type GenerateOption func(o *GenerateOptions) 75 76 // WithExpiry for the generated account's token expires 77 func WithExpiry(d time.Duration) GenerateOption { 78 return func(o *GenerateOptions) { 79 o.Expiry = d 80 } 81 } 82 83 // NewGenerateOptions from a slice of options 84 func NewGenerateOptions(opts ...GenerateOption) GenerateOptions { 85 var options GenerateOptions 86 for _, o := range opts { 87 o(&options) 88 } 89 //set default Expiry of token 90 if options.Expiry == 0 { 91 options.Expiry = time.Minute * 15 92 } 93 return options 94 }