login and authentication
login via totp authentication via access and refresh tokens
This commit is contained in:
parent
6696975bee
commit
e1ec65350d
28 changed files with 3750 additions and 0 deletions
37
src/middleware/authenticate.ts
Normal file
37
src/middleware/authenticate.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { Request, Response } from "express";
|
||||
import jwt from "jsonwebtoken";
|
||||
import BadRequestException from "../exceptions/badRequestException";
|
||||
import UnauthorizedRequestException from "../exceptions/unauthorizedRequestException";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
import { JWTHelper } from "../helpers/jwtHelper";
|
||||
|
||||
export default async function authenticate(req: Request, res: Response, next: Function) {
|
||||
const bearer = req.headers.authorization;
|
||||
|
||||
if (!bearer) {
|
||||
throw new BadRequestException("Provide Authorization Header");
|
||||
}
|
||||
|
||||
let decoded: string | jwt.JwtPayload;
|
||||
await JWTHelper.validate(bearer)
|
||||
.then((result) => {
|
||||
decoded = result;
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err == "jwt expired") {
|
||||
throw new UnauthorizedRequestException("Token expired");
|
||||
} else {
|
||||
throw new BadRequestException("Failed Authorization Header decoding");
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof decoded == "string" || !decoded) {
|
||||
throw new InternalException("process failed");
|
||||
}
|
||||
|
||||
req.userId = decoded.userId;
|
||||
req.username = decoded.username;
|
||||
req.rights = decoded.rights;
|
||||
|
||||
next();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue