mirror of
https://github.com/kou029w/quot.git
synced 2025-01-19 00:18:09 +00:00
fixed dev api server
This commit is contained in:
parent
2e05578e9a
commit
70006ba31c
4 changed files with 40 additions and 26 deletions
|
@ -7,15 +7,17 @@ interface Config {
|
||||||
apiEndpoint: string;
|
apiEndpoint: string;
|
||||||
viewsDir: string;
|
viewsDir: string;
|
||||||
rootUrl: URL;
|
rootUrl: URL;
|
||||||
openid: {
|
openid:
|
||||||
issuer: string;
|
| false
|
||||||
client: {
|
| {
|
||||||
client_id: string;
|
issuer: string;
|
||||||
client_secret: string;
|
client: {
|
||||||
};
|
client_id: string;
|
||||||
request: HttpOptions;
|
client_secret: string;
|
||||||
};
|
};
|
||||||
key: KeyObject;
|
request: HttpOptions;
|
||||||
|
};
|
||||||
|
key: false | KeyObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type { Config };
|
export type { Config };
|
||||||
|
@ -31,18 +33,20 @@ function defaultConfig(): Config {
|
||||||
apiUrl: new URL(process.env.QUOT_API_URL ?? "http://127.0.0.1:3000"),
|
apiUrl: new URL(process.env.QUOT_API_URL ?? "http://127.0.0.1:3000"),
|
||||||
apiEndpoint: process.env.QUOT_API_ENDPOINT ?? "/api",
|
apiEndpoint: process.env.QUOT_API_ENDPOINT ?? "/api",
|
||||||
viewsDir: "views",
|
viewsDir: "views",
|
||||||
openid: {
|
openid: Boolean(process.env.QUOT_OPENID_ISSUER) && {
|
||||||
issuer: process.env.QUOT_OPENID_ISSUER ?? "",
|
issuer: process.env.QUOT_OPENID_ISSUER!,
|
||||||
client: {
|
client: {
|
||||||
client_id: process.env.QUOT_OPENID_CLIENT_ID ?? "",
|
client_id: process.env.QUOT_OPENID_CLIENT_ID!,
|
||||||
client_secret: process.env.QUOT_OPENID_CLIENT_SECRET ?? "",
|
client_secret: process.env.QUOT_OPENID_CLIENT_SECRET!,
|
||||||
},
|
},
|
||||||
request: { timeout: 5_000 },
|
request: { timeout: 5_000 },
|
||||||
},
|
},
|
||||||
key: crypto.createPrivateKey({
|
key:
|
||||||
key: JSON.parse(process.env.QUOT_JWK ?? "{}"),
|
Boolean(process.env.QUOT_JWK) &&
|
||||||
format: "jwk",
|
crypto.createPrivateKey({
|
||||||
}),
|
key: JSON.parse(process.env.QUOT_JWK!),
|
||||||
|
format: "jwk",
|
||||||
|
}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,21 @@ import { SignJWT } from "jose";
|
||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
|
|
||||||
async function login(fastify: FastifyInstance) {
|
async function login(fastify: FastifyInstance) {
|
||||||
custom.setHttpOptionsDefaults(fastify.config.openid.request);
|
const key = fastify.config.key;
|
||||||
const issuer = await Issuer.discover(fastify.config.openid.issuer);
|
if (!key) {
|
||||||
const client = new issuer.Client(fastify.config.openid.client);
|
fastify.log.warn("The key is required to use login endpoint.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const openid = fastify.config.openid;
|
||||||
|
if (!openid) {
|
||||||
|
fastify.log.warn(
|
||||||
|
"The openid parameters is required to use login endpoint."
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
custom.setHttpOptionsDefaults(openid.request);
|
||||||
|
const issuer = await Issuer.discover(openid.issuer);
|
||||||
|
const client = new issuer.Client(openid.client);
|
||||||
|
|
||||||
fastify.get("/login", async (request, reply) => {
|
fastify.get("/login", async (request, reply) => {
|
||||||
const params = client.callbackParams(request.raw);
|
const params = client.callbackParams(request.raw);
|
||||||
|
@ -22,7 +34,7 @@ async function login(fastify: FastifyInstance) {
|
||||||
.setProtectedHeader({ typ: "JWT", alg: "RS256" })
|
.setProtectedHeader({ typ: "JWT", alg: "RS256" })
|
||||||
.setExpirationTime("30days")
|
.setExpirationTime("30days")
|
||||||
.setSubject(userUrl.href)
|
.setSubject(userUrl.href)
|
||||||
.sign(fastify.config.key);
|
.sign(key);
|
||||||
const url = new URL(fastify.config.rootUrl);
|
const url = new URL(fastify.config.rootUrl);
|
||||||
url.hash = new URLSearchParams({ jwt }).toString();
|
url.hash = new URLSearchParams({ jwt }).toString();
|
||||||
return reply.redirect(url.href);
|
return reply.redirect(url.href);
|
||||||
|
|
|
@ -11,13 +11,12 @@ async function updatePage(
|
||||||
content: Pages.RequestContentPage
|
content: Pages.RequestContentPage
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const jwt = window.localStorage.getItem("jwt");
|
const jwt = window.localStorage.getItem("jwt");
|
||||||
if (!jwt) return false;
|
|
||||||
const res = await fetch(
|
const res = await fetch(
|
||||||
`${import.meta.env.QUOT_API_ENDPOINT}/pages?id=eq.${id}`,
|
`${import.meta.env.QUOT_API_ENDPOINT}/pages?id=eq.${id}`,
|
||||||
{
|
{
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: {
|
headers: {
|
||||||
authorization: `Bearer ${jwt}`,
|
...(jwt ? { authorization: `Bearer ${jwt}` } : {}),
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify(content),
|
body: JSON.stringify(content),
|
||||||
|
@ -28,12 +27,11 @@ async function updatePage(
|
||||||
|
|
||||||
async function deletePage(id: number): Promise<boolean> {
|
async function deletePage(id: number): Promise<boolean> {
|
||||||
const jwt = window.localStorage.getItem("jwt");
|
const jwt = window.localStorage.getItem("jwt");
|
||||||
if (!jwt) return false;
|
|
||||||
const res = await fetch(
|
const res = await fetch(
|
||||||
`${import.meta.env.QUOT_API_ENDPOINT}/pages?id=eq.${id}`,
|
`${import.meta.env.QUOT_API_ENDPOINT}/pages?id=eq.${id}`,
|
||||||
{
|
{
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: { authorization: `Bearer ${jwt}` },
|
headers: jwt ? { authorization: `Bearer ${jwt}` } : {},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return res.ok;
|
return res.ok;
|
||||||
|
|
|
@ -22,8 +22,8 @@ services:
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports: ["3000:3000"]
|
ports: ["3000:3000"]
|
||||||
environment:
|
environment:
|
||||||
QUOT_JWK: ${QUOT_JWK:?} # https://mkjwk.org
|
|
||||||
DATABASE_URL: postgresql://postgres:${POSTGRES_PASSWORD}@/postgres?host=/var/run/postgresql
|
DATABASE_URL: postgresql://postgres:${POSTGRES_PASSWORD}@/postgres?host=/var/run/postgresql
|
||||||
|
PGRST_DB_ANON_ROLE: postgres
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_socket:/var/run/postgresql
|
- postgres_socket:/var/run/postgresql
|
||||||
depends_on:
|
depends_on:
|
||||||
|
|
Loading…
Add table
Reference in a new issue