protonfile-auth 1.0.2 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Entities/Session.d.ts +9 -0
- package/lib/Entities/Session.js +3 -0
- package/lib/Entities/User.d.ts +8 -0
- package/lib/controllers/login.d.ts +3 -0
- package/lib/controllers/login.js +5 -6
- package/lib/controllers/logout.d.ts +3 -0
- package/lib/controllers/logout.js +1 -0
- package/lib/controllers/refresh_token.d.ts +3 -0
- package/lib/controllers/refresh_token.js +4 -8
- package/lib/controllers/register.d.ts +3 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.js +1 -0
- package/lib/middlewares/verifyToken.d.ts +3 -0
- package/lib/services/auth.d.ts +4 -0
- package/lib/services/auth.js +21 -1
- package/lib/services/session.d.ts +7 -0
- package/lib/services/session.js +12 -1
- package/package.json +2 -1
package/lib/Entities/Session.js
CHANGED
|
@@ -24,6 +24,9 @@ __decorate([
|
|
|
24
24
|
(0, typeorm_1.ManyToOne)(() => User_1.User, { nullable: false }),
|
|
25
25
|
(0, typeorm_1.JoinColumn)({ name: 'user_id' })
|
|
26
26
|
], Session.prototype, "user", void 0);
|
|
27
|
+
__decorate([
|
|
28
|
+
(0, typeorm_1.Column)('text')
|
|
29
|
+
], Session.prototype, "user_agent", void 0);
|
|
27
30
|
Session = __decorate([
|
|
28
31
|
(0, typeorm_1.Entity)()
|
|
29
32
|
], Session);
|
package/lib/controllers/login.js
CHANGED
|
@@ -30,17 +30,15 @@ exports.default = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
30
30
|
if (user && (yield bcryptjs_1.default.compare(password, user.password))) {
|
|
31
31
|
// Create token
|
|
32
32
|
const token = (0, auth_1.createAccessToken)({ user_id: user.user_id });
|
|
33
|
-
|
|
34
|
-
const weekExpiration = currentTime + 86400 * 7; // adds 7 days to timestamp
|
|
35
|
-
res.cookie('jid', (0, auth_1.createRefreshToken)({
|
|
33
|
+
(0, auth_1.setRefreshTokenCookie)(res, (0, auth_1.createRefreshToken)({
|
|
36
34
|
user_id: user.user_id,
|
|
37
|
-
})
|
|
35
|
+
}));
|
|
38
36
|
if (req.cookies.session_id) {
|
|
39
37
|
const session = JSON.parse(req.cookies.session_id);
|
|
40
38
|
try {
|
|
41
39
|
(0, session_1.verifySessionToken)(session.token);
|
|
42
40
|
const newSession = yield (0, session_1.bumpSessionToken)(session.session_id);
|
|
43
|
-
|
|
41
|
+
(0, session_1.setSessionCookie)(res, JSON.stringify(newSession));
|
|
44
42
|
}
|
|
45
43
|
catch (err) {
|
|
46
44
|
res.clearCookie('session_id');
|
|
@@ -51,6 +49,7 @@ exports.default = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
51
49
|
const insert = yield Session_1.Session.insert({
|
|
52
50
|
token: (0, session_1.createSessionToken)({ user_id: user.user_id }),
|
|
53
51
|
user_id: user.user_id,
|
|
52
|
+
user_agent: req.headers['user-agent'] || 'Unknown',
|
|
54
53
|
});
|
|
55
54
|
const session = yield Session_1.Session.findOne({
|
|
56
55
|
session_id: insert.generatedMaps[0].session_id,
|
|
@@ -58,7 +57,7 @@ exports.default = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
58
57
|
if (!session) {
|
|
59
58
|
return;
|
|
60
59
|
}
|
|
61
|
-
|
|
60
|
+
(0, session_1.setSessionCookie)(res, JSON.stringify(session));
|
|
62
61
|
}
|
|
63
62
|
return res.status(200).json(Object.assign(Object.assign({}, user), { token }));
|
|
64
63
|
}
|
|
@@ -29,10 +29,11 @@ exports.default = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
29
29
|
decodedSession = (0, session_1.verifySessionToken)(JSON.parse(session).token);
|
|
30
30
|
const parsedSession = JSON.parse(session);
|
|
31
31
|
(0, session_1.compareSessionTokenVersion)(parsedSession.session_id, parsedSession.token);
|
|
32
|
-
const newSession = (0, session_1.bumpSessionToken)(parsedSession.session_id);
|
|
33
|
-
|
|
32
|
+
const newSession = yield (0, session_1.bumpSessionToken)(parsedSession.session_id);
|
|
33
|
+
(0, session_1.setSessionCookie)(res, JSON.stringify(newSession));
|
|
34
34
|
}
|
|
35
35
|
catch (err) {
|
|
36
|
+
console.log(err);
|
|
36
37
|
res.clearCookie('session_id');
|
|
37
38
|
return res.sendStatus(403);
|
|
38
39
|
}
|
|
@@ -40,12 +41,7 @@ exports.default = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
40
41
|
if (!user) {
|
|
41
42
|
return res.sendStatus(404);
|
|
42
43
|
}
|
|
43
|
-
|
|
44
|
-
const weekExpiration = currentTime + 86400 * 7; // adds 7 days to timestamp
|
|
45
|
-
res.cookie('jid', (0, auth_1.createRefreshToken)({ user_id: user.user_id }), {
|
|
46
|
-
httpOnly: true,
|
|
47
|
-
expires: new Date(weekExpiration * 1000),
|
|
48
|
-
});
|
|
44
|
+
(0, auth_1.setRefreshTokenCookie)(res, (0, auth_1.createRefreshToken)({ user_id: user.user_id }));
|
|
49
45
|
return res.send({
|
|
50
46
|
ok: true,
|
|
51
47
|
accessToken: (0, auth_1.createAccessToken)({ user_id: user.user_id }),
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="qs" />
|
|
2
|
+
import 'dotenv/config';
|
|
3
|
+
import express from 'express';
|
|
4
|
+
import { ConnectionOptions } from 'typeorm';
|
|
5
|
+
import { User } from './Entities/User';
|
|
6
|
+
import { Session } from './Entities/Session';
|
|
7
|
+
declare const _default: {
|
|
8
|
+
router: (typeormConfig: ConnectionOptions) => import("express-serve-static-core").Router;
|
|
9
|
+
authMiddleware: (req: express.Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: express.Response<any, Record<string, any>>, next: express.NextFunction) => Promise<void | express.Response<any, Record<string, any>>>;
|
|
10
|
+
entities: {
|
|
11
|
+
User: typeof User;
|
|
12
|
+
Session: typeof Session;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export default _default;
|
package/lib/index.js
CHANGED
package/lib/services/auth.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createRefreshToken = exports.createAccessToken = void 0;
|
|
12
|
+
exports.setRefreshTokenCookie = exports.createRefreshToken = exports.createAccessToken = void 0;
|
|
4
13
|
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
5
14
|
const createAccessToken = (payload) => {
|
|
6
15
|
return (0, jsonwebtoken_1.sign)({ user_id: payload.user_id }, process.env.ACCESS_TOKEN_KEY, {
|
|
@@ -14,3 +23,14 @@ const createRefreshToken = (payload) => {
|
|
|
14
23
|
});
|
|
15
24
|
};
|
|
16
25
|
exports.createRefreshToken = createRefreshToken;
|
|
26
|
+
const setRefreshTokenCookie = (res, value) => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
+
const currentTime = Math.floor(new Date().getTime() / 1000);
|
|
28
|
+
const weekExpiration = currentTime + 86400 * 7; // adds 7 days to timestamp
|
|
29
|
+
res.cookie('jid', value, {
|
|
30
|
+
sameSite: 'none',
|
|
31
|
+
expires: new Date(weekExpiration * 1000),
|
|
32
|
+
httpOnly: true,
|
|
33
|
+
secure: process.env.NODE_ENV === 'production' ? true : false,
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
exports.setRefreshTokenCookie = setRefreshTokenCookie;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Response } from 'express';
|
|
2
|
+
import { Session } from '../Entities/Session';
|
|
3
|
+
export declare const createSessionToken: (payload: any, version?: number | undefined) => string;
|
|
4
|
+
export declare const verifySessionToken: (token: string) => string | import("jsonwebtoken").JwtPayload;
|
|
5
|
+
export declare const bumpSessionToken: (session_id: string) => Promise<Session>;
|
|
6
|
+
export declare const compareSessionTokenVersion: (session_id: string, token: string) => Promise<import("jsonwebtoken").JwtPayload>;
|
|
7
|
+
export declare const setSessionCookie: (res: Response, value: string) => Promise<void>;
|
package/lib/services/session.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.compareSessionTokenVersion = exports.bumpSessionToken = exports.verifySessionToken = exports.createSessionToken = void 0;
|
|
12
|
+
exports.setSessionCookie = exports.compareSessionTokenVersion = exports.bumpSessionToken = exports.verifySessionToken = exports.createSessionToken = void 0;
|
|
13
13
|
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
14
14
|
const Session_1 = require("../Entities/Session");
|
|
15
15
|
const createSessionToken = (payload, version) => {
|
|
@@ -65,3 +65,14 @@ const compareSessionTokenVersion = (session_id, token) => __awaiter(void 0, void
|
|
|
65
65
|
}
|
|
66
66
|
});
|
|
67
67
|
exports.compareSessionTokenVersion = compareSessionTokenVersion;
|
|
68
|
+
const setSessionCookie = (res, value) => __awaiter(void 0, void 0, void 0, function* () {
|
|
69
|
+
const currentTime = Math.floor(new Date().getTime() / 1000);
|
|
70
|
+
const weekExpiration = currentTime + 86400 * 7; // adds 7 days to timestamp
|
|
71
|
+
res.cookie('session_id', value, {
|
|
72
|
+
sameSite: 'none',
|
|
73
|
+
expires: new Date(weekExpiration * 1000),
|
|
74
|
+
httpOnly: true,
|
|
75
|
+
secure: process.env.NODE_ENV === 'production' ? true : false,
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
exports.setSessionCookie = setSessionCookie;
|
package/package.json
CHANGED