github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/jwt/provider/provider.go (about) 1 package provider 2 3 import ( 4 "net/http" 5 "sync" 6 7 jwt "github.com/dgrijalva/jwt-go" 8 "github.com/hellofresh/janus/pkg/config" 9 ) 10 11 var providers *sync.Map 12 13 func init() { 14 providers = new(sync.Map) 15 } 16 17 // Provider represents an auth provider 18 type Provider interface { 19 Verifier 20 Build(config config.Credentials) Provider 21 GetClaims(httpClient *http.Client) (jwt.MapClaims, error) 22 } 23 24 // Register registers a new provider 25 func Register(providerName string, providerConstructor Provider) { 26 providers.Store(providerName, providerConstructor) 27 } 28 29 // GetProviders returns the list of registered providers 30 func GetProviders() *sync.Map { 31 return providers 32 } 33 34 // Factory represents a factory of providers 35 type Factory struct{} 36 37 // Build builds one provider based on the auth configuration 38 func (f *Factory) Build(providerName string, config config.Credentials) Provider { 39 provider, ok := providers.Load(providerName) 40 if !ok { 41 provider, _ = providers.Load("basic") 42 } 43 44 p := provider.(Provider) 45 return p.Build(config) 46 }