Skip to content
Snippets Groups Projects
Commit df899369 authored by Razzan Yoni's avatar Razzan Yoni
Browse files

fix : resolve auth

parent e377e82c
Branches
Tags
1 merge request!2Dev
......@@ -10,6 +10,7 @@ enum ErrorType {
ACCESS_TOKEN_MISSING,
ACCESS_TOKEN_EXPIRED,
ACCESS_TOKEN_NOT_ACTIVE,
INVALID_SIGNATURE,
AUTHORIZATION_HEADER_NOT_SET,
FINGERPRINT_MISSING,
ALBUM_NOT_FOUND,
......@@ -70,6 +71,11 @@ class StandardError {
this.status = StatusCodes.UNAUTHORIZED;
break;
case ErrorType.INVALID_SIGNATURE:
this.title = "Your access token is invalid."
this.status = StatusCodes.UNAUTHORIZED;
break;
case ErrorType.AUTHORIZATION_HEADER_NOT_SET:
this.title = "Authorization header not set."
this.status = StatusCodes.UNAUTHORIZED;
......
import { NextFunction, Request, Response } from "express";
import jwt, { NotBeforeError, TokenExpiredError } from "jsonwebtoken";
import jwt, {JsonWebTokenError, NotBeforeError, TokenExpiredError} from "jsonwebtoken";
import { ErrorType, StandardError } from "../errors/standard-error";
import { hashFingerprint } from "../utils/token";
......@@ -22,26 +22,26 @@ const verifyFingerprint = async (
};
const verifyToken = async (req: Request, res: Response, next: NextFunction) => {
try {
// Authorization Bearer ${accessToken}
const authHeader = req.headers.authorization;
const authHeader = req.headers.authorization;
if (!authHeader) {
throw new StandardError(ErrorType.AUTHORIZATION_HEADER_NOT_SET);
}
if (!authHeader) {
throw new StandardError(ErrorType.AUTHORIZATION_HEADER_NOT_SET);
}
const accessToken = authHeader.split(" ")[1];
const accessToken = authHeader.split(" ")[1];
if (!accessToken) {
throw new StandardError(ErrorType.ACCESS_TOKEN_MISSING);
}
if (!accessToken) {
throw new StandardError(ErrorType.ACCESS_TOKEN_MISSING);
}
const fingerprint = req.cookies["__Secure-fingerprint"];
const fingerprint = req.cookies["__Secure-fingerprint"];
if (!fingerprint) {
throw new StandardError(ErrorType.FINGERPRINT_MISSING);
}
if (!fingerprint) {
throw new StandardError(ErrorType.FINGERPRINT_MISSING);
}
try {
const decodedPayload = jwt.verify(
accessToken,
process.env.JWT_SHARED_SECRET as string,
......@@ -61,6 +61,10 @@ const verifyToken = async (req: Request, res: Response, next: NextFunction) => {
next(new StandardError(ErrorType.ACCESS_TOKEN_EXPIRED));
} else if (error instanceof NotBeforeError) {
next(new StandardError(ErrorType.ACCESS_TOKEN_NOT_ACTIVE));
} else if (error instanceof JsonWebTokenError) {
next(new StandardError(ErrorType.INVALID_SIGNATURE));
} else if (error instanceof StandardError) {
next(error);
}
// unknown error
next(error);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment