Skip to content

Commit fbdb11c

Browse files
committed
finish restructure
1 parent f78700b commit fbdb11c

14 files changed

+77
-109
lines changed

backend/dashboard/src/services/wembat.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class WembatRequestService {
2828

2929
public async applicationUpdate(data: any): Promise<boolean> {
3030
try {
31-
await axios.post(`${this.tokenStore.apiUrl}/admin/application/update`, data, {
31+
await axios.post(`${this.tokenStore.apiUrl}/api/application/update`, data, {
3232
headers: {
3333
Authorization: `Bearer ${this.tokenStore.token}`,
3434
},
@@ -42,7 +42,7 @@ export class WembatRequestService {
4242

4343
public async applicationList(): Promise<Application[]> {
4444
try {
45-
let listRequest = await axios.get(`${this.tokenStore.apiUrl}/admin/application/list`, {
45+
let listRequest = await axios.get(`${this.tokenStore.apiUrl}/api/application/list`, {
4646
headers: {
4747
Authorization: `Bearer ${this.tokenStore.token}`,
4848
},
@@ -56,7 +56,7 @@ export class WembatRequestService {
5656

5757
public async applicationCreate(data: any): Promise<boolean> {
5858
try {
59-
await axios.post(`${this.tokenStore.apiUrl}/admin/application/create`, data, {
59+
await axios.post(`${this.tokenStore.apiUrl}/api/application/create`, data, {
6060
headers: {
6161
Authorization: `Bearer ${this.tokenStore.token}`,
6262
},
@@ -70,7 +70,7 @@ export class WembatRequestService {
7070

7171
public async applicationDelete(data: any): Promise<boolean> {
7272
try {
73-
await axios.post(`${this.tokenStore.apiUrl}/admin/application/delete`, data, {
73+
await axios.post(`${this.tokenStore.apiUrl}/api/application/delete`, data, {
7474
headers: {
7575
Authorization: `Bearer ${this.tokenStore.token}`,
7676
},
@@ -84,7 +84,7 @@ export class WembatRequestService {
8484

8585
public async applicationToken(data: any): Promise<string> {
8686
try {
87-
let tokenRequest = await axios.post(`${this.tokenStore.apiUrl}/admin/application/token`, data, {
87+
let tokenRequest = await axios.post(`${this.tokenStore.apiUrl}/api/application/token`, data, {
8888
headers: {
8989
Authorization: `Bearer ${this.tokenStore.token}`,
9090
},

backend/src/api/application/applicationCreate.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Request, Response } from "express";
22
import { Application, PrismaClient } from "@prisma/client";
33
import { ApplicationInfo } from "../types";
4-
import { applicationKeys } from "../../application";
4+
import { domainWhitelist } from "../../app";
55

66
const prisma = new PrismaClient();
77

@@ -24,7 +24,7 @@ export async function applicationCreate(req: Request, res: Response) {
2424
}) as Application;
2525

2626
const appUrl = `https://${app.domain}`;
27-
applicationKeys.push(appUrl);
27+
domainWhitelist.push(appUrl);
2828

2929
res.status(200).send();
3030

backend/src/api/application/applicationDelete.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Application, PrismaClient } from "@prisma/client";
22
import { Request, Response } from "express";
33
import { ApplicationInfo } from "../types";
4-
import { applicationKeys } from "../../application";
4+
import { domainWhitelist } from "../../app";
55

66
const prisma = new PrismaClient();
77

@@ -23,10 +23,10 @@ export async function applicationDelete(req: Request, res: Response) {
2323
}) as Application;
2424

2525
const appUrl = `https://${app.domain}`;
26-
const index = applicationKeys.indexOf(appUrl);
26+
const index = domainWhitelist.indexOf(appUrl);
2727

2828
if (index !== -1) {
29-
applicationKeys.splice(index, 1);
29+
domainWhitelist.splice(index, 1);
3030
}
3131

3232
res.status(200).send();

backend/src/api/application/applicationUpdate.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Application, PrismaClient } from "@prisma/client";
22
import { Request, Response } from "express";
33
import { ApplicationInfo } from "../types";
4-
import { applicationKeys } from "../../application";
4+
import { domainWhitelist } from "../../app";
55

66
const prisma = new PrismaClient();
77

@@ -38,11 +38,11 @@ export async function applicationUpdate(req: Request, res: Response) {
3838
}) as Application;
3939

4040
const appUrl = `https://${tempApp.domain}`;
41-
const index = applicationKeys.indexOf(appUrl);
41+
const index = domainWhitelist.indexOf(appUrl);
4242

4343
if (index !== -1) {
4444
const newAppUrl = `https://${app.domain}`;
45-
applicationKeys[index] = newAppUrl;
45+
domainWhitelist[index] = newAppUrl;
4646
}
4747

4848
res.status(200).send();

backend/src/api/index.ts

+22-25
Original file line numberDiff line numberDiff line change
@@ -5,106 +5,103 @@ import { applicationCreate } from "./application/applicationCreate";
55
import { applicationToken } from "./application/applicationToken";
66
import { applicationUpdate } from "./application/applicationUpdate";
77
import { applicationDelete } from "./application/applicationDelete";
8-
import { createAdminJWT } from "../crypto";
98
import { requestRegister } from "./webauthn/requestRegister";
10-
import { validateAdminFunctions, validateAppFunctions, validateWebAuthnFunctions } from "./validate";
119
import { register } from "./webauthn/register";
1210
import { refresh } from "./webauthn/refresh";
1311
import { onboard } from "./webauthn/onboard";
1412
import { requestOnboard } from "./webauthn/requestOnboard";
1513
import { updateCredentials } from "./webauthn/updateCredentials";
1614
import { requestLogin } from "./webauthn/requestLogin";
1715
import { login } from "./webauthn/login";
16+
import { validateWebAuthnToken } from "./validate/validateWebAuthn";
17+
import { validateApplicationToken } from "./validate/validateApplication";
18+
import { validateAdminToken } from "./validate/validateAdmin";
19+
import { serverExportPublicKey } from "./server/serverExportPublicKey";
1820

1921
export const apiRouter = Router();
2022

2123
const dashboardUrl = process.env.DASHBOARD_URL || "http://localhost:9090";
2224

2325
apiRouter.get(
2426
"/application/list",
25-
validateAdminFunctions,
27+
[validateAdminToken],
2628
async (req: Request, res: Response) => applicationList(req, res)
2729
);
2830

2931
apiRouter.post(
3032
"/application/token",
31-
validateAdminFunctions,
33+
[validateAdminToken],
3234
async (req: Request, res: Response) => applicationToken(req, res)
3335
);
3436

3537
apiRouter.post(
3638
"/application/create",
37-
validateAdminFunctions,
39+
[validateAdminToken],
3840
async (req: Request, res: Response) => applicationCreate(req, res)
3941
);
4042

4143
apiRouter.post(
4244
"/application/update",
43-
validateAdminFunctions,
45+
[validateAdminToken],
4446
async (req: Request, res: Response) => applicationUpdate(req, res)
4547
);
4648

4749
apiRouter.post(
4850
"/application/delete",
49-
validateAdminFunctions,
51+
[validateAdminToken],
5052
async (req: Request, res: Response) => applicationDelete(req, res)
5153
);
5254

5355
apiRouter.post(
5456
"/webauthn/request-register",
55-
validateAppFunctions,
57+
[validateApplicationToken],
5658
async (req: Request, res: Response) => requestRegister(req, res)
5759
);
5860

5961
apiRouter.post(
6062
"/webauthn/register",
61-
validateAppFunctions,
63+
[validateApplicationToken],
6264
async (req: Request, res: Response) => register(req, res)
6365
);
6466

6567
apiRouter.post(
6668
"/webauthn/request-login",
67-
validateAppFunctions,
69+
[validateApplicationToken],
6870
async (req: Request, res: Response) => requestLogin(req, res)
6971
);
7072

7173
apiRouter.post(
7274
"/webauthn/login",
73-
validateAppFunctions,
75+
[validateApplicationToken],
7476
async (req: Request, res: Response) => login(req, res)
7577
);
7678

7779
apiRouter.post(
7880
"/webauthn/update-credentials",
79-
validateWebAuthnFunctions,
81+
[validateWebAuthnToken],
8082
async (req: Request, res: Response) => updateCredentials(req, res)
8183
);
8284

8385
apiRouter.post(
8486
"/webauthn/request-onboard",
85-
validateWebAuthnFunctions,
87+
[validateWebAuthnToken],
8688
async (req: Request, res: Response) => requestOnboard(req, res)
8789
);
8890

8991
apiRouter.post(
9092
"/webauthn/onboard",
91-
validateWebAuthnFunctions,
93+
[validateWebAuthnToken],
9294
async (req: Request, res: Response) => onboard(req, res)
9395
);
9496

9597
apiRouter.post(
9698
"/webauthn/refresh-token",
97-
validateAppFunctions,
99+
[validateApplicationToken],
98100
async (req: Request, res: Response) => refresh(req, res)
99101
)
100102

101-
export async function initAdmin(): Promise<boolean> {
102-
try {
103-
const token = await createAdminJWT();
104-
console.log(`Dashboard Url: ${dashboardUrl}/${token}`);
105-
return true;
106-
} catch (err) {
107-
console.error(err);
108-
return false;
109-
}
110-
}
103+
apiRouter.get(
104+
"/server/publicKey",
105+
[validateApplicationToken],
106+
async (req: Request, res: Response) => serverExportPublicKey(req, res)
107+
);

backend/src/api/server/index.ts

-20
This file was deleted.

backend/src/api/server/functions/serverExportPublicKey.ts backend/src/api/server/serverExportPublicKey.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Response } from "express";
2-
import { exportPublicKey } from "../../../crypto";
2+
import { exportPublicKey } from "../../crypto";
33

44
export async function serverExportPublicKey(req: Request, res: Response) {
55
try {

backend/src/api/validate/index.ts

-7
This file was deleted.

backend/src/api/validate/functions/validateAdmin.ts backend/src/api/validate/validateAdmin.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Request, Response } from "express";
2-
import { checkForWebAuthnToken } from "../../../redis";
32
import { decodeProtectedHeader, importJWK, jwtVerify } from "jose";
4-
import { publicKeyJwk } from "../../../crypto";
3+
import { publicKeyJwk } from "../../crypto";
54

65
const serverUrl = process.env.SERVER_URL || "http://localhost:8080";
76

backend/src/api/validate/functions/validateApplication.ts backend/src/api/validate/validateApplication.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response } from "express";
22
import { decodeProtectedHeader, importJWK, jwtVerify } from "jose";
3-
import { publicKeyJwk } from "../../../crypto";
3+
import { publicKeyJwk } from "../../crypto";
44

55
const serverUrl = process.env.SERVER_URL || "http://localhost:8080";
66

backend/src/api/validate/functions/validateWebAuthn.ts backend/src/api/validate/validateWebAuthn.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Request, Response } from "express";
2-
import { checkForWebAuthnToken } from "../../../redis";
32
import { decodeProtectedHeader, importJWK, jwtVerify } from "jose";
4-
import { publicKeyJwk } from "../../../crypto";
3+
import { publicKeyJwk } from "../../crypto";
54

65
const serverUrl = process.env.SERVER_URL || "http://localhost:8080";
76

backend/src/app.ts

+37-12
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import helmet from "helmet";
33
import express from "express";
44
import bodyParser from "body-parser";
55
import compression from "compression";
6-
76
import { rateLimit} from "express-rate-limit";
7+
8+
import { apiRouter } from "./api";
89
import { initRedis } from "./redis";
9-
import { initCrypto } from "./crypto";
10-
import { applicationKeys, initApplications } from "./application";
11-
import { apiRouter, initAdmin } from "./api";
12-
import { initServer } from "./api/server";
10+
import { PrismaClient } from "@prisma/client";
11+
import { createAdminJWT, initCrypto } from "./crypto";
1312

1413
const port = 8080;
14+
const dashboardUrl = process.env.DASHBOARD_URL || "http://localhost:9090";
15+
const prisma = new PrismaClient();
16+
export const domainWhitelist = new Array<string>();
1517

1618
const limiter = rateLimit({
1719
windowMs: 15 * 60 * 1000, // 15 minutes
@@ -20,11 +22,6 @@ const limiter = rateLimit({
2022

2123
async function init() {
2224

23-
if (!await initServer()) {
24-
console.error("Failed to initialize server");
25-
return;
26-
}
27-
2825
if (!await initCrypto()) {
2926
console.error("Failed to initialize crypto");
3027
return;
@@ -40,7 +37,7 @@ async function init() {
4037
return;
4138
}
4239

43-
if (!await initApplications()) {
40+
if (!await initWhitelist()) {
4441
console.error("Failed to initialize applications");
4542
return;
4643
}
@@ -52,7 +49,7 @@ async function init() {
5249

5350
const origin = req.header("Origin");
5451
const method = req.method;
55-
const isDomainAllowed = applicationKeys.indexOf(origin) !== -1;
52+
const isDomainAllowed = domainWhitelist.indexOf(origin) !== -1;
5653

5754
console.log(`Request from ${origin} with method ${method} is allowed: ${isDomainAllowed}`);
5855

@@ -93,3 +90,31 @@ async function init() {
9390
}
9491

9592
init();
93+
94+
async function initAdmin(): Promise<boolean> {
95+
try {
96+
const token = await createAdminJWT();
97+
console.log(`Dashboard Url: ${dashboardUrl}/${token}`);
98+
return true;
99+
} catch (err) {
100+
console.error(err);
101+
return false;
102+
}
103+
}
104+
105+
async function initWhitelist(): Promise<boolean> {
106+
try {
107+
108+
const apps = await prisma.application.findMany();
109+
110+
for (const app of apps) {
111+
const appUrl = `https://${app.domain}`;
112+
domainWhitelist.push(appUrl);
113+
}
114+
domainWhitelist.push(dashboardUrl);
115+
return true;
116+
} catch (err) {
117+
console.error(err);
118+
return false;
119+
}
120+
}

0 commit comments

Comments
 (0)