-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
149ed9b
commit 6a5361d
Showing
11 changed files
with
145 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Application, PrismaClient } from "@prisma/client"; | ||
import { Request, Response } from "express"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
type ApplicationInfo = { | ||
appDomain: string; | ||
}; | ||
|
||
export async function applicationCreate(req: Request, res: Response) { | ||
try { | ||
|
||
if (!req.body.applicationInfo) throw Error("Application Info not present"); | ||
const { appDomain } = req.body.userInfo as ApplicationInfo; | ||
|
||
const app = await prisma.application | ||
.create({ | ||
data: { | ||
domain: appDomain, | ||
} | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
throw Error("Error while creating new application"); | ||
}) as Application; | ||
|
||
res.json(app); | ||
|
||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send("Internal Server Error"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import { Request, Response } from "express"; | ||
import { createApplicationJWT } from "../../crypto"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
interface ApplicationInfo { | ||
appUId: string; | ||
} | ||
|
||
export async function applicationToken(req: Request, res: Response) { | ||
try { | ||
|
||
// const appUId = res.locals.payload.appUId; | ||
if (!req.body.applicationInfo) throw Error("Application Info not present"); | ||
const { appUId } = req.body.userInfo as ApplicationInfo; | ||
const app = await prisma.application.findUnique({ | ||
where: { | ||
uid: appUId | ||
} | ||
}); | ||
|
||
const token = await createApplicationJWT(app); | ||
|
||
res.json(token); | ||
|
||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send("Internal Server Error"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
import { Router } from "express"; | ||
import { listApplications } from "./functions/listApplications"; | ||
import { applicationList } from "./functions/applicationList"; | ||
import { applicationCreate } from "./functions/applicationCreate"; | ||
import { applicationToken } from "./functions/applicationToken"; | ||
import { validateJWTToken } from "../validate"; | ||
|
||
export const adminRoutes = Router(); | ||
|
||
// const validateJWTToken = [validateWebAuthnToken]; | ||
|
||
adminRoutes.get( | ||
"/application/list", | ||
// validateJWTToken, | ||
async (req, res) => listApplications(req, res) | ||
validateJWTToken, | ||
async (req, res) => applicationList(req, res) | ||
); | ||
|
||
adminRoutes.post( | ||
"/application/token", | ||
validateJWTToken, | ||
async (req, res) => applicationToken(req, res) | ||
); | ||
|
||
adminRoutes.post( | ||
"/application/create", | ||
validateJWTToken, | ||
async (req, res) => applicationCreate(req, res) | ||
); |
41 changes: 0 additions & 41 deletions
41
backend/src/webauthn/validate.ts → ...validate/functions/validateApplication.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Request, Response } from "express"; | ||
import { checkForWebAuthnToken } from "../../redis"; | ||
import { decodeProtectedHeader, importJWK, jwtVerify } from "jose"; | ||
|
||
export async function validateWebAuthnToken( | ||
req: Request, | ||
res: Response, | ||
next: any | ||
) { | ||
console.log("validate webauthn token"); | ||
|
||
try { | ||
if (req.headers.authorization == null) return res.status(401).send(); | ||
|
||
const authorization = req.headers.authorization.split(" "); | ||
|
||
if (authorization[0] !== "Bearer") return res.status(401).send(); | ||
|
||
const jwt = authorization[1]; | ||
// check if jwt token was issued by us | ||
if ((await checkForWebAuthnToken(jwt)) === false) | ||
return res.status(401).send(); | ||
|
||
// extract public key from jwk parameters | ||
const header = decodeProtectedHeader(jwt); | ||
const algorithm = header.alg; | ||
const spki = header.jwk; | ||
|
||
if (algorithm !== "ES256" || spki == undefined) | ||
return res.status(401).send(); | ||
|
||
const importedKey = await importJWK(spki, algorithm); | ||
const { payload, protectedHeader } = await jwtVerify(jwt, importedKey, { | ||
issuer: "http://localhost:8080", | ||
algorithms: ["ES256"], | ||
}); | ||
res.locals.payload = payload; | ||
return next(); | ||
} catch (error) { | ||
console.log(error); | ||
return res.status(401).send(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { validateWebAuthnToken } from "./functions/validateWebAuthn"; | ||
import { validateApplicationToken } from "./functions/validateApplication"; | ||
|
||
export const validateAppToken = [validateApplicationToken]; | ||
export const validateJWTToken = [validateWebAuthnToken]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters