-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
36 lines (30 loc) · 1.01 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as jwt from "jsonwebtoken";
export const verifyAndDecodeToken = function(context){
if(!context || !context.req || !context.req.headers || !context.req.headers.authorization) {
throw new AccessControlError("No authorization token.");
}
const token = context.req.headers.authorization;
try {
const id_token = token.replace("Bearer ", "");
const { JWT_SECRET, JWT_NO_VERIFY } = process.env;
if (JWT_NO_VERIFY) {
return jwt.decode(id_token);
} else {
return jwt.verify(id_token, JWT_SECRET, {
algorithms: ["HS256", "RS256"]
});
}
} catch (err) {
if (err.name === "TokenExpiredError") {
throw new Error("Your token is expired.");
} else {
throw new Error("Could not verify and decode token.");
}
}
};
export class AccessControlError extends Error{
constructor(message) {
super(message);
this.name = "AccessControlError"
}
}