authen-express 0.0.1 → 0.0.4

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