ztechno_core 0.0.14 → 0.0.16

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.
@@ -6,5 +6,20 @@ export declare class ZCryptoService {
6
6
  static encrypt(text: string): HashStruct;
7
7
  static decrypt(data: HashStruct): string;
8
8
  static decryptJSON(data: HashStruct): any;
9
+ static hash(
10
+ hashAlgorithm: 'sha256' | 'sha512' | 'md5',
11
+ data: string,
12
+ opt?:
13
+ | {
14
+ saltMode: 'none';
15
+ }
16
+ | {
17
+ saltMode: 'simple';
18
+ salt: string;
19
+ },
20
+ ): {
21
+ data: string;
22
+ hash: string;
23
+ };
9
24
  }
10
25
  export {};
@@ -71,5 +71,18 @@ class ZCryptoService {
71
71
  throw new Error(`Couldn't decrypt JSON ${JSON.stringify(data)}`);
72
72
  }
73
73
  }
74
+ static hash(hashAlgorithm, data, opt) {
75
+ if (opt && opt.saltMode === 'simple') {
76
+ const salt = opt.salt;
77
+ data = data
78
+ .split('')
79
+ .map((c, i) => c + salt.charAt(salt.length % i))
80
+ .join('');
81
+ }
82
+ return {
83
+ data,
84
+ hash: crypto.createHash(hashAlgorithm).update(data).digest('hex'),
85
+ };
86
+ }
74
87
  }
75
88
  exports.ZCryptoService = ZCryptoService;
package/lib/index.d.ts CHANGED
@@ -2,4 +2,5 @@ import { ZCryptoService } from './crypto_service';
2
2
  import { ZMailService } from './mail_service';
3
3
  import { ZSqlService } from './sql_service';
4
4
  import { ZTranslateService } from './translate_service';
5
- export { ZCryptoService, ZMailService, ZSqlService, ZTranslateService };
5
+ import { ZUserService } from './user_service';
6
+ export { ZCryptoService, ZMailService, ZSqlService, ZTranslateService, ZUserService };
package/lib/index.js CHANGED
@@ -1,6 +1,11 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, '__esModule', { value: true });
3
- exports.ZTranslateService = exports.ZSqlService = exports.ZMailService = exports.ZCryptoService = void 0;
3
+ exports.ZUserService =
4
+ exports.ZTranslateService =
5
+ exports.ZSqlService =
6
+ exports.ZMailService =
7
+ exports.ZCryptoService =
8
+ void 0;
4
9
  const crypto_service_1 = require('./crypto_service');
5
10
  Object.defineProperty(exports, 'ZCryptoService', {
6
11
  enumerable: true,
@@ -29,5 +34,12 @@ Object.defineProperty(exports, 'ZTranslateService', {
29
34
  return translate_service_1.ZTranslateService;
30
35
  },
31
36
  });
37
+ const user_service_1 = require('./user_service');
38
+ Object.defineProperty(exports, 'ZUserService', {
39
+ enumerable: true,
40
+ get: function () {
41
+ return user_service_1.ZUserService;
42
+ },
43
+ });
32
44
  // declare module 'ztechno_core' {
33
45
  // }
@@ -5,7 +5,9 @@ export declare class ZSqlService {
5
5
  private pool;
6
6
  private defaultPoolconfig;
7
7
  private listeners;
8
+ private databaseName;
8
9
  constructor(options: mysql.PoolConfig);
10
+ get database(): string;
9
11
  on(eventName: 'err', listener: ZOnErrorCallback): any;
10
12
  on(eventName: 'log', listener: ZOnLogCallback): any;
11
13
  private triggerEvent;
@@ -52,6 +52,7 @@ class ZSqlService {
52
52
  acquireTimeout: 20000,
53
53
  };
54
54
  this.listeners = { err: [], log: [] };
55
+ this.databaseName = options.database;
55
56
  this.pool = mysql.createPool(Object.assign({}, this.defaultPoolconfig, options));
56
57
  this.pool.on('connection', (connection) => {
57
58
  connection.on('error', (err) => {
@@ -62,6 +63,9 @@ class ZSqlService {
62
63
  });
63
64
  });
64
65
  }
66
+ get database() {
67
+ return this.databaseName;
68
+ }
65
69
  on(eventName, listener) {
66
70
  if (!this.listeners.hasOwnProperty(eventName))
67
71
  throw new Error(`EventName not supported for ZSqlService.on(${eventName}, ...)`);
@@ -0,0 +1,24 @@
1
+ import { ZSqlService } from './sql_service';
2
+ type ZRequiredUserColumns = {
3
+ name: string;
4
+ role: string | null;
5
+ pass: string;
6
+ admin: 0 | 1;
7
+ };
8
+ type ZUserCredentials = {
9
+ name: string;
10
+ pass: string;
11
+ };
12
+ export declare class ZUserService {
13
+ private tableName;
14
+ private sqlService;
15
+ private salt;
16
+ constructor({ sqlService, tableName }: { sqlService: ZSqlService; tableName?: string });
17
+ private checkTableExists;
18
+ private createTable;
19
+ ensureTableExists(): Promise<void>;
20
+ register({ name, pass, role, admin }: ZRequiredUserColumns): Promise<void>;
21
+ auth({ name, pass }: ZUserCredentials): Promise<boolean>;
22
+ private saltPass;
23
+ }
24
+ export {};
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.ZUserService = void 0;
4
+ const crypto_service_1 = require('./crypto_service');
5
+ class ZUserService {
6
+ constructor({ sqlService, tableName }) {
7
+ this.sqlService = sqlService;
8
+ this.tableName = tableName || 'users';
9
+ this.salt = sqlService.database;
10
+ }
11
+ async checkTableExists() {
12
+ const res = await this.sqlService.query(`
13
+ SELECT ENGINE, VERSION, CREATE_TIME FROM information_schema.tables
14
+ WHERE table_schema = '${this.sqlService.database}' AND table_name = '${this.tableName}'
15
+ LIMIT 1
16
+ `);
17
+ return res.length > 0;
18
+ }
19
+ async createTable() {
20
+ await this.sqlService.query(`
21
+ CREATE TABLE \`${this.tableName}\` (
22
+ \`id\` int(10) unsigned zerofill NOT NULL,
23
+ \`name\` varchar(64) NOT NULL,
24
+ \`role\` varchar(64) DEFAULT NULL,
25
+ \`pass\` varchar(512) NOT NULL,
26
+ \`admin\` tinyint(1) NOT NULL DEFAULT 0,
27
+ \`updated_at\` datetime NOT NULL DEFAULT current_timestamp(),
28
+ \`created_at\` datetime NOT NULL DEFAULT current_timestamp(),
29
+ PRIMARY KEY (\`id\`),
30
+ UNIQUE KEY \`name_UNIQUE\` (\`name\`),
31
+ KEY \`name\` (\`name\`),
32
+ KEY \`createdat\` (\`created_at\`),
33
+ KEY \`updatedat\` (\`updated_at\`)
34
+ ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
35
+ `);
36
+ }
37
+ async ensureTableExists() {
38
+ const exists = await this.checkTableExists();
39
+ if (!exists) {
40
+ await this.createTable();
41
+ }
42
+ }
43
+ async register({ name, pass, role, admin }) {
44
+ await this.sqlService.query(
45
+ `
46
+ INSERT INTO \`${this.tableName}\` (name, pass, role, admin)
47
+ VALUES (?, ?, ?, ?)
48
+ `,
49
+ [name, this.saltPass({ name, pass }), role, admin],
50
+ );
51
+ }
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.saltPass({ name, pass })],
60
+ );
61
+ return res.length === 1;
62
+ }
63
+ saltPass({ name, pass }) {
64
+ const salt = name + this.salt;
65
+ const { hash } = crypto_service_1.ZCryptoService.hash('sha256', pass, { saltMode: 'simple', salt });
66
+ return hash;
67
+ }
68
+ }
69
+ exports.ZUserService = ZUserService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztechno_core",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "description": "Core files for ztechno framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",