ztechno_core 0.0.23 → 0.0.24
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/user_service.d.ts +11 -2
- package/lib/user_service.js +29 -15
- package/package.json +1 -1
package/lib/user_service.d.ts
CHANGED
|
@@ -9,6 +9,9 @@ type ZUserCredentials = {
|
|
|
9
9
|
name: string;
|
|
10
10
|
pass: string;
|
|
11
11
|
};
|
|
12
|
+
type ZUserSession = {
|
|
13
|
+
session: string;
|
|
14
|
+
};
|
|
12
15
|
export declare class ZUserService {
|
|
13
16
|
private tableName;
|
|
14
17
|
private sqlService;
|
|
@@ -18,8 +21,14 @@ export declare class ZUserService {
|
|
|
18
21
|
checkTableHasAdmin(): Promise<boolean>;
|
|
19
22
|
private createTable;
|
|
20
23
|
ensureTableExists(): Promise<void>;
|
|
21
|
-
register({ name, pass, role, admin }: ZRequiredUserColumns): Promise<
|
|
22
|
-
|
|
24
|
+
register({ name, pass, role, admin }: ZRequiredUserColumns): Promise<{
|
|
25
|
+
session: string;
|
|
26
|
+
}>;
|
|
27
|
+
auth(opt: ZUserSession | ZUserCredentials): Promise<{
|
|
28
|
+
session?: string;
|
|
29
|
+
authenticated: boolean;
|
|
30
|
+
}>;
|
|
31
|
+
private genSession;
|
|
23
32
|
private hashPass;
|
|
24
33
|
}
|
|
25
34
|
export {};
|
package/lib/user_service.js
CHANGED
|
@@ -29,15 +29,17 @@ class ZUserService {
|
|
|
29
29
|
\`name\` varchar(64) NOT NULL,
|
|
30
30
|
\`role\` varchar(64) DEFAULT NULL,
|
|
31
31
|
\`pass\` varchar(512) NOT NULL,
|
|
32
|
-
\`
|
|
32
|
+
\`session\` varchar(512) NOT NULL,
|
|
33
|
+
\`admin\` tinyint(1) NOT NULL,
|
|
33
34
|
\`updated_at\` datetime NOT NULL DEFAULT current_timestamp(),
|
|
34
35
|
\`created_at\` datetime NOT NULL DEFAULT current_timestamp(),
|
|
35
36
|
PRIMARY KEY (\`id\`),
|
|
36
37
|
UNIQUE KEY \`name_UNIQUE\` (\`name\`),
|
|
37
38
|
KEY \`name\` (\`name\`),
|
|
38
39
|
KEY \`createdat\` (\`created_at\`),
|
|
39
|
-
KEY \`updatedat\` (\`updated_at\`)
|
|
40
|
-
|
|
40
|
+
KEY \`updatedat\` (\`updated_at\`),
|
|
41
|
+
KEY \`session\` (\`session\`)
|
|
42
|
+
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
|
|
41
43
|
`);
|
|
42
44
|
}
|
|
43
45
|
async ensureTableExists() {
|
|
@@ -47,24 +49,36 @@ class ZUserService {
|
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
51
|
async register({ name, pass, role, admin }) {
|
|
52
|
+
const session = this.genSession({ name, pass });
|
|
50
53
|
await this.sqlService.query(
|
|
51
54
|
`
|
|
52
|
-
INSERT INTO \`${this.tableName}\` (name, pass, role, admin)
|
|
55
|
+
INSERT INTO \`${this.tableName}\` (name, pass, session, role, admin)
|
|
53
56
|
VALUES (?, ?, ?, ?)
|
|
54
57
|
`,
|
|
55
|
-
[name, this.hashPass({ name, pass }), role, admin],
|
|
58
|
+
[name, this.hashPass({ name, pass }), session, role, admin],
|
|
56
59
|
);
|
|
60
|
+
return { session };
|
|
57
61
|
}
|
|
58
|
-
async auth(
|
|
59
|
-
const res = await
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
FROM \`${this.tableName}\`
|
|
63
|
-
WHERE
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
62
|
+
async auth(opt) {
|
|
63
|
+
const res = await (opt.session
|
|
64
|
+
? this.sqlService.query(
|
|
65
|
+
`
|
|
66
|
+
SELECT id, name, session, role, admin, updated_at, created_at FROM \`${this.tableName}\`
|
|
67
|
+
WHERE session=?`,
|
|
68
|
+
[opt.session],
|
|
69
|
+
)
|
|
70
|
+
: this.sqlService.query(
|
|
71
|
+
`
|
|
72
|
+
SELECT id, name, session, role, admin, updated_at, created_at FROM \`${this.tableName}\`
|
|
73
|
+
WHERE name=? AND pass=?`,
|
|
74
|
+
[opt.name, this.hashPass(opt)],
|
|
75
|
+
));
|
|
76
|
+
return res.length === 0 ? { authenticated: false } : { session: res[0].session, authenticated: true };
|
|
77
|
+
}
|
|
78
|
+
genSession({ name }) {
|
|
79
|
+
const salt = this.salt;
|
|
80
|
+
const data = name + Date.now() * Math.random();
|
|
81
|
+
return crypto_service_1.ZCryptoService.hash('sha256', data, { saltMode: 'simple', salt });
|
|
68
82
|
}
|
|
69
83
|
hashPass({ name, pass }) {
|
|
70
84
|
const salt = name + this.salt;
|