From 667249bc815240dea31731e419937ac56dab6b38 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 23 Apr 2025 22:45:39 +0200 Subject: [PATCH] Add CreatedBy value in apikeys --- auth/apikey.go | 12 ++++++++++++ auth/dbc/apikeys.sql.go | 18 ++++++++++++------ auth/dbc/models.go | 2 +- auth/sql/migrations/000003_apikeys.up.sql | 2 +- auth/sql/queries/apikeys.sql | 4 ++-- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/auth/apikey.go b/auth/apikey.go index c258b9f1..4f2e2d07 100644 --- a/auth/apikey.go +++ b/auth/apikey.go @@ -85,10 +85,22 @@ func (h *Handler) CreateApiKey(c echo.Context) error { return err } + var user *int32 + uid, err :=GetCurrentUserId(c) + // if err, we probably are using an api key (so no user) + if err != nil { + u, _ := h.db.GetUser(context.Background(), dbc.GetUserParams{ + UseId: true, + Id: uid, + }) + user = &u[0].User.Pk + } + dbkey, err := h.db.CreateApiKey(context.Background(), dbc.CreateApiKeyParams{ Name: req.Name, Token: base64.RawURLEncoding.EncodeToString(id), Claims: req.Claims, + CreatedBy: user, }) if ErrIs(err, pgerrcode.UniqueViolation) { return echo.NewHTTPError(409, "An apikey with the same name already exists.") diff --git a/auth/dbc/apikeys.sql.go b/auth/dbc/apikeys.sql.go index 230a5367..fa4d1643 100644 --- a/auth/dbc/apikeys.sql.go +++ b/auth/dbc/apikeys.sql.go @@ -13,20 +13,26 @@ import ( ) const createApiKey = `-- name: CreateApiKey :one -insert into apikeys(name, token, claims) - values ($1, $2, $3) +insert into apikeys(name, token, claims, created_by) + values ($1, $2, $3, $4) returning pk, id, name, token, claims, created_by, created_at, last_used ` type CreateApiKeyParams struct { - Name string `json:"name"` - Token string `json:"token"` - Claims jwt.MapClaims `json:"claims"` + Name string `json:"name"` + Token string `json:"token"` + Claims jwt.MapClaims `json:"claims"` + CreatedBy *int32 `json:"createdBy"` } func (q *Queries) CreateApiKey(ctx context.Context, arg CreateApiKeyParams) (Apikey, error) { - row := q.db.QueryRow(ctx, createApiKey, arg.Name, arg.Token, arg.Claims) + row := q.db.QueryRow(ctx, createApiKey, + arg.Name, + arg.Token, + arg.Claims, + arg.CreatedBy, + ) var i Apikey err := row.Scan( &i.Pk, diff --git a/auth/dbc/models.go b/auth/dbc/models.go index b64b96aa..7bf7c38f 100644 --- a/auth/dbc/models.go +++ b/auth/dbc/models.go @@ -17,7 +17,7 @@ type Apikey struct { Name string `json:"name"` Token string `json:"token"` Claims jwt.MapClaims `json:"claims"` - CreatedBy int32 `json:"createdBy"` + CreatedBy *int32 `json:"createdBy"` CreatedAt time.Time `json:"createdAt"` LastUsed time.Time `json:"lastUsed"` } diff --git a/auth/sql/migrations/000003_apikeys.up.sql b/auth/sql/migrations/000003_apikeys.up.sql index 40e5d3d8..22ddbd86 100644 --- a/auth/sql/migrations/000003_apikeys.up.sql +++ b/auth/sql/migrations/000003_apikeys.up.sql @@ -7,7 +7,7 @@ create table apikeys( token varchar(128) not null unique, claims jsonb not null, - created_by integer not null references users(pk) on delete cascade, + created_by integer references users(pk) on delete cascade, created_at timestamptz not null default now()::timestamptz, last_used timestamptz not null default now()::timestamptz ); diff --git a/auth/sql/queries/apikeys.sql b/auth/sql/queries/apikeys.sql index 257dfe2f..634038c1 100644 --- a/auth/sql/queries/apikeys.sql +++ b/auth/sql/queries/apikeys.sql @@ -24,8 +24,8 @@ order by last_used; -- name: CreateApiKey :one -insert into apikeys(name, token, claims) - values ($1, $2, $3) +insert into apikeys(name, token, claims, created_by) + values ($1, $2, $3, $4) returning *;