isolate auth

0.1.44 v0.1.44
Vernon Keenan 2021-01-30 09:06:39 -08:00
parent 530e351912
commit 27447ac9bd
2 changed files with 13 additions and 27 deletions

View File

@ -4,16 +4,28 @@ import (
"code.tnxs.net/taxnexus/lib/api/auth/auth_client" "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_client/user"
"code.tnxs.net/taxnexus/lib/api/auth/auth_models" "code.tnxs.net/taxnexus/lib/api/auth/auth_models"
httptransport "github.com/go-openapi/runtime/client"
) )
var authClient = auth_client.Default var authClient = auth_client.Default
var authUser *User
// CheckAPIUser is exported // CheckAPIUser is exported
func CheckAPIUser(token *string) (*User, error) { func CheckAPIUser(token *string) (*User, error) {
sugar.Debug("app.CheckAPIUser: 📥") 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 := user.NewGetUsersParams()
params.Apikey = token params.Apikey = token
response, err := authClient.User.GetUsers(params, apiUsers["auth"].Auth) response, err := authClient.User.GetUsers(params, authUser.Auth)
if err != nil { if err != nil {
sugar.Warnf("app.CheckAPIUser: ❗ Access attempt with invalid API key: %s", *token) sugar.Warnf("app.CheckAPIUser: ❗ Access attempt with invalid API key: %s", *token)
return nil, err return nil, err

View File

@ -12,7 +12,6 @@ import (
"code.tnxs.net/taxnexus/lib/api/regs/regs_client" "code.tnxs.net/taxnexus/lib/api/regs/regs_client"
"code.tnxs.net/taxnexus/lib/api/stash/stash_client" "code.tnxs.net/taxnexus/lib/api/stash/stash_client"
"code.tnxs.net/taxnexus/lib/app/logger" "code.tnxs.net/taxnexus/lib/app/logger"
httptransport "github.com/go-openapi/runtime/client"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
@ -28,7 +27,6 @@ var stashClient = stash_client.Default
var opsClient = ops_client.Default var opsClient = ops_client.Default
var regsClient = regs_client.Default var regsClient = regs_client.Default
var configured = false var configured = false
var apiUsers map[string]User
const getTimeout = 6 * time.Minute const getTimeout = 6 * time.Minute
const postTimeout = 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.Fatalf("app.InitConfig: 💣 ⛔ can't unmarshal system config: %w", err)
} }
sugar = logger.New(GetLogLevel()) sugar = logger.New(GetLogLevel())
apiUsers = initAPIUsers()
sugar.Debugf("app.InitConfig: 👍 📤 serviceAccounts: %d", len(config.ServiceAccounts)) sugar.Debugf("app.InitConfig: 👍 📤 serviceAccounts: %d", len(config.ServiceAccounts))
configured = true 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
}