ztechno_core 0.0.90 → 0.0.93
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/crypto_service.d.ts +3 -1
- package/lib/crypto_service.js +9 -2
- package/lib/schema/index.d.ts +1 -0
- package/lib/schema/index.js +1 -0
- package/lib/sql_service.d.ts +13 -3
- package/lib/sql_service.js +4 -10
- package/lib/user_service.js +27 -25
- package/package.json +1 -1
package/lib/crypto_service.d.ts
CHANGED
|
@@ -2,7 +2,9 @@ import { HashStruct } from './typings';
|
|
|
2
2
|
export declare class ZCryptoService {
|
|
3
3
|
static encrypt(text: string): HashStruct;
|
|
4
4
|
static decrypt(data: HashStruct): string;
|
|
5
|
-
static
|
|
5
|
+
static decrypt(encrypted: string): string;
|
|
6
|
+
static decryptJSON<T = any>(data: HashStruct): T;
|
|
7
|
+
static decryptJSON<T = any>(data: string): T;
|
|
6
8
|
static hash(
|
|
7
9
|
hashAlgorithm: 'sha256' | 'sha512' | 'md5',
|
|
8
10
|
data: string,
|
package/lib/crypto_service.js
CHANGED
|
@@ -50,15 +50,22 @@ const key = Buffer.from([
|
|
|
50
50
|
const iv = Buffer.from([0, 209, 223, 20, 147, 45, 14, 107, 93, 6, 76, 206, 176, 55, 245, 134]);
|
|
51
51
|
class ZCryptoService {
|
|
52
52
|
static encrypt(text) {
|
|
53
|
-
const cipher = crypto.createCipheriv(algorithm,
|
|
53
|
+
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
|
54
54
|
let encrypted = cipher.update(text);
|
|
55
55
|
encrypted = Buffer.concat([encrypted, cipher.final()]);
|
|
56
56
|
return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
|
|
57
57
|
}
|
|
58
58
|
static decrypt(data) {
|
|
59
|
+
if (typeof data === 'string') {
|
|
60
|
+
const encryptedText = Buffer.from(data, 'hex');
|
|
61
|
+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
|
62
|
+
let decrypted = decipher.update(encryptedText);
|
|
63
|
+
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
64
|
+
return decrypted.toString();
|
|
65
|
+
}
|
|
59
66
|
const niv = Buffer.from(data.iv, 'hex');
|
|
60
67
|
const encryptedText = Buffer.from(data.encryptedData, 'hex');
|
|
61
|
-
const decipher = crypto.createDecipheriv(algorithm,
|
|
68
|
+
const decipher = crypto.createDecipheriv(algorithm, key, niv);
|
|
62
69
|
let decrypted = decipher.update(encryptedText);
|
|
63
70
|
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
64
71
|
return decrypted.toString();
|
package/lib/schema/index.d.ts
CHANGED
package/lib/schema/index.js
CHANGED
package/lib/sql_service.d.ts
CHANGED
|
@@ -67,10 +67,20 @@ export declare class ZSQLService {
|
|
|
67
67
|
*/
|
|
68
68
|
fetch<T = any>(opt: {
|
|
69
69
|
Query: string;
|
|
70
|
-
Params:
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
Params:
|
|
71
|
+
| {
|
|
72
|
+
[key: string]: any;
|
|
73
|
+
}
|
|
74
|
+
| any[];
|
|
73
75
|
}): Promise<T[]>;
|
|
76
|
+
fetch<T = any>(
|
|
77
|
+
query: string,
|
|
78
|
+
params:
|
|
79
|
+
| {
|
|
80
|
+
[key: string]: any;
|
|
81
|
+
}
|
|
82
|
+
| any[],
|
|
83
|
+
): Promise<T[]>;
|
|
74
84
|
/**
|
|
75
85
|
* Executes a SQL query without type conversions.
|
|
76
86
|
* Supports both positional (array) and named (object) parameters.
|
package/lib/sql_service.js
CHANGED
|
@@ -117,16 +117,10 @@ class ZSQLService {
|
|
|
117
117
|
return row;
|
|
118
118
|
});
|
|
119
119
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
* @param opt - Query options
|
|
125
|
-
* @param opt.Query - The SQL query string to execute
|
|
126
|
-
* @param opt.Params - Named parameters for the query
|
|
127
|
-
* @returns Promise resolving to query results
|
|
128
|
-
*/
|
|
129
|
-
async fetch(opt) {
|
|
120
|
+
async fetch(opt, params) {
|
|
121
|
+
if (typeof opt === 'string') {
|
|
122
|
+
return await this.exec({ query: opt, params });
|
|
123
|
+
}
|
|
130
124
|
return await this.exec({ query: opt.Query, params: opt.Params });
|
|
131
125
|
}
|
|
132
126
|
async query(sql, params) {
|
package/lib/user_service.js
CHANGED
|
@@ -149,20 +149,22 @@ class ZUserService {
|
|
|
149
149
|
async find(opt) {
|
|
150
150
|
const selectColumns = this.getSelectColumns();
|
|
151
151
|
if (opt.email !== undefined) {
|
|
152
|
-
const rows = await this.sqlService.
|
|
153
|
-
`
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
152
|
+
const rows = await this.sqlService.fetch({
|
|
153
|
+
Query: /*SQL*/ `
|
|
154
|
+
SELECT ${selectColumns} FROM \`${this.tableName}\`
|
|
155
|
+
WHERE email=:email
|
|
156
|
+
`,
|
|
157
|
+
Params: { email: opt.email },
|
|
158
|
+
});
|
|
158
159
|
return rows[0];
|
|
159
160
|
} else if (opt.user_id !== undefined) {
|
|
160
|
-
const rows = await this.sqlService.
|
|
161
|
-
`
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
161
|
+
const rows = await this.sqlService.fetch({
|
|
162
|
+
Query: /*SQL*/ `
|
|
163
|
+
SELECT ${selectColumns} FROM \`${this.tableName}\`
|
|
164
|
+
WHERE user_id=:user_id
|
|
165
|
+
`,
|
|
166
|
+
Params: { user_id: opt.user_id },
|
|
167
|
+
});
|
|
166
168
|
return rows[0];
|
|
167
169
|
} else {
|
|
168
170
|
throw new Error(`Unexpected Input for ZUserService.find(${JSON.stringify(opt)})`);
|
|
@@ -174,24 +176,24 @@ class ZUserService {
|
|
|
174
176
|
}
|
|
175
177
|
const selectColumns = this.getSelectColumns();
|
|
176
178
|
const res = await (opt.session
|
|
177
|
-
? this.sqlService.
|
|
178
|
-
`
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
[opt.session],
|
|
182
|
-
)
|
|
183
|
-
: this.sqlService.
|
|
184
|
-
`
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
[opt.email, this.hashPass(opt)],
|
|
188
|
-
));
|
|
179
|
+
? this.sqlService.fetch({
|
|
180
|
+
Query: /*SQL*/ `
|
|
181
|
+
SELECT ${selectColumns} FROM \`${this.tableName}\` WHERE session=?
|
|
182
|
+
`,
|
|
183
|
+
Params: [opt.session],
|
|
184
|
+
})
|
|
185
|
+
: this.sqlService.fetch({
|
|
186
|
+
Query: /*SQL*/ `
|
|
187
|
+
SELECT ${selectColumns} FROM \`${this.tableName}\` WHERE email=? AND pass=?
|
|
188
|
+
`,
|
|
189
|
+
Params: [opt.email, this.hashPass(opt)],
|
|
190
|
+
}));
|
|
189
191
|
return res.length === 0 ? { authenticated: false } : { user: res[0], session: res[0].session, authenticated: true };
|
|
190
192
|
}
|
|
191
193
|
async fetch(opt) {
|
|
192
194
|
const selectColumns = this.getSelectColumns();
|
|
193
195
|
const limit = opt?.limit || 100;
|
|
194
|
-
const rows = await this.sqlService.
|
|
196
|
+
const rows = await this.sqlService.fetch(
|
|
195
197
|
`
|
|
196
198
|
SELECT ${selectColumns} FROM \`${this.tableName}\`
|
|
197
199
|
ORDER BY created_at DESC
|