mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 20:25:43 +00:00
Added: - New error types such as `IntegrationNotFoundError`, `WorkspaceNotFoundError`, `AccountNotFoundError' and more. Refactored: - Refactored most of the middlewares and very little number of helper functions to use RequestError - Deleted unused imports Changed: - Some of the error types in middlewares changed to more related error types. - Environment variable of 'VERBOSE_ERROR_OUTPUT' changed to more reliable validation method in `config/index.ts` as per @dangtony98 requested.
45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import jwt from 'jsonwebtoken';
|
|
import { Request, Response, NextFunction } from 'express';
|
|
import { User } from '../models';
|
|
import { JWT_SIGNUP_SECRET } from '../config';
|
|
import { BadRequestError, UnauthorizedRequestError } from '../utils/errors';
|
|
|
|
declare module 'jsonwebtoken' {
|
|
export interface UserIDJwtPayload extends jwt.JwtPayload {
|
|
userId: string;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate if JWT temporary token on request is valid (e.g. not expired)
|
|
* and if there is an associated user.
|
|
*/
|
|
const requireSignupAuth = async (
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
) => {
|
|
// JWT (temporary) authentication middleware for complete signup
|
|
|
|
const [ AUTH_TOKEN_TYPE, AUTH_TOKEN_VALUE ] = <[string, string]>req.headers['authorization']?.split(' ', 2) ?? [null, null]
|
|
if(AUTH_TOKEN_TYPE === null) return next(BadRequestError({message: `Missing Authorization Header in the request header.`}))
|
|
if(AUTH_TOKEN_TYPE.toLowerCase() !== 'bearer') return next(BadRequestError({message: `The provided authentication type '${AUTH_TOKEN_TYPE}' is not supported.`}))
|
|
if(AUTH_TOKEN_VALUE === null) return next(BadRequestError({message: 'Missing Authorization Body in the request header'}))
|
|
|
|
const decodedToken = <jwt.UserIDJwtPayload>(
|
|
jwt.verify(AUTH_TOKEN_VALUE, JWT_SIGNUP_SECRET)
|
|
);
|
|
|
|
const user = await User.findOne({
|
|
_id: decodedToken.userId
|
|
}).select('+publicKey');
|
|
|
|
if (!user)
|
|
return next(UnauthorizedRequestError({message: 'Unable to authenticate for User account completion. Try logging in again'}))
|
|
|
|
req.user = user;
|
|
return next();
|
|
};
|
|
|
|
export default requireSignupAuth;
|