Fix auth tests

This commit is contained in:
Zoe Roux 2025-05-26 00:42:09 +02:00
parent 953ac5ffa8
commit af97c52e48
No known key found for this signature in database
3 changed files with 18 additions and 18 deletions

View File

@ -49,7 +49,7 @@ jobs:
run: | run: |
./keibi > logs & ./keibi > logs &
wget --retry-connrefused --retry-on-http-error=502 http://localhost:4568/health wget --retry-connrefused --retry-on-http-error=502 http://localhost:4568/health
hurl --error-format long --variable host=http://localhost:4568 tests/* hurl --error-format long --variable host=http://localhost:4568/auth tests/*
env: env:
PGHOST: localhost PGHOST: localhost
FIRST_USER_CLAIMS: '{"permissions": ["users.read"]}' FIRST_USER_CLAIMS: '{"permissions": ["users.read"]}'

View File

@ -19,11 +19,11 @@ import (
) )
type ApiKey struct { type ApiKey struct {
Id uuid.UUID `json:"id" example:"e05089d6-9179-4b5b-a63e-94dd5fc2a397"` Id uuid.UUID `json:"id" example:"e05089d6-9179-4b5b-a63e-94dd5fc2a397"`
Name string `json:"name" example:"myapp"` Name string `json:"name" example:"myapp"`
CreatedAt time.Time `json:"createAt" example:"2025-03-29T18:20:05.267Z"` CreatedAt time.Time `json:"createAt" example:"2025-03-29T18:20:05.267Z"`
LastUsed time.Time `json:"lastUsed" example:"2025-03-29T18:20:05.267Z"` LastUsed time.Time `json:"lastUsed" example:"2025-03-29T18:20:05.267Z"`
Claims jwt.MapClaims `json:"claims" example:"isAdmin: true"` Claims jwt.MapClaims `json:"claims" example:"isAdmin: true"`
} }
type ApiKeyWToken struct { type ApiKeyWToken struct {
@ -32,18 +32,18 @@ type ApiKeyWToken struct {
} }
type ApiKeyDto struct { type ApiKeyDto struct {
Name string `json:"name" example:"myapp" validate:"alpha"` Name string `json:"name" example:"myapp" validate:"alpha"`
Claims jwt.MapClaims `json:"claims" example:"isAdmin: true"` Claims jwt.MapClaims `json:"claims" example:"isAdmin: true"`
} }
func MapDbKey(key *dbc.Apikey) ApiKeyWToken { func MapDbKey(key *dbc.Apikey) ApiKeyWToken {
return ApiKeyWToken{ return ApiKeyWToken{
ApiKey: ApiKey{ ApiKey: ApiKey{
Id: key.Id, Id: key.Id,
Name: key.Name, Name: key.Name,
Claims: key.Claims, Claims: key.Claims,
CreatedAt: key.CreatedAt, CreatedAt: key.CreatedAt,
LastUsed: key.LastUsed, LastUsed: key.LastUsed,
}, },
Token: fmt.Sprintf("%s-%s", key.Name, key.Token), Token: fmt.Sprintf("%s-%s", key.Name, key.Token),
} }
@ -91,15 +91,15 @@ func (h *Handler) CreateApiKey(c echo.Context) error {
if err != nil { if err != nil {
u, _ := h.db.GetUser(context.Background(), dbc.GetUserParams{ u, _ := h.db.GetUser(context.Background(), dbc.GetUserParams{
UseId: true, UseId: true,
Id: uid, Id: uid,
}) })
user = &u[0].User.Pk user = &u[0].User.Pk
} }
dbkey, err := h.db.CreateApiKey(context.Background(), dbc.CreateApiKeyParams{ dbkey, err := h.db.CreateApiKey(context.Background(), dbc.CreateApiKeyParams{
Name: req.Name, Name: req.Name,
Token: base64.RawURLEncoding.EncodeToString(id), Token: base64.RawURLEncoding.EncodeToString(id),
Claims: req.Claims, Claims: req.Claims,
CreatedBy: user, CreatedBy: user,
}) })
if ErrIs(err, pgerrcode.UniqueViolation) { if ErrIs(err, pgerrcode.UniqueViolation) {
@ -169,7 +169,7 @@ func (h *Handler) ListApiKey(c echo.Context) error {
return c.JSON(200, Page[ApiKey]{ return c.JSON(200, Page[ApiKey]{
Items: ret, Items: ret,
This: c.Request().URL.String(), This: c.Request().URL.String(),
}) })
} }
@ -182,7 +182,7 @@ func (h *Handler) createApiJwt(apikey string) (string, error) {
key, fromEnv := h.config.EnvApiKeys[info[0]] key, fromEnv := h.config.EnvApiKeys[info[0]]
if !fromEnv { if !fromEnv {
dbKey, err := h.db.GetApiKey(context.Background(), dbc.GetApiKeyParams{ dbKey, err := h.db.GetApiKey(context.Background(), dbc.GetApiKeyParams{
Name: info[0], Name: info[0],
Token: info[1], Token: info[1],
}) })
if err == pgx.ErrNoRows { if err == pgx.ErrNoRows {

View File

@ -56,7 +56,7 @@ func GetCurrentSessionId(c echo.Context) (uuid.UUID, error) {
func CheckPermissions(c echo.Context, perms []string) error { func CheckPermissions(c echo.Context, perms []string) error {
token, ok := c.Get("user").(*jwt.Token) token, ok := c.Get("user").(*jwt.Token)
if !ok{ if !ok {
return echo.NewHTTPError(401, "Not logged in") return echo.NewHTTPError(401, "Not logged in")
} }
sub, err := token.Claims.GetSubject() sub, err := token.Claims.GetSubject()