authen-express 0.0.1

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # authen-express
package/lib/index.js ADDED
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var AuthenticationController = (function () {
4
+ function AuthenticationController(log, auth, cookie) {
5
+ this.log = log;
6
+ this.auth = auth;
7
+ this.cookie = cookie;
8
+ this.authenticate = this.authenticate.bind(this);
9
+ }
10
+ AuthenticationController.prototype.authenticate = function (req, res) {
11
+ var _this = this;
12
+ 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');
15
+ }
16
+ this.auth(user).then(function (result) {
17
+ var account = result.user;
18
+ if (_this.cookie && account && account.token && account.tokenExpiredTime) {
19
+ res.status(200).cookie('token', account.token, {
20
+ sameSite: 'strict',
21
+ path: '/',
22
+ expires: account.tokenExpiredTime,
23
+ httpOnly: true,
24
+ secure: true,
25
+ }).json(result).end();
26
+ }
27
+ else {
28
+ res.status(200).json(result).end();
29
+ }
30
+ }).catch(function (err) { return handleError(err, res, _this.log); });
31
+ };
32
+ return AuthenticationController;
33
+ }());
34
+ exports.AuthenticationController = AuthenticationController;
35
+ exports.AuthenticationHandler = AuthenticationController;
36
+ var PrivilegeController = (function () {
37
+ function PrivilegeController(log, privileges) {
38
+ this.log = log;
39
+ this.privileges = privileges;
40
+ this.all = this.all.bind(this);
41
+ }
42
+ PrivilegeController.prototype.all = function (req, res) {
43
+ var _this = this;
44
+ this.privileges().then(function (result) {
45
+ res.json(result).end();
46
+ }).catch(function (err) { return handleError(err, res, _this.log); });
47
+ };
48
+ return PrivilegeController;
49
+ }());
50
+ exports.PrivilegeController = PrivilegeController;
51
+ exports.PrivilegesController = PrivilegeController;
52
+ exports.PrivilegeHandler = PrivilegeController;
53
+ exports.PrivilegesHandler = PrivilegeController;
54
+ function handleError(err, res, log) {
55
+ if (log) {
56
+ log(toString(err));
57
+ res.status(500).end('Internal Server Error');
58
+ }
59
+ else {
60
+ res.status(500).end(toString(err));
61
+ }
62
+ }
63
+ exports.handleError = handleError;
64
+ function toString(v) {
65
+ if (typeof v === 'string') {
66
+ return v;
67
+ }
68
+ else {
69
+ return JSON.stringify(v);
70
+ }
71
+ }
72
+ exports.toString = toString;
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "authen-express",
3
+ "version": "0.0.1",
4
+ "description": "authen-express",
5
+ "main": "./lib/index.js",
6
+ "types": "./src/index.ts",
7
+ "scripts": {
8
+ "build:lib": "tsc",
9
+ "clean:lib": "rimraf lib"
10
+ },
11
+ "devDependencies": {
12
+ "@types/express": "^4.17.13",
13
+ "express": "^4.17.1",
14
+ "tslint": "5.10.0",
15
+ "typescript": "^3.3.3333"
16
+ },
17
+ "publishConfig": {
18
+ "registry": "https://registry.npmjs.org/"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git@github.com/core-ts/authen-express"
23
+ },
24
+ "keywords": [
25
+ "authen-express"
26
+ ]
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,106 @@
1
+ import { Request, Response } from 'express';
2
+
3
+ export type Log = (msg: string) => void;
4
+ export interface User {
5
+ step?: number;
6
+ username: string;
7
+ password: string;
8
+ passcode?: string;
9
+ ip?: string;
10
+ device?: string;
11
+ }
12
+ export type Login = User;
13
+ export type AuthInfo = User;
14
+ export interface Privilege {
15
+ id?: string;
16
+ name: string;
17
+ resource?: string;
18
+ path?: string;
19
+ icon?: string;
20
+ sequence?: number;
21
+ children?: Privilege[];
22
+ permissions?: number;
23
+ }
24
+ export interface AuthResult {
25
+ status: number | string;
26
+ user?: UserAccount;
27
+ message?: string;
28
+ }
29
+ export type Result = AuthResult;
30
+ export type LoginResult = AuthResult;
31
+ export interface UserAccount {
32
+ id?: string;
33
+ username?: string;
34
+ contact?: string;
35
+ email?: string;
36
+ phone?: string;
37
+ displayName?: string;
38
+ passwordExpiredTime?: Date;
39
+ token?: string;
40
+ tokenExpiredTime?: Date;
41
+ newUser?: boolean;
42
+ userType?: string;
43
+ roles?: string[];
44
+ privileges?: Privilege[];
45
+ language?: string;
46
+ dateFormat?: string;
47
+ timeFormat?: string;
48
+ gender?: string;
49
+ imageURL?: string;
50
+ }
51
+ export class AuthenticationController<T extends User> {
52
+ constructor (private log: Log, private auth: (user: T) => Promise<AuthResult>, public cookie?: boolean) {
53
+ this.authenticate = this.authenticate.bind(this);
54
+ }
55
+ authenticate(req: Request, res: Response) {
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');
59
+ }
60
+ this.auth(user).then(result => {
61
+ const account = result.user;
62
+ if (this.cookie && account && account.token && account.tokenExpiredTime) {
63
+ res.status(200).cookie(
64
+ 'token', account.token,
65
+ {
66
+ sameSite: 'strict',
67
+ path: '/',
68
+ expires: account.tokenExpiredTime,
69
+ httpOnly: true,
70
+ secure: true,
71
+ }).json(result).end();
72
+ } else {
73
+ res.status(200).json(result).end();
74
+ }
75
+ }).catch(err => handleError(err, res, this.log));
76
+ }
77
+ }
78
+ export const AuthenticationHandler = AuthenticationController;
79
+ export class PrivilegeController {
80
+ constructor(private log: Log, public privileges: () => Promise<Privilege[]>) {
81
+ this.all = this.all.bind(this);
82
+ }
83
+ all(req: Request, res: Response) {
84
+ this.privileges().then(result => {
85
+ res.json(result).end();
86
+ }).catch(err => handleError(err, res, this.log));
87
+ }
88
+ }
89
+ export const PrivilegesController = PrivilegeController;
90
+ export const PrivilegeHandler = PrivilegeController;
91
+ export const PrivilegesHandler = PrivilegeController;
92
+ export function handleError(err: any, res: Response, log?: (msg: string) => void) {
93
+ if (log) {
94
+ log(toString(err));
95
+ res.status(500).end('Internal Server Error');
96
+ } else {
97
+ res.status(500).end(toString(err));
98
+ }
99
+ }
100
+ export function toString(v: any): string {
101
+ if (typeof v === 'string') {
102
+ return v;
103
+ } else {
104
+ return JSON.stringify(v);
105
+ }
106
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "compileOnSave": true,
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "baseUrl": "./src",
6
+ "outDir": "./lib",
7
+ "module": "commonjs",
8
+ "moduleResolution": "node",
9
+ "strict": true,
10
+ "pretty": true,
11
+ "sourceMap": false,
12
+ "declaration": false,
13
+ "experimentalDecorators": false,
14
+ "removeComments": true
15
+ },
16
+ "include": [
17
+ "src/**/*.ts"
18
+ ],
19
+ "exclude": [
20
+ "node_modules"
21
+ ]
22
+ }