From 27447ac9bd1f411e69874ceba19c3c35fea2b377 Mon Sep 17 00:00:00 2001 From: Vernon Keenan Date: Sat, 30 Jan 2021 09:06:39 -0800 Subject: [PATCH] isolate auth --- app/auth.go | 14 +++++++++++++- app/root.go | 26 -------------------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/app/auth.go b/app/auth.go index 2f2a9ed..f0560d5 100644 --- a/app/auth.go +++ b/app/auth.go @@ -4,16 +4,28 @@ import ( "code.tnxs.net/taxnexus/lib/api/auth/auth_client" "code.tnxs.net/taxnexus/lib/api/auth/auth_client/user" "code.tnxs.net/taxnexus/lib/api/auth/auth_models" + httptransport "github.com/go-openapi/runtime/client" ) var authClient = auth_client.Default +var authUser *User // CheckAPIUser is exported func CheckAPIUser(token *string) (*User, error) { sugar.Debug("app.CheckAPIUser: 📥") + if authUser == nil { + authUser = &User{ + APIKey: GetServiceAccount("auth").APIKey, + Auth: httptransport.APIKeyAuth( + "X-API-Key", + "header", + GetServiceAccount("auth").APIKey, + ), + } + } params := user.NewGetUsersParams() params.Apikey = token - response, err := authClient.User.GetUsers(params, apiUsers["auth"].Auth) + response, err := authClient.User.GetUsers(params, authUser.Auth) if err != nil { sugar.Warnf("app.CheckAPIUser: ❗ Access attempt with invalid API key: %s", *token) return nil, err diff --git a/app/root.go b/app/root.go index f348fa8..cd453fe 100644 --- a/app/root.go +++ b/app/root.go @@ -12,7 +12,6 @@ import ( "code.tnxs.net/taxnexus/lib/api/regs/regs_client" "code.tnxs.net/taxnexus/lib/api/stash/stash_client" "code.tnxs.net/taxnexus/lib/app/logger" - httptransport "github.com/go-openapi/runtime/client" "github.com/spf13/viper" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -28,7 +27,6 @@ var stashClient = stash_client.Default var opsClient = ops_client.Default var regsClient = regs_client.Default var configured = false -var apiUsers map[string]User const getTimeout = 6 * time.Minute const postTimeout = 6 * time.Minute @@ -64,30 +62,6 @@ func InitConfig(systemName string, initalLogLevel zapcore.Level) { sugar.Fatalf("app.InitConfig: 💣 ⛔ can't unmarshal system config: %w", err) } sugar = logger.New(GetLogLevel()) - apiUsers = initAPIUsers() sugar.Debugf("app.InitConfig: 👍 📤 serviceAccounts: %d", len(config.ServiceAccounts)) configured = true } - -func initAPIUsers() map[string]User { - sugar.Info("app.initAPIUsers: 📥") - users := map[string]User{ - "auth": { - APIKey: GetServiceAccount("auth").APIKey, - Auth: httptransport.APIKeyAuth( - "X-API-Key", - "header", - GetServiceAccount("auth").APIKey, - ), - }, - "test-service-account": { - APIKey: GetServiceAccount("test-service-account").APIKey, - Auth: httptransport.APIKeyAuth( - "X-API-Key", - "header", - GetServiceAccount("test-service-account").APIKey), - }, - } - sugar.Debugf("app.initAPIUsers: 👍 📤") - return users -}