mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
wip: Add api keys apis
This commit is contained in:
parent
099d893da9
commit
822a7029ef
@ -61,11 +61,11 @@ GET `/users/$id/sessions` can be used by admins to list others session
|
|||||||
|
|
||||||
```
|
```
|
||||||
Get `/apikeys`
|
Get `/apikeys`
|
||||||
Post `/apikeys` {...nlaims} Create a new api keys with given claims
|
Post `/apikeys` {...claims} Create a new api keys with given claims
|
||||||
```
|
```
|
||||||
|
|
||||||
An api key can be used like an opaque token, calling /jwt with it will return a valid jwt with the claims you specified during the post request to create it.
|
An api key can be used like an opaque token, calling /jwt with it will return a valid jwt with the claims you specified during the post request to create it.
|
||||||
Creating an apikeys requires the `apikey.create` permission, reading them requires the `apikey.read` permission.
|
Creating an apikeys requires the `apikey.write` permission, reading them requires the `apikey.read` permission.
|
||||||
|
|
||||||
### OIDC
|
### OIDC
|
||||||
|
|
||||||
|
44
auth/apikey.go
Normal file
44
auth/apikey.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ApiKey struct {
|
||||||
|
Name string `json:"name" example:"my-app"`
|
||||||
|
Token string `json:"token" example:"lyHzTYm9yi+pkEv3m2tamAeeK7Dj7N3QRP7xv7dPU5q9MAe8tU4ySwYczE0RaMr4fijsA=="`
|
||||||
|
CreatedAt time.Time `json:"createAt" 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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ApiKeyDto struct {
|
||||||
|
Name string `json:"name" example:"my-app" validate:"alpha"`
|
||||||
|
Claims jwt.MapClaims `json:"claims" example:"isAdmin: true"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Summary Create API key
|
||||||
|
// @Description Create a new API key
|
||||||
|
// @Tags apikeys
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Security Jwt[apikeys.write]
|
||||||
|
// @Param key body ApiKeyDto false "Api key info"
|
||||||
|
// @Success 201 {object} ApiKey
|
||||||
|
// @Failure 409 {object} KError "Duplicated api key"
|
||||||
|
// @Failure 422 {object} KError "Invalid create body"
|
||||||
|
// @Router /users [get]
|
||||||
|
func (h *Handler) CreateApiKey(c echo.Context) error {
|
||||||
|
var req ApiKeyDto
|
||||||
|
err := c.Bind(&req)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
|
||||||
|
}
|
||||||
|
if err = c.Validate(&req); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
5
auth/sql/migrations/000003_apikeys.down.sql
Normal file
5
auth/sql/migrations/000003_apikeys.down.sql
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
begin;
|
||||||
|
|
||||||
|
drop table apikeys;
|
||||||
|
|
||||||
|
commit;
|
14
auth/sql/migrations/000003_apikeys.up.sql
Normal file
14
auth/sql/migrations/000003_apikeys.up.sql
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
begin;
|
||||||
|
|
||||||
|
create table apikeys(
|
||||||
|
pk serial primary key,
|
||||||
|
id uuid not null default gen_random_uuid(),
|
||||||
|
name varchar(256) not null unique,
|
||||||
|
token varchar(128) not null unique,
|
||||||
|
claims jsonb not null,
|
||||||
|
|
||||||
|
created_at timestamptz not null default now()::timestamptz,
|
||||||
|
last_used timestamptz not null default now()::temistamptz
|
||||||
|
);
|
||||||
|
|
||||||
|
commit;
|
Loading…
x
Reference in New Issue
Block a user