1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
7 | const isNil_1 = __importDefault(require("lodash/isNil"));
|
8 | const APIConfig_1 = require("./APIConfig");
|
9 | class APIAuthUser {
|
10 | }
|
11 | exports.APIAuthUser = APIAuthUser;
|
12 | class APIAuthCredentials extends APIAuthUser {
|
13 | }
|
14 | exports.APIAuthCredentials = APIAuthCredentials;
|
15 | class APIAuthUtils {
|
16 | static setJWTSessionCookie(res, jwtToken, domain) {
|
17 | let expiration = new Date(Number(new Date()) + 1.577e+11);
|
18 | res.cookie("session", jwtToken, { domain: domain, expires: expiration, httpOnly: true });
|
19 | }
|
20 | static deleteJWTSessionCookie(res, domain) {
|
21 | let expiration = new Date();
|
22 | res.cookie("session", "", { domain: domain, expires: expiration, httpOnly: true });
|
23 | }
|
24 | static getAuthCredentialsFromJWT(token, ignoreExpiration = true) {
|
25 | let authCreds = {
|
26 | isAuthenticated: false,
|
27 | isExpired: true,
|
28 | userID: null,
|
29 | userName: null
|
30 | };
|
31 | if (!token) {
|
32 | return authCreds;
|
33 | }
|
34 | try {
|
35 | const decodedAuthToken = jsonwebtoken_1.default.verify(token, APIConfig_1.APIConfig.JWT_SECRET, { ignoreExpiration });
|
36 | authCreds.isAuthenticated = true;
|
37 | authCreds.userName = decodedAuthToken.u;
|
38 | authCreds.userID = decodedAuthToken.i;
|
39 | authCreds.refreshToken = decodedAuthToken.r;
|
40 | authCreds.extraData = decodedAuthToken.ext;
|
41 | authCreds.rawJWTPayload = decodedAuthToken;
|
42 | if (decodedAuthToken.exp) {
|
43 | authCreds.expires = new Date(decodedAuthToken.exp * 1000);
|
44 | authCreds.isExpired = authCreds.expires <= (new Date());
|
45 | }
|
46 | }
|
47 | catch (err) {
|
48 | }
|
49 | return authCreds;
|
50 | }
|
51 | static getJWTFromRequest(req) {
|
52 | let token;
|
53 |
|
54 | if (req.cookies) {
|
55 | token = req.cookies.session;
|
56 | }
|
57 | if (isNil_1.default(token)) {
|
58 |
|
59 | token = req.get("Authorization");
|
60 | if (!isNil_1.default(token)) {
|
61 | token = token.replace(/^Bearer\s/, "");
|
62 | }
|
63 | }
|
64 | return token;
|
65 | }
|
66 | static getAuthCredentialsFromRequest(req, allowExpired = false) {
|
67 | return APIAuthUtils.getAuthCredentialsFromJWT(APIAuthUtils.getJWTFromRequest(req));
|
68 | }
|
69 | static createJWT(userID, username, expiresIn = "1h", refreshToken, extraData) {
|
70 | let options = undefined;
|
71 | if (expiresIn) {
|
72 | options = {
|
73 | expiresIn: expiresIn
|
74 | };
|
75 | }
|
76 | let payload = {
|
77 | i: userID,
|
78 | u: username
|
79 | };
|
80 | if (refreshToken) {
|
81 | payload.r = refreshToken;
|
82 | }
|
83 | if (extraData) {
|
84 | payload.ext = extraData;
|
85 | }
|
86 | return jsonwebtoken_1.default.sign(payload, APIConfig_1.APIConfig.JWT_SECRET, options);
|
87 | }
|
88 | static getAPIAuthUserFromAuthCredentials(authCredentials) {
|
89 | return {
|
90 | userID: authCredentials.userID,
|
91 | userName: authCredentials.userName,
|
92 | extraData: authCredentials.extraData
|
93 | };
|
94 | }
|
95 | }
|
96 | exports.APIAuthUtils = APIAuthUtils;
|
97 |
|
\ | No newline at end of file |