ztechno_core 0.0.22 → 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.
@@ -9,16 +9,26 @@ 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;
15
18
  private salt;
16
19
  constructor({ sqlService, tableName }: { sqlService: ZSqlService; tableName?: string });
17
20
  private checkTableExists;
21
+ checkTableHasAdmin(): Promise<boolean>;
18
22
  private createTable;
19
23
  ensureTableExists(): Promise<void>;
20
- register({ name, pass, role, admin }: ZRequiredUserColumns): Promise<void>;
21
- auth({ name, pass }: ZUserCredentials): Promise<boolean>;
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;
22
32
  private hashPass;
23
33
  }
24
34
  export {};
@@ -16,6 +16,12 @@ class ZUserService {
16
16
  `);
17
17
  return res.length > 0;
18
18
  }
19
+ async checkTableHasAdmin() {
20
+ const res = await this.sqlService.query(`
21
+ SELECT id FROM \`${this.tableName}\` WHERE admin=1
22
+ `);
23
+ return res.length > 0;
24
+ }
19
25
  async createTable() {
20
26
  await this.sqlService.query(`
21
27
  CREATE TABLE \`${this.tableName}\` (
@@ -23,15 +29,17 @@ class ZUserService {
23
29
  \`name\` varchar(64) NOT NULL,
24
30
  \`role\` varchar(64) DEFAULT NULL,
25
31
  \`pass\` varchar(512) NOT NULL,
26
- \`admin\` tinyint(1) NOT NULL DEFAULT 0,
32
+ \`session\` varchar(512) NOT NULL,
33
+ \`admin\` tinyint(1) NOT NULL,
27
34
  \`updated_at\` datetime NOT NULL DEFAULT current_timestamp(),
28
35
  \`created_at\` datetime NOT NULL DEFAULT current_timestamp(),
29
36
  PRIMARY KEY (\`id\`),
30
37
  UNIQUE KEY \`name_UNIQUE\` (\`name\`),
31
38
  KEY \`name\` (\`name\`),
32
39
  KEY \`createdat\` (\`created_at\`),
33
- KEY \`updatedat\` (\`updated_at\`)
34
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
40
+ KEY \`updatedat\` (\`updated_at\`),
41
+ KEY \`session\` (\`session\`)
42
+ ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
35
43
  `);
36
44
  }
37
45
  async ensureTableExists() {
@@ -41,24 +49,36 @@ class ZUserService {
41
49
  }
42
50
  }
43
51
  async register({ name, pass, role, admin }) {
52
+ const session = this.genSession({ name, pass });
44
53
  await this.sqlService.query(
45
54
  `
46
- INSERT INTO \`${this.tableName}\` (name, pass, role, admin)
55
+ INSERT INTO \`${this.tableName}\` (name, pass, session, role, admin)
47
56
  VALUES (?, ?, ?, ?)
48
57
  `,
49
- [name, this.hashPass({ name, pass }), role, admin],
58
+ [name, this.hashPass({ name, pass }), session, role, admin],
50
59
  );
60
+ return { session };
51
61
  }
52
- async auth({ name, pass }) {
53
- const res = await this.sqlService.query(
54
- `
55
- SELECT id, name, role, admin, updated_at, created_at
56
- FROM ${this.tableName}
57
- WHERE name=? AND pass=?
58
- `,
59
- [name, this.hashPass({ name, pass })],
60
- );
61
- return res.length === 1;
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 });
62
82
  }
63
83
  hashPass({ name, pass }) {
64
84
  const salt = name + this.salt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztechno_core",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "description": "Core files for ztechno framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",