mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Add CreatedBy value in apikeys
This commit is contained in:
parent
dcbbb6352a
commit
667249bc81
@ -85,10 +85,22 @@ func (h *Handler) CreateApiKey(c echo.Context) error {
|
|||||||
return err
|
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{
|
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,
|
||||||
})
|
})
|
||||||
if ErrIs(err, pgerrcode.UniqueViolation) {
|
if ErrIs(err, pgerrcode.UniqueViolation) {
|
||||||
return echo.NewHTTPError(409, "An apikey with the same name already exists.")
|
return echo.NewHTTPError(409, "An apikey with the same name already exists.")
|
||||||
|
@ -13,20 +13,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const createApiKey = `-- name: CreateApiKey :one
|
const createApiKey = `-- name: CreateApiKey :one
|
||||||
insert into apikeys(name, token, claims)
|
insert into apikeys(name, token, claims, created_by)
|
||||||
values ($1, $2, $3)
|
values ($1, $2, $3, $4)
|
||||||
returning
|
returning
|
||||||
pk, id, name, token, claims, created_by, created_at, last_used
|
pk, id, name, token, claims, created_by, created_at, last_used
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateApiKeyParams struct {
|
type CreateApiKeyParams struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Claims jwt.MapClaims `json:"claims"`
|
Claims jwt.MapClaims `json:"claims"`
|
||||||
|
CreatedBy *int32 `json:"createdBy"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateApiKey(ctx context.Context, arg CreateApiKeyParams) (Apikey, error) {
|
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
|
var i Apikey
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.Pk,
|
&i.Pk,
|
||||||
|
@ -17,7 +17,7 @@ type Apikey struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Claims jwt.MapClaims `json:"claims"`
|
Claims jwt.MapClaims `json:"claims"`
|
||||||
CreatedBy int32 `json:"createdBy"`
|
CreatedBy *int32 `json:"createdBy"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
LastUsed time.Time `json:"lastUsed"`
|
LastUsed time.Time `json:"lastUsed"`
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ create table apikeys(
|
|||||||
token varchar(128) not null unique,
|
token varchar(128) not null unique,
|
||||||
claims jsonb not null,
|
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,
|
created_at timestamptz not null default now()::timestamptz,
|
||||||
last_used timestamptz not null default now()::timestamptz
|
last_used timestamptz not null default now()::timestamptz
|
||||||
);
|
);
|
||||||
|
@ -24,8 +24,8 @@ order by
|
|||||||
last_used;
|
last_used;
|
||||||
|
|
||||||
-- name: CreateApiKey :one
|
-- name: CreateApiKey :one
|
||||||
insert into apikeys(name, token, claims)
|
insert into apikeys(name, token, claims, created_by)
|
||||||
values ($1, $2, $3)
|
values ($1, $2, $3, $4)
|
||||||
returning
|
returning
|
||||||
*;
|
*;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user