Loads of updates

This commit is contained in:
2021-06-22 22:52:48 +02:00
parent 8dc20a4eb0
commit ccafd60923
18 changed files with 1206 additions and 125 deletions

View File

@@ -5,7 +5,6 @@ import (
"strings"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
// AccountCreate writes a user to database
@@ -15,38 +14,31 @@ func (d Db) AccountCreate(input AccountCreateInput) (CreatedAccount, error) {
_, err := d.DbPool.Exec(context.Background(), accountSQL, input.ID, input.Name, input.APIKey, input.Password)
if err != nil {
if strings.HasPrefix(err.Error(), "ERROR: duplicate key") {
log.WithFields(log.Fields{"name": input.Name}).Debug("Duplicate name in accounts database")
d.Log.Debug("Duplicate name in accounts database", "name", input.Name)
} else {
log.Error("Database error when trying to add account: " + err.Error())
d.Log.Error("Database error when trying to add account", "err", err.Error())
}
return CreatedAccount{}, err
}
log.WithFields(log.Fields{
"id": input.ID,
"name": input.Name,
}).Info("Added account to database")
d.Log.Info("Added account to database", "id", input.ID, "name", input.Name)
accountFieldsSQL := "INSERT INTO \"accountsFields\" (id, \"accountId\", name, value) VALUES($1,$2,$3,$4);"
for _, field := range input.Fields {
newFieldID, uuidErr := uuid.NewRandom()
if uuidErr != nil {
log.Fatal("Could not create new Uuid, err: " + uuidErr.Error())
d.Log.Fatal("Could not create new Uuid", "err", uuidErr.Error())
}
_, err := d.DbPool.Exec(context.Background(), accountFieldsSQL, newFieldID, input.ID, field.Name, field.Values)
if err != nil {
if strings.HasPrefix(err.Error(), "ERROR: duplicate key") {
log.Error("Database error when trying to account field: " + err.Error())
d.Log.Error("Database error when trying to account field", "err", err.Error())
}
}
log.WithFields(log.Fields{
"accountId": input.ID,
"fieldName": field.Name,
"fieldValues": field.Values,
}).Debug("Added account field")
d.Log.Debug("Added account field", "accountId", input.ID, "fieldName", field.Name, "fieldValues", field.Values)
}
return CreatedAccount{
@@ -58,12 +50,7 @@ func (d Db) AccountCreate(input AccountCreateInput) (CreatedAccount, error) {
// AccountGet fetches an account from the database
func (d Db) AccountGet(accountID string, APIKey string, Name string) (Account, error) {
logContext := log.WithFields(log.Fields{
"accountID": accountID,
"APIKey": len(APIKey),
})
logContext.Debug("Trying to get account")
d.Log.Debug("Trying to get account", "accountID", accountID, "len(APIKey)", len(APIKey))
var account Account
var searchParam string
@@ -82,18 +69,18 @@ func (d Db) AccountGet(accountID string, APIKey string, Name string) (Account, e
accountErr := d.DbPool.QueryRow(context.Background(), accountSQL, searchParam).Scan(&account.ID, &account.Created, &account.Name, &account.Password)
if accountErr != nil {
if accountErr.Error() == "no rows in result set" {
logContext.Debug("No account found")
d.Log.Debug("No account found", "accountID", accountID, "APIKey", len(APIKey))
return Account{}, accountErr
}
logContext.Error("Database error when fetching account, err: " + accountErr.Error())
d.Log.Error("Database error when fetching account", "err", accountErr.Error(), "accountID", accountID, "APIKey", len(APIKey))
return Account{}, accountErr
}
fieldsSQL := "SELECT name, value FROM \"accountsFields\" WHERE \"accountId\" = $1"
rows, fieldsErr := d.DbPool.Query(context.Background(), fieldsSQL, account.ID)
if fieldsErr != nil {
logContext.Error("Database error when fetching account fields, err: " + accountErr.Error())
d.Log.Error("Database error when fetching account fields", "err", accountErr.Error(), "accountID", accountID, "APIKey", len(APIKey))
return Account{}, fieldsErr
}
@@ -103,7 +90,7 @@ func (d Db) AccountGet(accountID string, APIKey string, Name string) (Account, e
var value []string
err := rows.Scan(&name, &value)
if err != nil {
logContext.Error("Could not get name or value from database row, err: " + err.Error())
d.Log.Error("Could not get name or value from database row", "err", err.Error(), "accountID", accountID, "APIKey", len(APIKey))
return Account{}, err
}
account.Fields[name] = value

View File

@@ -3,22 +3,19 @@ package db
import (
"context"
log "github.com/sirupsen/logrus"
"gitlab.larvit.se/power-plan/auth/src/utils"
)
// RenewalTokenCreate obtain a new renewal token
func (d Db) RenewalTokenCreate(accountID string) (string, error) {
logContext := log.WithFields(log.Fields{"accountID": accountID})
logContext.Debug("Creating new renewal token")
d.Log.Debug("Creating new renewal token", "accountID", accountID)
newToken := utils.RandString(60)
insertSQL := "INSERT INTO \"renewalTokens\" (\"accountId\",token) VALUES($1,$2);"
_, insertErr := d.DbPool.Exec(context.Background(), insertSQL, accountID, newToken)
if insertErr != nil {
logContext.Error("Could not insert into database table \"renewalTokens\", err: " + insertErr.Error())
d.Log.Error("Could not insert into database table \"renewalTokens\"", "err", insertErr.Error(), "accountID", accountID)
return "", insertErr
}
@@ -27,7 +24,7 @@ func (d Db) RenewalTokenCreate(accountID string) (string, error) {
// RenewalTokenGet checks if a valid renewal token exists in database
func (d Db) RenewalTokenGet(token string) (string, error) {
log.Debug("Trying to get a renewal token")
d.Log.Debug("Trying to get a renewal token")
sql := "SELECT \"accountId\" FROM \"renewalTokens\" WHERE exp >= now() AND token = $1"
@@ -38,7 +35,7 @@ func (d Db) RenewalTokenGet(token string) (string, error) {
return "", nil
}
log.Error("Database error when fetching renewal token, err: " + err.Error())
d.Log.Error("Database error when fetching renewal token", "err", err.Error())
return "", err
}
@@ -47,12 +44,12 @@ func (d Db) RenewalTokenGet(token string) (string, error) {
// RenewalTokenRm removes a renewal token from the database
func (d Db) RenewalTokenRm(token string) error {
log.Debug("Trying to remove a renewal token")
d.Log.Debug("Trying to remove a renewal token")
sql := "DELETE FROM \"renewalTokens\" WHERE token = $1"
_, err := d.DbPool.Exec(context.Background(), sql, token)
if err != nil {
log.Error("Database error when trying to remove token, err: " + err.Error())
d.Log.Error("Database error when trying to remove token", "err", err.Error())
return err
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v4/pgxpool"
"go.uber.org/zap"
)
// Account is an account as represented in the database
@@ -41,4 +42,5 @@ type AccountCreateInput struct {
// Db struct
type Db struct {
DbPool *pgxpool.Pool
Log *zap.SugaredLogger
}