protonfile-auth 1.6.4 → 1.6.5

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.
Files changed (50) hide show
  1. package/README.md +22 -18
  2. package/lib/Entities/OTP.d.ts +10 -10
  3. package/lib/Entities/OTP.js +48 -48
  4. package/lib/Entities/Session.d.ts +10 -10
  5. package/lib/Entities/Session.js +36 -36
  6. package/lib/Entities/TempToken.d.ts +5 -5
  7. package/lib/Entities/TempToken.js +22 -22
  8. package/lib/Entities/User.d.ts +9 -9
  9. package/lib/Entities/User.js +34 -34
  10. package/lib/controllers/change_password.d.ts +3 -3
  11. package/lib/controllers/change_password.js +59 -58
  12. package/lib/controllers/login.d.ts +3 -3
  13. package/lib/controllers/login.js +40 -40
  14. package/lib/controllers/logout.d.ts +3 -3
  15. package/lib/controllers/logout.js +26 -26
  16. package/lib/controllers/qr.d.ts +3 -3
  17. package/lib/controllers/qr.js +55 -55
  18. package/lib/controllers/refresh_token.d.ts +3 -3
  19. package/lib/controllers/refresh_token.js +43 -43
  20. package/lib/controllers/register.d.ts +3 -3
  21. package/lib/controllers/register.js +57 -58
  22. package/lib/controllers/registerOtp.d.ts +3 -3
  23. package/lib/controllers/registerOtp.js +30 -30
  24. package/lib/index.d.ts +31 -29
  25. package/lib/index.js +58 -58
  26. package/lib/middlewares/appendSessionCache.d.ts +4 -4
  27. package/lib/middlewares/appendSessionCache.js +7 -7
  28. package/lib/middlewares/appendUms.d.ts +4 -4
  29. package/lib/middlewares/appendUms.js +7 -7
  30. package/lib/middlewares/verifyToken.d.ts +4 -4
  31. package/lib/middlewares/verifyToken.js +34 -34
  32. package/lib/services/OTPVacuum.d.ts +7 -7
  33. package/lib/services/OTPVacuum.js +31 -31
  34. package/lib/services/SessionCache.d.ts +8 -8
  35. package/lib/services/SessionCache.js +25 -25
  36. package/lib/services/SessionCleaner.d.ts +7 -7
  37. package/lib/services/SessionCleaner.js +35 -35
  38. package/lib/services/TempToken.d.ts +12 -9
  39. package/lib/services/TempToken.js +47 -40
  40. package/lib/services/UMS.d.ts +12 -12
  41. package/lib/services/UMS.js +31 -31
  42. package/lib/services/auth.d.ts +8 -8
  43. package/lib/services/auth.js +60 -60
  44. package/lib/services/mail/index.d.ts +36 -36
  45. package/lib/services/mail/index.js +101 -101
  46. package/lib/services/mail/templates/default.d.ts +2 -2
  47. package/lib/services/mail/templates/default.js +268 -268
  48. package/lib/services/session.d.ts +10 -10
  49. package/lib/services/session.js +81 -81
  50. package/package.json +52 -46
package/README.md CHANGED
@@ -1,18 +1,22 @@
1
- # protonfile-auth
2
-
3
- Authentication and authorization solution initially built for Protonfile but usable for any modern app. It was created as an Auth0 replacement for Protonfile.
4
-
5
- ## How does it work?
6
-
7
- protonfile-auth works with the traditional JWT access and refresh token model, refresh tokens are long-lived (currently hard-coded to 7 days) and access tokens are short-lived and new ones can be obtained with the refresh token.
8
-
9
- An Express middleware is exposed, which can be used to verify the users' access token before allowing them to access a resource.
10
-
11
- Refresh token versions are stored in the database, which enables token rotation. It also allows for an instant session revocation by the user, which in turn blocks any access token issuing for that session.
12
-
13
- ## Disadvantages
14
-
15
- protonfile-auth is in no means a perfect authentication solution, there are some known issues. This module was built to have a complete control over the authentication process but it surely can't compete with solutions like OAuth. **If you are building a professional application you should use more tested solutions than this**.
16
-
17
- - JWT is not advised as a session token because it's self contained with no central autority that can invalidate it. This is solved in protonfile-auth by saving those tokens in a database and removing them once a session is expired.
18
- - **Session/access token clutter**: there were some bad decisions during the developement which introduced the session tokens. Sessions are a nice way of knowing on which devices a user is authenticated, but it can be solved without having 2 tokens.
1
+ # protonfile-auth
2
+
3
+ Authentication and authorization solution initially built for Protonfile but usable for any modern app. It was created as an Auth0 replacement for Protonfile.
4
+
5
+ ## How does it work?
6
+
7
+ protonfile-auth works with the traditional JWT access and refresh token model, refresh tokens are long-lived (currently hard-coded to 7 days) and access tokens are short-lived and new ones can be obtained with the refresh token.
8
+
9
+ An Express middleware is exposed, which can be used to verify the users' access token before allowing them to access a resource.
10
+
11
+ Refresh token versions are stored in the database, which enables token rotation. It also allows for an instant session revocation by the user, which in turn blocks any access token issuing for that session.
12
+
13
+ ## Disadvantages
14
+
15
+ protonfile-auth is in no means a perfect authentication solution, there are some known issues. This module was built to have a complete control over the authentication process but it surely can't compete with solutions like OAuth. **If you are building a professional application you should use more tested solutions than this**.
16
+
17
+ - JWT is not advised as a session token because it's self contained with no central autority that can invalidate it. This is solved in protonfile-auth by saving those tokens in a database and removing them once a session is expired.
18
+
19
+ ## Advantages
20
+
21
+ - Access token revocation is istantaneous, each token is linked to a session and if the session is recoked (on logout or device kick) the access token is also invalidated. Auth middleware does not make a query to the database on each verification and instead keeps an in-memory cache of revoked sessions to be able to instantaneosly verify the token. Though the cache is in-memory all revoked sessions are also stored in the database till their expiration, this allows to recover them in case the server goes down.
22
+ - **Total control over your data**. All TypeORM entities are exported, which means that you can access your data from external TypeScript applications.
@@ -1,10 +1,10 @@
1
- import { BaseEntity } from 'typeorm';
2
- import { User } from './User';
3
- export declare class OTP extends BaseEntity {
4
- code: string;
5
- setCode(): void;
6
- scope: string;
7
- expiration: number;
8
- user_id: string;
9
- user: User;
10
- }
1
+ import { BaseEntity } from 'typeorm';
2
+ import { User } from './User';
3
+ export declare class OTP extends BaseEntity {
4
+ code: string;
5
+ setCode(): void;
6
+ scope: string;
7
+ expiration: number;
8
+ user_id: string;
9
+ user: User;
10
+ }
@@ -1,48 +1,48 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.OTP = void 0;
10
- const typeorm_1 = require("typeorm");
11
- const User_1 = require("./User");
12
- const nanoid_1 = require("nanoid");
13
- const randomOtp = (0, nanoid_1.customAlphabet)('1234567890', 6);
14
- let OTP = class OTP extends typeorm_1.BaseEntity {
15
- setCode() {
16
- this.code = randomOtp();
17
- }
18
- };
19
- __decorate([
20
- (0, typeorm_1.PrimaryColumn)({
21
- name: 'code',
22
- type: 'varchar',
23
- length: 6,
24
- })
25
- ], OTP.prototype, "code", void 0);
26
- __decorate([
27
- (0, typeorm_1.BeforeInsert)()
28
- ], OTP.prototype, "setCode", null);
29
- __decorate([
30
- (0, typeorm_1.Column)('text')
31
- ], OTP.prototype, "scope", void 0);
32
- __decorate([
33
- (0, typeorm_1.Column)({
34
- type: 'bigint',
35
- default: () => Date.now().toFixed(0),
36
- })
37
- ], OTP.prototype, "expiration", void 0);
38
- __decorate([
39
- (0, typeorm_1.Column)({ name: 'user_id', type: 'text' })
40
- ], OTP.prototype, "user_id", void 0);
41
- __decorate([
42
- (0, typeorm_1.ManyToOne)(() => User_1.User, { nullable: false }),
43
- (0, typeorm_1.JoinColumn)({ name: 'user_id' })
44
- ], OTP.prototype, "user", void 0);
45
- OTP = __decorate([
46
- (0, typeorm_1.Entity)()
47
- ], OTP);
48
- exports.OTP = OTP;
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.OTP = void 0;
10
+ const typeorm_1 = require("typeorm");
11
+ const User_1 = require("./User");
12
+ const nanoid_1 = require("nanoid");
13
+ const randomOtp = (0, nanoid_1.customAlphabet)('1234567890', 6);
14
+ let OTP = class OTP extends typeorm_1.BaseEntity {
15
+ setCode() {
16
+ this.code = randomOtp();
17
+ }
18
+ };
19
+ __decorate([
20
+ (0, typeorm_1.PrimaryColumn)({
21
+ name: 'code',
22
+ type: 'varchar',
23
+ length: 6,
24
+ })
25
+ ], OTP.prototype, "code", void 0);
26
+ __decorate([
27
+ (0, typeorm_1.BeforeInsert)()
28
+ ], OTP.prototype, "setCode", null);
29
+ __decorate([
30
+ (0, typeorm_1.Column)('text')
31
+ ], OTP.prototype, "scope", void 0);
32
+ __decorate([
33
+ (0, typeorm_1.Column)({
34
+ type: 'bigint',
35
+ default: () => Date.now().toFixed(0),
36
+ })
37
+ ], OTP.prototype, "expiration", void 0);
38
+ __decorate([
39
+ (0, typeorm_1.Column)({ name: 'user_id', type: 'text' })
40
+ ], OTP.prototype, "user_id", void 0);
41
+ __decorate([
42
+ (0, typeorm_1.ManyToOne)(() => User_1.User, { nullable: false }),
43
+ (0, typeorm_1.JoinColumn)({ name: 'user_id' })
44
+ ], OTP.prototype, "user", void 0);
45
+ OTP = __decorate([
46
+ (0, typeorm_1.Entity)()
47
+ ], OTP);
48
+ exports.OTP = OTP;
@@ -1,10 +1,10 @@
1
- import { BaseEntity } from 'typeorm';
2
- import { User } from './User';
3
- export declare class Session extends BaseEntity {
4
- session_id: string;
5
- user_id: string;
6
- user: User;
7
- user_agent: string;
8
- version: number;
9
- last_used: string;
10
- }
1
+ import { BaseEntity } from 'typeorm';
2
+ import { User } from './User';
3
+ export declare class Session extends BaseEntity {
4
+ session_id: string;
5
+ user_id: string;
6
+ user: User;
7
+ user_agent: string;
8
+ version: number;
9
+ last_used: string;
10
+ }
@@ -1,36 +1,36 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.Session = void 0;
10
- const typeorm_1 = require("typeorm");
11
- const User_1 = require("./User");
12
- let Session = class Session extends typeorm_1.BaseEntity {
13
- };
14
- __decorate([
15
- (0, typeorm_1.PrimaryGeneratedColumn)('uuid')
16
- ], Session.prototype, "session_id", void 0);
17
- __decorate([
18
- (0, typeorm_1.Column)({ name: 'user_id', type: 'text' })
19
- ], Session.prototype, "user_id", void 0);
20
- __decorate([
21
- (0, typeorm_1.ManyToOne)(() => User_1.User, { nullable: false }),
22
- (0, typeorm_1.JoinColumn)({ name: 'user_id' })
23
- ], Session.prototype, "user", void 0);
24
- __decorate([
25
- (0, typeorm_1.Column)('text')
26
- ], Session.prototype, "user_agent", void 0);
27
- __decorate([
28
- (0, typeorm_1.Column)('int')
29
- ], Session.prototype, "version", void 0);
30
- __decorate([
31
- (0, typeorm_1.Column)('bigint')
32
- ], Session.prototype, "last_used", void 0);
33
- Session = __decorate([
34
- (0, typeorm_1.Entity)()
35
- ], Session);
36
- exports.Session = Session;
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Session = void 0;
10
+ const typeorm_1 = require("typeorm");
11
+ const User_1 = require("./User");
12
+ let Session = class Session extends typeorm_1.BaseEntity {
13
+ };
14
+ __decorate([
15
+ (0, typeorm_1.PrimaryGeneratedColumn)('uuid')
16
+ ], Session.prototype, "session_id", void 0);
17
+ __decorate([
18
+ (0, typeorm_1.Column)({ name: 'user_id', type: 'text' })
19
+ ], Session.prototype, "user_id", void 0);
20
+ __decorate([
21
+ (0, typeorm_1.ManyToOne)(() => User_1.User, { nullable: false }),
22
+ (0, typeorm_1.JoinColumn)({ name: 'user_id' })
23
+ ], Session.prototype, "user", void 0);
24
+ __decorate([
25
+ (0, typeorm_1.Column)('text')
26
+ ], Session.prototype, "user_agent", void 0);
27
+ __decorate([
28
+ (0, typeorm_1.Column)('int')
29
+ ], Session.prototype, "version", void 0);
30
+ __decorate([
31
+ (0, typeorm_1.Column)('bigint')
32
+ ], Session.prototype, "last_used", void 0);
33
+ Session = __decorate([
34
+ (0, typeorm_1.Entity)()
35
+ ], Session);
36
+ exports.Session = Session;
@@ -1,5 +1,5 @@
1
- import { BaseEntity } from 'typeorm';
2
- export declare class TempToken extends BaseEntity {
3
- token: string;
4
- expires: number;
5
- }
1
+ import { BaseEntity } from 'typeorm';
2
+ export declare class TempToken extends BaseEntity {
3
+ token: string;
4
+ expires: number;
5
+ }
@@ -1,22 +1,22 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.TempToken = void 0;
10
- const typeorm_1 = require("typeorm");
11
- let TempToken = class TempToken extends typeorm_1.BaseEntity {
12
- };
13
- __decorate([
14
- (0, typeorm_1.PrimaryColumn)('text')
15
- ], TempToken.prototype, "token", void 0);
16
- __decorate([
17
- (0, typeorm_1.Column)('bigint')
18
- ], TempToken.prototype, "expires", void 0);
19
- TempToken = __decorate([
20
- (0, typeorm_1.Entity)()
21
- ], TempToken);
22
- exports.TempToken = TempToken;
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.TempToken = void 0;
10
+ const typeorm_1 = require("typeorm");
11
+ let TempToken = class TempToken extends typeorm_1.BaseEntity {
12
+ };
13
+ __decorate([
14
+ (0, typeorm_1.PrimaryColumn)('text')
15
+ ], TempToken.prototype, "token", void 0);
16
+ __decorate([
17
+ (0, typeorm_1.Column)('bigint')
18
+ ], TempToken.prototype, "expires", void 0);
19
+ TempToken = __decorate([
20
+ (0, typeorm_1.Entity)()
21
+ ], TempToken);
22
+ exports.TempToken = TempToken;
@@ -1,9 +1,9 @@
1
- import { BaseEntity } from 'typeorm';
2
- export declare class User extends BaseEntity {
3
- user_id: string;
4
- first_name: string;
5
- last_name: string;
6
- email: string;
7
- password: string;
8
- email_verified: boolean;
9
- }
1
+ import { BaseEntity } from 'typeorm';
2
+ export declare class User extends BaseEntity {
3
+ user_id: string;
4
+ first_name: string;
5
+ last_name: string;
6
+ email: string;
7
+ password: string;
8
+ email_verified: boolean;
9
+ }
@@ -1,34 +1,34 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.User = void 0;
10
- const typeorm_1 = require("typeorm");
11
- let User = class User extends typeorm_1.BaseEntity {
12
- };
13
- __decorate([
14
- (0, typeorm_1.PrimaryGeneratedColumn)('uuid')
15
- ], User.prototype, "user_id", void 0);
16
- __decorate([
17
- (0, typeorm_1.Column)('text')
18
- ], User.prototype, "first_name", void 0);
19
- __decorate([
20
- (0, typeorm_1.Column)('text')
21
- ], User.prototype, "last_name", void 0);
22
- __decorate([
23
- (0, typeorm_1.Column)('text')
24
- ], User.prototype, "email", void 0);
25
- __decorate([
26
- (0, typeorm_1.Column)('text')
27
- ], User.prototype, "password", void 0);
28
- __decorate([
29
- (0, typeorm_1.Column)({ name: 'email_verified', type: 'boolean', default: false })
30
- ], User.prototype, "email_verified", void 0);
31
- User = __decorate([
32
- (0, typeorm_1.Entity)()
33
- ], User);
34
- exports.User = User;
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.User = void 0;
10
+ const typeorm_1 = require("typeorm");
11
+ let User = class User extends typeorm_1.BaseEntity {
12
+ };
13
+ __decorate([
14
+ (0, typeorm_1.PrimaryGeneratedColumn)('uuid')
15
+ ], User.prototype, "user_id", void 0);
16
+ __decorate([
17
+ (0, typeorm_1.Column)('text')
18
+ ], User.prototype, "first_name", void 0);
19
+ __decorate([
20
+ (0, typeorm_1.Column)('text')
21
+ ], User.prototype, "last_name", void 0);
22
+ __decorate([
23
+ (0, typeorm_1.Column)('text')
24
+ ], User.prototype, "email", void 0);
25
+ __decorate([
26
+ (0, typeorm_1.Column)('text')
27
+ ], User.prototype, "password", void 0);
28
+ __decorate([
29
+ (0, typeorm_1.Column)({ name: 'email_verified', type: 'boolean', default: false })
30
+ ], User.prototype, "email_verified", void 0);
31
+ User = __decorate([
32
+ (0, typeorm_1.Entity)()
33
+ ], User);
34
+ exports.User = User;
@@ -1,3 +1,3 @@
1
- import { Request, Response } from 'express';
2
- export declare const get: (req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>;
3
- export declare const post: (req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>;
1
+ import { Request, Response } from 'express';
2
+ export declare const get: (req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>;
3
+ export declare const post: (req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>;
@@ -1,58 +1,59 @@
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
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.post = exports.get = void 0;
16
- const User_1 = require("../Entities/User");
17
- const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
18
- const mail_1 = require("../services/mail");
19
- const bcryptjs_1 = __importDefault(require("bcryptjs"));
20
- const Session_1 = require("../Entities/Session");
21
- const TempToken_1 = require("../services/TempToken");
22
- const get = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
23
- const user = yield User_1.User.findOne({ email: req.params.email });
24
- if (!user) {
25
- return res.sendStatus(404);
26
- }
27
- const token = jsonwebtoken_1.default.sign({ user_id: user.user_id }, process.env.CHANGE_PASSWORD_TOKEN_KEY, { expiresIn: '2h' });
28
- const email = new mail_1.ChangePasswordMail(user, (process.env.PROTONFILE_AUTH_APP_URL || '') + '/change_password/' + token);
29
- yield email.send();
30
- res.sendStatus(200);
31
- });
32
- exports.get = get;
33
- const post = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
34
- try {
35
- const token = jsonwebtoken_1.default.verify(req.body.token, process.env.CHANGE_PASSWORD_TOKEN_KEY);
36
- if (typeof token === 'string') {
37
- throw 'token malformed';
38
- }
39
- const temptoken = new TempToken_1.TempTokenService(req.body.token, token.exp || 0);
40
- if (!(yield temptoken.isValid())) {
41
- throw 'token expired';
42
- }
43
- const user = yield User_1.User.findOne({ user_id: token.user_id });
44
- if (!user) {
45
- return res.sendStatus(404);
46
- }
47
- User_1.User.update({ user_id: token.user_id }, { password: yield bcryptjs_1.default.hash(req.body.password, 10) });
48
- if (req.body.revoke_all) {
49
- yield Session_1.Session.delete({ user_id: user.user_id });
50
- }
51
- yield temptoken.invalidate();
52
- res.sendStatus(200);
53
- }
54
- catch (err) {
55
- res.sendStatus(500);
56
- }
57
- });
58
- exports.post = post;
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
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.post = exports.get = void 0;
16
+ const User_1 = require("../Entities/User");
17
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
18
+ const bcryptjs_1 = __importDefault(require("bcryptjs"));
19
+ const Session_1 = require("../Entities/Session");
20
+ const TempToken_1 = require("../services/TempToken");
21
+ const get = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
22
+ const user = yield User_1.User.findOne({ where: { email: req.params.email } });
23
+ if (!user) {
24
+ return res.sendStatus(404);
25
+ }
26
+ const token = jsonwebtoken_1.default.sign({ user_id: user.user_id }, process.env.CHANGE_PASSWORD_TOKEN_KEY, { expiresIn: '2h' });
27
+ const ums = req.ums;
28
+ ums.send('change_password', { user, token });
29
+ res.sendStatus(200);
30
+ });
31
+ exports.get = get;
32
+ const post = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
33
+ const temptoken = new TempToken_1.TempTokenService(req.body.token, 0);
34
+ try {
35
+ const token = jsonwebtoken_1.default.verify(req.body.token, process.env.CHANGE_PASSWORD_TOKEN_KEY);
36
+ if (typeof token === 'string') {
37
+ throw 'token malformed';
38
+ }
39
+ temptoken.setExpiration(token.exp || 0);
40
+ if (!(yield temptoken.isValid())) {
41
+ throw 'token expired';
42
+ }
43
+ const user = yield User_1.User.findOne({ where: { user_id: token.user_id } });
44
+ if (!user) {
45
+ return res.sendStatus(404);
46
+ }
47
+ yield User_1.User.update({ user_id: token.user_id }, { password: yield bcryptjs_1.default.hash(req.body.password, 10) });
48
+ if (req.body.revoke_all) {
49
+ yield Session_1.Session.delete({ user_id: user.user_id });
50
+ }
51
+ yield temptoken.invalidate();
52
+ res.sendStatus(200);
53
+ }
54
+ catch (err) {
55
+ res.sendStatus(500);
56
+ }
57
+ temptoken.close();
58
+ });
59
+ exports.post = post;
@@ -1,3 +1,3 @@
1
- import { Request, Response } from 'express';
2
- declare const _default: (req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>;
3
- export default _default;
1
+ import { Request, Response } from 'express';
2
+ declare const _default: (req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>;
3
+ export default _default;
@@ -1,40 +1,40 @@
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
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const bcryptjs_1 = __importDefault(require("bcryptjs"));
16
- const auth_1 = require("../services/auth");
17
- const User_1 = require("../Entities/User");
18
- exports.default = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
19
- try {
20
- const { email, password } = req.body;
21
- if (!(email && password)) {
22
- return res.status(400).send('All input is required');
23
- }
24
- const user = yield User_1.User.findOne({ email });
25
- if (!user) {
26
- return res.status(404).send('User not found');
27
- }
28
- if (!user.email_verified)
29
- return res.status(403).send('Email verification required');
30
- if (user && (yield bcryptjs_1.default.compare(password, user.password))) {
31
- // Create token
32
- const token = yield (0, auth_1.performLogin)(req, res, user);
33
- return res.status(200).json(Object.assign(Object.assign({}, user), { token }));
34
- }
35
- res.status(400).send('Invalid Credentials');
36
- }
37
- catch (err) {
38
- console.log(err);
39
- }
40
- });
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
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const bcryptjs_1 = __importDefault(require("bcryptjs"));
16
+ const auth_1 = require("../services/auth");
17
+ const User_1 = require("../Entities/User");
18
+ exports.default = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
19
+ try {
20
+ const { email, password } = req.body;
21
+ if (!(email && password)) {
22
+ return res.status(400).send('All input is required');
23
+ }
24
+ const user = yield User_1.User.findOne({ where: { email } });
25
+ if (!user) {
26
+ return res.status(404).send('User not found');
27
+ }
28
+ if (!user.email_verified)
29
+ return res.status(403).send('Email verification required');
30
+ if (user && (yield bcryptjs_1.default.compare(password, user.password))) {
31
+ // Create token
32
+ const token = yield (0, auth_1.performLogin)(req, res, user);
33
+ return res.status(200).json(Object.assign(Object.assign({}, user), { token }));
34
+ }
35
+ res.status(400).send('Invalid Credentials');
36
+ }
37
+ catch (err) {
38
+ console.log(err);
39
+ }
40
+ });
@@ -1,3 +1,3 @@
1
- import { Request, Response } from 'express';
2
- declare const _default: (req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>;
3
- export default _default;
1
+ import { Request, Response } from 'express';
2
+ declare const _default: (req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>;
3
+ export default _default;