authen-express 0.0.1 → 0.0.2
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/index.js +19 -14
- package/package.json +1 -1
- package/src/index.ts +17 -12
package/lib/index.js
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
var AuthenticationController = (function () {
|
4
|
-
function AuthenticationController(log,
|
4
|
+
function AuthenticationController(log, login, cookie, decrypt) {
|
5
5
|
this.log = log;
|
6
|
-
this.
|
6
|
+
this.login = login;
|
7
7
|
this.cookie = cookie;
|
8
|
+
this.decrypt = decrypt;
|
8
9
|
this.authenticate = this.authenticate.bind(this);
|
9
10
|
}
|
10
11
|
AuthenticationController.prototype.authenticate = function (req, res) {
|
@@ -13,8 +14,17 @@ var AuthenticationController = (function () {
|
|
13
14
|
if (!user.username || user.username.length === 0 || !user.password || user.password.length === 0) {
|
14
15
|
res.status(401).end('username and password cannot be empty');
|
15
16
|
}
|
16
|
-
this.
|
17
|
-
var
|
17
|
+
if (this.decrypt) {
|
18
|
+
var p = this.decrypt(user.password);
|
19
|
+
if (p === undefined) {
|
20
|
+
return res.status(401).end('cannot decrypt password');
|
21
|
+
}
|
22
|
+
else {
|
23
|
+
user.password = p;
|
24
|
+
}
|
25
|
+
}
|
26
|
+
this.login(user).then(function (r) {
|
27
|
+
var account = r.user;
|
18
28
|
if (_this.cookie && account && account.token && account.tokenExpiredTime) {
|
19
29
|
res.status(200).cookie('token', account.token, {
|
20
30
|
sameSite: 'strict',
|
@@ -22,10 +32,10 @@ var AuthenticationController = (function () {
|
|
22
32
|
expires: account.tokenExpiredTime,
|
23
33
|
httpOnly: true,
|
24
34
|
secure: true,
|
25
|
-
}).json(
|
35
|
+
}).json(r).end();
|
26
36
|
}
|
27
37
|
else {
|
28
|
-
res.status(200).json(
|
38
|
+
res.status(200).json(r).end();
|
29
39
|
}
|
30
40
|
}).catch(function (err) { return handleError(err, res, _this.log); });
|
31
41
|
};
|
@@ -41,8 +51,8 @@ var PrivilegeController = (function () {
|
|
41
51
|
}
|
42
52
|
PrivilegeController.prototype.all = function (req, res) {
|
43
53
|
var _this = this;
|
44
|
-
this.privileges().then(function (
|
45
|
-
res.json(
|
54
|
+
this.privileges().then(function (r) {
|
55
|
+
res.json(r).end();
|
46
56
|
}).catch(function (err) { return handleError(err, res, _this.log); });
|
47
57
|
};
|
48
58
|
return PrivilegeController;
|
@@ -62,11 +72,6 @@ function handleError(err, res, log) {
|
|
62
72
|
}
|
63
73
|
exports.handleError = handleError;
|
64
74
|
function toString(v) {
|
65
|
-
|
66
|
-
return v;
|
67
|
-
}
|
68
|
-
else {
|
69
|
-
return JSON.stringify(v);
|
70
|
-
}
|
75
|
+
return typeof v === 'string' ? v : JSON.stringify(v);
|
71
76
|
}
|
72
77
|
exports.toString = toString;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
@@ -49,7 +49,7 @@ export interface UserAccount {
|
|
49
49
|
imageURL?: string;
|
50
50
|
}
|
51
51
|
export class AuthenticationController<T extends User> {
|
52
|
-
constructor (
|
52
|
+
constructor (public log: Log, public login: (user: T) => Promise<AuthResult>, public cookie?: boolean, public decrypt?: (cipherText: string) => string|undefined) {
|
53
53
|
this.authenticate = this.authenticate.bind(this);
|
54
54
|
}
|
55
55
|
authenticate(req: Request, res: Response) {
|
@@ -57,8 +57,16 @@ export class AuthenticationController<T extends User> {
|
|
57
57
|
if (!user.username || user.username.length === 0 || !user.password || user.password.length === 0) {
|
58
58
|
res.status(401).end('username and password cannot be empty');
|
59
59
|
}
|
60
|
-
this.
|
61
|
-
const
|
60
|
+
if (this.decrypt) {
|
61
|
+
const p = this.decrypt(user.password);
|
62
|
+
if (p === undefined) {
|
63
|
+
return res.status(401).end('cannot decrypt password');
|
64
|
+
} else {
|
65
|
+
user.password = p;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
this.login(user).then(r => {
|
69
|
+
const account = r.user;
|
62
70
|
if (this.cookie && account && account.token && account.tokenExpiredTime) {
|
63
71
|
res.status(200).cookie(
|
64
72
|
'token', account.token,
|
@@ -68,21 +76,22 @@ export class AuthenticationController<T extends User> {
|
|
68
76
|
expires: account.tokenExpiredTime,
|
69
77
|
httpOnly: true,
|
70
78
|
secure: true,
|
71
|
-
}).json(
|
79
|
+
}).json(r).end();
|
72
80
|
} else {
|
73
|
-
res.status(200).json(
|
81
|
+
res.status(200).json(r).end();
|
74
82
|
}
|
75
83
|
}).catch(err => handleError(err, res, this.log));
|
76
84
|
}
|
77
85
|
}
|
78
86
|
export const AuthenticationHandler = AuthenticationController;
|
87
|
+
// tslint:disable-next-line:max-classes-per-file
|
79
88
|
export class PrivilegeController {
|
80
89
|
constructor(private log: Log, public privileges: () => Promise<Privilege[]>) {
|
81
90
|
this.all = this.all.bind(this);
|
82
91
|
}
|
83
92
|
all(req: Request, res: Response) {
|
84
|
-
this.privileges().then(
|
85
|
-
res.json(
|
93
|
+
this.privileges().then(r => {
|
94
|
+
res.json(r).end();
|
86
95
|
}).catch(err => handleError(err, res, this.log));
|
87
96
|
}
|
88
97
|
}
|
@@ -98,9 +107,5 @@ export function handleError(err: any, res: Response, log?: (msg: string) => void
|
|
98
107
|
}
|
99
108
|
}
|
100
109
|
export function toString(v: any): string {
|
101
|
-
|
102
|
-
return v;
|
103
|
-
} else {
|
104
|
-
return JSON.stringify(v);
|
105
|
-
}
|
110
|
+
return typeof v === 'string' ? v : JSON.stringify(v);
|
106
111
|
}
|