@puca/puca-core 3.9.1 → 3.11.0
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/dist/core/services/auth.service.d.ts +25 -0
- package/dist/core/services/auth.service.js +143 -0
- package/dist/core/services/auth.service.js.map +1 -0
- package/dist/core/services/comunication.service.d.ts +9 -1
- package/dist/core/services/comunication.service.js +34 -2
- package/dist/core/services/comunication.service.js.map +1 -1
- package/dist/core/services/index.d.ts +2 -0
- package/dist/core/services/index.js +5 -3
- package/dist/core/services/index.js.map +1 -1
- package/dist/core/services/web-socket.service.d.ts +3 -0
- package/dist/core/services/web-socket.service.js +3 -3
- package/dist/core/services/web-socket.service.js.map +1 -1
- package/dist/models/module-especification.d.ts +1 -0
- package/dist/models/puca-auth.d.ts +5 -0
- package/dist/orm/services/repository.d.ts +16 -7
- package/dist/orm/services/repository.js +34 -11
- package/dist/orm/services/repository.js.map +1 -1
- package/dist/plugins/nestjs/puca-auth.decorators.js +3 -6
- package/dist/plugins/nestjs/puca-auth.decorators.js.map +1 -1
- package/dist/plugins/nestjs/puca-auth.guard.js +4 -9
- package/dist/plugins/nestjs/puca-auth.guard.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +77 -77
- package/dist/plugins/jsonwebtoken/index.d.ts +0 -2
- package/dist/plugins/jsonwebtoken/index.js +0 -74
- package/dist/plugins/jsonwebtoken/index.js.map +0 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Request } from 'express';
|
|
2
|
+
import { PucaAuth } from "../../models/puca-auth";
|
|
3
|
+
import { RepositoryFactory } from "../../orm/services/repository-factory";
|
|
4
|
+
import { FileSystemService } from "../../plugins/file-system/file-system.service";
|
|
5
|
+
import { UuidService } from "../../plugins/uuid/uuid.service";
|
|
6
|
+
import { EnvConfigService } from "./envs-config.service";
|
|
7
|
+
import { ModuleConfigService } from "./module-config.service";
|
|
8
|
+
import { ModuleEspecificationService } from './module-especification.service';
|
|
9
|
+
export declare class AuthService {
|
|
10
|
+
private readonly envConfigService;
|
|
11
|
+
private readonly fileSystemService;
|
|
12
|
+
private readonly moduleConfigService;
|
|
13
|
+
private readonly uuidService;
|
|
14
|
+
private readonly moduleEspecificationService;
|
|
15
|
+
private readonly logger;
|
|
16
|
+
private readonly userManager;
|
|
17
|
+
constructor(envConfigService: EnvConfigService, fileSystemService: FileSystemService, repositoryFactory: RepositoryFactory, moduleConfigService: ModuleConfigService, uuidService: UuidService, moduleEspecificationService: ModuleEspecificationService);
|
|
18
|
+
private getPublicKey;
|
|
19
|
+
private unsaveDecode;
|
|
20
|
+
private isValid;
|
|
21
|
+
decodeAndVerify(token: string): Promise<PucaAuth>;
|
|
22
|
+
decodeAndVerifyRequest(r: Request): Promise<Request & {
|
|
23
|
+
pucaAuth: PucaAuth;
|
|
24
|
+
}>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthService = void 0;
|
|
4
|
+
const _ = require("lodash");
|
|
5
|
+
const jwt = require("jsonwebtoken");
|
|
6
|
+
const logger_1 = require("./logger");
|
|
7
|
+
class UserManager {
|
|
8
|
+
constructor(repositoryFactory) {
|
|
9
|
+
this.repositoryFactory = repositoryFactory;
|
|
10
|
+
this.users = {};
|
|
11
|
+
}
|
|
12
|
+
async findUser(companyKey, pucaPid) {
|
|
13
|
+
if (!this.users[companyKey]) {
|
|
14
|
+
this.users[companyKey] = {};
|
|
15
|
+
const userRepository = this.repositoryFactory.getCacheInstance({
|
|
16
|
+
companyKey,
|
|
17
|
+
tableName: 'puca_base_api_system_user'
|
|
18
|
+
});
|
|
19
|
+
const users = await userRepository.findAll();
|
|
20
|
+
users.forEach(user => this.users[companyKey][user.puca_pid] = user);
|
|
21
|
+
userRepository.onChange(args => {
|
|
22
|
+
const user = args.new || args.old;
|
|
23
|
+
const update = () => {
|
|
24
|
+
this.users[companyKey][user.puca_pid] = user;
|
|
25
|
+
};
|
|
26
|
+
if (!user.login_id) {
|
|
27
|
+
this.timeout = setTimeout(update, 10000);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
if (this.timeout) {
|
|
31
|
+
clearTimeout(this.timeout);
|
|
32
|
+
}
|
|
33
|
+
update();
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
while (_.isEqual(this.users[companyKey], {})) {
|
|
38
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
39
|
+
}
|
|
40
|
+
return this.users[companyKey][pucaPid];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
class AuthService {
|
|
44
|
+
constructor(envConfigService, fileSystemService, repositoryFactory, moduleConfigService, uuidService, moduleEspecificationService) {
|
|
45
|
+
this.envConfigService = envConfigService;
|
|
46
|
+
this.fileSystemService = fileSystemService;
|
|
47
|
+
this.moduleConfigService = moduleConfigService;
|
|
48
|
+
this.uuidService = uuidService;
|
|
49
|
+
this.moduleEspecificationService = moduleEspecificationService;
|
|
50
|
+
this.logger = new logger_1.Logger('AuthService');
|
|
51
|
+
this.userManager = new UserManager(repositoryFactory);
|
|
52
|
+
}
|
|
53
|
+
async getPublicKey({ companyKey, moduleName }) {
|
|
54
|
+
const fallback = async () => {
|
|
55
|
+
const pathPublicKey = (await this.envConfigService.getEnvByName('PATH_PUBLIC_KEY')) || './key.pub';
|
|
56
|
+
const publicKey = await this.fileSystemService.readStringFile(pathPublicKey, {
|
|
57
|
+
throwErrors: false,
|
|
58
|
+
});
|
|
59
|
+
if (!publicKey) {
|
|
60
|
+
throw new Error('public key not found');
|
|
61
|
+
}
|
|
62
|
+
return publicKey;
|
|
63
|
+
};
|
|
64
|
+
if (!moduleName) {
|
|
65
|
+
return await fallback();
|
|
66
|
+
}
|
|
67
|
+
if (moduleName === (await this.moduleConfigService.getModuleConfig()).name) {
|
|
68
|
+
return await fallback();
|
|
69
|
+
}
|
|
70
|
+
const moduleEspecificationList = await this.moduleEspecificationService.resolveByCompanyKeyAndModuleName({
|
|
71
|
+
companyKey,
|
|
72
|
+
moduleName,
|
|
73
|
+
onlyInstalled: false
|
|
74
|
+
});
|
|
75
|
+
const moduleEspecification = moduleEspecificationList[0];
|
|
76
|
+
if (!moduleEspecification) {
|
|
77
|
+
return await fallback();
|
|
78
|
+
}
|
|
79
|
+
return moduleEspecification.publicKey || await fallback();
|
|
80
|
+
}
|
|
81
|
+
async unsaveDecode(token) {
|
|
82
|
+
try {
|
|
83
|
+
return jwt.decode(token);
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
this.logger.warn(e);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async isValid(token, publicKey) {
|
|
91
|
+
try {
|
|
92
|
+
const decoded = jwt.verify(token, publicKey, {
|
|
93
|
+
algorithms: ['RS256'],
|
|
94
|
+
});
|
|
95
|
+
return !!decoded;
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
this.logger.warn(e);
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async decodeAndVerify(token) {
|
|
103
|
+
const decoded = await this.unsaveDecode(token);
|
|
104
|
+
if (!decoded) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
if ((decoded.version || 0) < 1) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const publicKey = await this.getPublicKey({
|
|
111
|
+
moduleName: _.get(decoded, ['module', 'name']),
|
|
112
|
+
companyKey: _.get(decoded, ['company', 'companyKey'])
|
|
113
|
+
});
|
|
114
|
+
const isValid = await this.isValid(token, publicKey);
|
|
115
|
+
if (!isValid) {
|
|
116
|
+
this.logger.warn('invalid assignature token', decoded);
|
|
117
|
+
await new Promise(r => setTimeout(r, 3000));
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
if (!decoded.ignoreLoginId) {
|
|
121
|
+
const userCache = await this.userManager.findUser(decoded.company.companyKey, decoded.user.puca_pid);
|
|
122
|
+
if (userCache.login_id !== decoded.loginId) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const moduleConfig = await this.moduleConfigService.getModuleConfig();
|
|
127
|
+
decoded.user.token = token;
|
|
128
|
+
decoded.user.idRequest = this.uuidService.v4();
|
|
129
|
+
decoded.user.moduleName = moduleConfig.name;
|
|
130
|
+
decoded.user.moduleVersion = moduleConfig.version;
|
|
131
|
+
return decoded;
|
|
132
|
+
}
|
|
133
|
+
async decodeAndVerifyRequest(r) {
|
|
134
|
+
const request = r;
|
|
135
|
+
const token = request.headers['authorization'] || request.headers['Authorization'];
|
|
136
|
+
if (token) {
|
|
137
|
+
request.pucaAuth = await this.decodeAndVerify(token);
|
|
138
|
+
}
|
|
139
|
+
return request;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.AuthService = AuthService;
|
|
143
|
+
//# sourceMappingURL=auth.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.service.js","sourceRoot":"","sources":["../../../src/core/services/auth.service.ts"],"names":[],"mappings":";;;AAAA,4BAA4B;AAC5B,oCAAmC;AAUnC,qCAAkC;AAGlC,MAAM,WAAW;IAIf,YAA6B,iBAAoC;QAApC,sBAAiB,GAAjB,iBAAiB,CAAmB;QAHhD,UAAK,GAA0D,EAAE,CAAA;IAIlF,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,UAAkB,EAAE,OAAe;QAChD,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE;YAC3B,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,EAAE,CAAA;YAC3B,MAAM,cAAc,GAAG,IAAI,CAAC,iBAAiB,CAAC,gBAAgB,CAAO;gBACnE,UAAU;gBACV,SAAS,EAAE,2BAA2B;aACvC,CAAC,CAAA;YACF,MAAM,KAAK,GA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AC;CACF;AA3HD,kCA2HC"}
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { CallResponse } from '../../models/call-response';
|
|
2
2
|
import { ModuleEspecificationService } from './module-especification.service';
|
|
3
3
|
import { User } from '../../models/user';
|
|
4
|
+
import { EnvConfigService } from './envs-config.service';
|
|
5
|
+
import { FileSystemService } from '../../plugins/file-system/file-system.service';
|
|
6
|
+
import { PucaAuth } from '../../models/puca-auth';
|
|
7
|
+
import { ModuleConfigService } from './module-config.service';
|
|
4
8
|
export declare class ComunicationService {
|
|
5
9
|
private readonly us;
|
|
10
|
+
private readonly envConfigService;
|
|
11
|
+
private readonly fileSystemService;
|
|
12
|
+
private readonly moduleConfigService;
|
|
6
13
|
private readonly logger;
|
|
7
|
-
constructor(us: ModuleEspecificationService);
|
|
14
|
+
constructor(us: ModuleEspecificationService, envConfigService: EnvConfigService, fileSystemService: FileSystemService, moduleConfigService: ModuleConfigService);
|
|
8
15
|
private internalCall;
|
|
9
16
|
private internalCallLoop;
|
|
10
17
|
call<B>({ companyKey, moduleName, path, version, headers, body, user, protocol, method }: {
|
|
@@ -18,4 +25,5 @@ export declare class ComunicationService {
|
|
|
18
25
|
protocol?: string;
|
|
19
26
|
method?: string;
|
|
20
27
|
}): Promise<CallResponse<B>>;
|
|
28
|
+
computeToken(pucaAuth: PucaAuth): Promise<string>;
|
|
21
29
|
}
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ComunicationService = void 0;
|
|
4
|
+
const moment = require("moment");
|
|
4
5
|
const axios_1 = require("axios");
|
|
5
6
|
const logger_1 = require("./logger");
|
|
7
|
+
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
6
8
|
class ComunicationService {
|
|
7
|
-
constructor(us) {
|
|
9
|
+
constructor(us, envConfigService, fileSystemService, moduleConfigService) {
|
|
8
10
|
this.us = us;
|
|
11
|
+
this.envConfigService = envConfigService;
|
|
12
|
+
this.fileSystemService = fileSystemService;
|
|
13
|
+
this.moduleConfigService = moduleConfigService;
|
|
9
14
|
this.logger = new logger_1.Logger('ComunicationService');
|
|
10
15
|
}
|
|
11
16
|
async internalCall({ domain, path, body, headers, protocol, method }) {
|
|
@@ -56,7 +61,21 @@ class ComunicationService {
|
|
|
56
61
|
if (!headers) {
|
|
57
62
|
headers = {};
|
|
58
63
|
}
|
|
59
|
-
|
|
64
|
+
const moduleConfig = await this.moduleConfigService.getModuleConfig();
|
|
65
|
+
headers['Authorization'] = await this.computeToken({
|
|
66
|
+
company: {
|
|
67
|
+
companyKey
|
|
68
|
+
},
|
|
69
|
+
exp: moment(new Date()).add(1, 'days').unix(),
|
|
70
|
+
loginId: null,
|
|
71
|
+
ignoreLoginId: true,
|
|
72
|
+
module: {
|
|
73
|
+
name: moduleConfig.name,
|
|
74
|
+
version: moduleConfig.version,
|
|
75
|
+
},
|
|
76
|
+
user,
|
|
77
|
+
version: 1
|
|
78
|
+
});
|
|
60
79
|
headers['puca-id-request'] = user.idRequest;
|
|
61
80
|
const response = await this.internalCallLoop({
|
|
62
81
|
domains: moduleEspecificationToCall.map(({ url }) => url),
|
|
@@ -68,6 +87,19 @@ class ComunicationService {
|
|
|
68
87
|
});
|
|
69
88
|
return response;
|
|
70
89
|
}
|
|
90
|
+
async computeToken(pucaAuth) {
|
|
91
|
+
const pathPrivateKey = await this.envConfigService.getEnvByName('PATH_PRIVATE_KEY', './key');
|
|
92
|
+
const privateKey = await this.fileSystemService.readStringFile(pathPrivateKey);
|
|
93
|
+
if (!privateKey) {
|
|
94
|
+
if (!pucaAuth.user.token) {
|
|
95
|
+
throw new Error('computeToken error 1');
|
|
96
|
+
}
|
|
97
|
+
return pucaAuth.user.token;
|
|
98
|
+
}
|
|
99
|
+
return jsonwebtoken_1.sign(pucaAuth, privateKey, {
|
|
100
|
+
algorithm: 'RS256'
|
|
101
|
+
});
|
|
102
|
+
}
|
|
71
103
|
}
|
|
72
104
|
exports.ComunicationService = ComunicationService;
|
|
73
105
|
//# sourceMappingURL=comunication.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"comunication.service.js","sourceRoot":"","sources":["../../../src/core/services/comunication.service.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"comunication.service.js","sourceRoot":"","sources":["../../../src/core/services/comunication.service.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAIhC,iCAA0B;AAG1B,qCAAkC;AAGlC,+CAAoC;AAGpC,MAAa,mBAAmB;IAE9B,YAA6B,EAA+B,EACzC,gBAAkC,EAClC,iBAAoC,EACpC,mBAAwC;QAH9B,OAAE,GAAF,EAAE,CAA6B;QACzC,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,sBAAiB,GAAjB,iBAAiB,CAAmB;QACpC,wBAAmB,GAAnB,mBAAmB,CAAqB;QAJ1C,WAAM,GAAG,IAAI,eAAM,CAAC,qBAAqB,CAAC,CAAC;IAIG,CAAC;IACxD,KAAK,CAAC,YAAY,CAAI,EAC5B,MAAM,EACN,IAAI,EACJ,IAAI,EACJ,OAAO,EACP,QAAQ,EACR,MAAM,EAQP;QACC,MAAM,GAAG,GAAG,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC;QAChC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAC/B,MAAM,QAAQ,GAAG,MAAM,eAAK,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE;YAC9C,OAAO;SACR,CAAC,CAAC;QACH,OAAO;YACL,OAAO,EAAE,QAAQ,CAAC,OAAO;YACzB,IAAI,EAAE,QAAQ,CAAC,IAAI;SACpB,CAAC;IACJ,CAAC;IACO,KAAK,CAAC,gBAAgB,CAAI,EAChC,OAAO,EACP,IAAI,EACJ,IAAI,EACJ,OAAO,EACP,QAAQ,EACR,MAAM,EAQP;QACC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE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,EAAE,UAAU,EAAE;YAChC,SAAS,EAAE,OAAO;SACnB,CAAC,CAAA;IACJ,CAAC;CACF;AA1JD,kDA0JC"}
|
|
@@ -11,12 +11,14 @@ import { CompanyFeatureRepositoryManager } from './company-features.repository';
|
|
|
11
11
|
import { UserFeatureService } from './user-feature.service';
|
|
12
12
|
import { UserConfigService } from './user-config.service';
|
|
13
13
|
import { WebSocketService } from './web-socket.service';
|
|
14
|
+
import { AuthService } from './auth.service';
|
|
14
15
|
export declare const envConfigService: EnvConfigService;
|
|
15
16
|
export declare const companyService: CompanyService;
|
|
16
17
|
export declare const moduleConfigService: ModuleConfigService;
|
|
17
18
|
export declare const companyFeatureRepositoryManager: CompanyFeatureRepositoryManager;
|
|
18
19
|
export declare const moduleEspecificationRepository: ModuleEspecificationRepository;
|
|
19
20
|
export declare const moduleEspecificationService: ModuleEspecificationService;
|
|
21
|
+
export declare const authService: AuthService;
|
|
20
22
|
export declare const userFeatureService: UserFeatureService;
|
|
21
23
|
export declare const userConfigService: UserConfigService;
|
|
22
24
|
export declare const comunicationService: ComunicationService;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.lifeCycleService = exports.webSocketService = exports.installService = exports.bootstrapService = exports.comunicationService = exports.userConfigService = exports.userFeatureService = exports.moduleEspecificationService = exports.moduleEspecificationRepository = exports.companyFeatureRepositoryManager = exports.moduleConfigService = exports.companyService = exports.envConfigService = void 0;
|
|
3
|
+
exports.lifeCycleService = exports.webSocketService = exports.installService = exports.bootstrapService = exports.comunicationService = exports.userConfigService = exports.userFeatureService = exports.authService = exports.moduleEspecificationService = exports.moduleEspecificationRepository = exports.companyFeatureRepositoryManager = exports.moduleConfigService = exports.companyService = exports.envConfigService = void 0;
|
|
4
4
|
const services_1 = require("../../orm/services");
|
|
5
5
|
const plugins_1 = require("../../plugins");
|
|
6
6
|
const cases_1 = require("../cases");
|
|
@@ -18,18 +18,20 @@ const company_features_repository_1 = require("./company-features.repository");
|
|
|
18
18
|
const user_feature_service_1 = require("./user-feature.service");
|
|
19
19
|
const user_config_service_1 = require("./user-config.service");
|
|
20
20
|
const web_socket_service_1 = require("./web-socket.service");
|
|
21
|
+
const auth_service_1 = require("./auth.service");
|
|
21
22
|
exports.envConfigService = new envs_config_service_1.EnvConfigService(plugins_1.fileSystemService);
|
|
22
23
|
exports.companyService = new company_service_1.CompanyService(plugins_1.fileSystemService, exports.envConfigService);
|
|
23
24
|
exports.moduleConfigService = new module_config_service_1.ModuleConfigService(plugins_1.fileSystemService);
|
|
24
25
|
exports.companyFeatureRepositoryManager = new company_features_repository_1.CompanyFeatureRepositoryManager(exports.envConfigService, exports.moduleConfigService);
|
|
25
26
|
exports.moduleEspecificationRepository = new module_especification_repository_1.ModuleEspecificationRepository(plugins_1.fileSystemService, exports.envConfigService);
|
|
26
27
|
exports.moduleEspecificationService = new module_especification_service_1.ModuleEspecificationService(exports.moduleEspecificationRepository, cases_1.versionService, cases_1.versionResolverService, exports.companyService, services_1.repositoryFactory);
|
|
28
|
+
exports.authService = new auth_service_1.AuthService(exports.envConfigService, plugins_1.fileSystemService, services_1.repositoryFactory, exports.moduleConfigService, plugins_1.uuidService, exports.moduleEspecificationService);
|
|
27
29
|
exports.userFeatureService = new user_feature_service_1.UserFeatureService(exports.companyFeatureRepositoryManager, services_1.repositoryFactory, exports.moduleConfigService, exports.moduleEspecificationService);
|
|
28
30
|
exports.userConfigService = new user_config_service_1.UserConfigService(exports.moduleConfigService, services_1.repositoryFactory);
|
|
29
|
-
exports.comunicationService = new comunication_service_1.ComunicationService(exports.moduleEspecificationService);
|
|
31
|
+
exports.comunicationService = new comunication_service_1.ComunicationService(exports.moduleEspecificationService, exports.envConfigService, plugins_1.fileSystemService, exports.moduleConfigService);
|
|
30
32
|
const migrationService = new migration_service_1.MigrationService(services_1.databaseManagerService, services_1.repositoryFactory, exports.moduleConfigService);
|
|
31
33
|
exports.bootstrapService = new bootstrap_service_1.BootstrapService(migrationService, exports.companyService);
|
|
32
34
|
exports.installService = new install_service_1.InstallService(migrationService, services_1.repositoryFactory, cases_1.versionService);
|
|
33
|
-
exports.webSocketService = new web_socket_service_1.WebSocketService();
|
|
35
|
+
exports.webSocketService = new web_socket_service_1.WebSocketService(exports.authService);
|
|
34
36
|
exports.lifeCycleService = new life_cycle_service_1.LifeCycleService(services_1.databaseManagerService, exports.bootstrapService, exports.companyService, exports.envConfigService, exports.installService, services_1.repositoryFactory, plugins_1.fileSystemService, exports.moduleConfigService, exports.webSocketService);
|
|
35
37
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/services/index.ts"],"names":[],"mappings":";;;AAAA,iDAA+E;AAC/E,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/services/index.ts"],"names":[],"mappings":";;;AAAA,iDAA+E;AAC/E,2CAA+D;AAC/D,oCAAkE;AAClE,2DAAuD;AACvD,uDAAmD;AACnD,iEAA6D;AAC7D,+DAAyD;AACzD,6DAAwD;AACxD,mEAA8D;AAC9D,mFAA8E;AAC9E,yFAAoF;AACpF,2DAAuD;AACvD,uDAAmD;AACnD,+EAAgF;AAChF,iEAA4D;AAC5D,+DAA0D;AAC1D,6DAAwD;AACxD,iDAA6C;AAEhC,QAAA,gBAAgB,GAAG,IAAI,sCAAgB,CAAC,2BAAiB,CAAC,CAAC;AAC3D,QAAA,cAAc,GAAG,IAAI,gCAAc,CAC9C,2BAAiB,EACjB,wBAAgB,CACjB,CAAC;AACW,QAAA,mBAAmB,GAAG,IAAI,2CAAmB,CAAC,2BAAiB,CAAC,CAAC;AACjE,QAAA,+BAA+B,GAAG,IAAI,6DAA+B,CAChF,wBAAgB,EAChB,2BAAmB,CACpB,CAAC;AACW,QAAA,8BAA8B,GAAG,IAAI,iEAA8B,CAC9E,2BAAiB,EACjB,wBAAgB,CACjB,CAAC;AACW,QAAA,2BAA2B,GAAG,IAAI,2DAA2B,CACxE,sCAA8B,EAC9B,sBAAc,EACd,8BAAsB,EACtB,sBAAc,EACd,4BAAiB,CAClB,CAAC;AACW,QAAA,WAAW,GAAG,IAAI,0BAAW,CACxC,wBAAgB,EAChB,2BAAiB,EACjB,4BAAiB,EACjB,2BAAmB,EACnB,qBAAW,EACX,mCAA2B,CAC5B,CAAA;AACY,QAAA,kBAAkB,GAAG,IAAI,yCAAkB,CACtD,uCAA+B,EAC/B,4BAAiB,EACjB,2BAAmB,EACnB,mCAA2B,CAC5B,CAAC;AACW,QAAA,iBAAiB,GAAG,IAAI,uCAAiB,CACpD,2BAAmB,EACnB,4BAAiB,CAClB,CAAC;AACW,QAAA,mBAAmB,GAAG,IAAI,0CAAmB,CACxD,mCAA2B,EAC3B,wBAAgB,EAChB,2BAAiB,EACjB,2BAAmB,CACpB,CAAC;AACF,MAAM,gBAAgB,GAAG,IAAI,oCAAgB,CAC3C,iCAAsB,EACtB,4BAAiB,EACjB,2BAAmB,CACpB,CAAC;AACW,QAAA,gBAAgB,GAAG,IAAI,oCAAgB,CAClD,gBAAgB,EAChB,sBAAc,CACf,CAAC;AACW,QAAA,cAAc,GAAG,IAAI,gCAAc,CAC9C,gBAAgB,EAChB,4BAAiB,EACjB,sBAAc,CACf,CAAC;AACW,QAAA,gBAAgB,GAAG,IAAI,qCAAgB,CAClD,mBAAW,CACZ,CAAA;AACY,QAAA,gBAAgB,GAAG,IAAI,qCAAgB,CAClD,iCAAsB,EACtB,wBAAgB,EAChB,sBAAc,EACd,wBAAgB,EAChB,sBAAc,EACd,4BAAiB,EACjB,2BAAiB,EACjB,2BAAmB,EACnB,wBAAgB,CACjB,CAAC"}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { Socket } from 'socket.io';
|
|
2
2
|
import { PucaAuth } from '../../models/puca-auth';
|
|
3
|
+
import { AuthService } from './auth.service';
|
|
3
4
|
export interface SocketDetail {
|
|
4
5
|
socket: Socket;
|
|
5
6
|
pucaAuth: PucaAuth;
|
|
6
7
|
method: string;
|
|
7
8
|
}
|
|
8
9
|
export declare class WebSocketService {
|
|
10
|
+
private readonly authService;
|
|
11
|
+
constructor(authService: AuthService);
|
|
9
12
|
private readonly logger;
|
|
10
13
|
private io;
|
|
11
14
|
private checkLogin;
|
|
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.WebSocketService = void 0;
|
|
4
4
|
const _ = require("lodash");
|
|
5
5
|
const socket_io_1 = require("socket.io");
|
|
6
|
-
const jsonwebtoken_1 = require("../../plugins/jsonwebtoken");
|
|
7
6
|
const logger_1 = require("./logger");
|
|
8
7
|
class WebSocketService {
|
|
9
|
-
constructor() {
|
|
8
|
+
constructor(authService) {
|
|
9
|
+
this.authService = authService;
|
|
10
10
|
this.logger = new logger_1.Logger('WebSocketService');
|
|
11
11
|
}
|
|
12
12
|
async checkLogin(token) {
|
|
@@ -15,7 +15,7 @@ class WebSocketService {
|
|
|
15
15
|
}
|
|
16
16
|
let decoded;
|
|
17
17
|
try {
|
|
18
|
-
decoded = await
|
|
18
|
+
decoded = await this.authService.decodeAndVerify(token);
|
|
19
19
|
}
|
|
20
20
|
catch (e) {
|
|
21
21
|
this.logger.error(e);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web-socket.service.js","sourceRoot":"","sources":["../../../src/core/services/web-socket.service.ts"],"names":[],"mappings":";;;AAAA,4BAA4B;AAC5B,yCAA2C;AAE3C,
|
|
1
|
+
{"version":3,"file":"web-socket.service.js","sourceRoot":"","sources":["../../../src/core/services/web-socket.service.ts"],"names":[],"mappings":";;;AAAA,4BAA4B;AAC5B,yCAA2C;AAE3C,qCAAkC;AAalC,MAAa,gBAAgB;IAC3B,YAA6B,WAAwB;QAAxB,gBAAW,GAAX,WAAW,CAAa;QAEpC,WAAM,GAAG,IAAI,eAAM,CAAC,kBAAkB,CAAC,CAAC;IAFF,C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AC,QAAQ;aACjC,CAAC,CAAC,CAAA;YAEH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE;gBACzB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAC3B,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE;YAC1B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,EACT,UAAU,EACV,WAAW,EACX,MAAM,EACN,OAAO,GAMR;QACC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC;YAC9B,UAAU;YACV,QAAQ,EAAE,WAAW;YACrB,MAAM;SACP,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,EAAC,OAAO,CAAC,CAAA;IAC7B,CAAC;IAED,KAAK,CAAC,UAAU;QACd,MAAM,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;CACF;AA5ED,4CA4EC"}
|
|
@@ -12,17 +12,19 @@ export declare class Repository<T extends {
|
|
|
12
12
|
private readonly table;
|
|
13
13
|
private readonly log;
|
|
14
14
|
constructor(database: Database, sqlService: SqlService, uuidService: UuidService, table: string);
|
|
15
|
-
findByIn({ key, values, fields, orderBy, user, }: {
|
|
15
|
+
findByIn({ key, values, fields, orderBy, user, alias }: {
|
|
16
16
|
key: string;
|
|
17
17
|
values: any[];
|
|
18
18
|
fields: SelectField[];
|
|
19
19
|
orderBy?: OrderByField[];
|
|
20
20
|
user?: User;
|
|
21
|
+
alias?: string;
|
|
21
22
|
}): Promise<T[]>;
|
|
22
|
-
findByPucaPid({ pucaPid, fields, user, }: {
|
|
23
|
+
findByPucaPid({ pucaPid, fields, user, alias }: {
|
|
23
24
|
pucaPid: string;
|
|
24
25
|
fields: SelectField[];
|
|
25
26
|
user?: User;
|
|
27
|
+
alias?: string;
|
|
26
28
|
}): Promise<T>;
|
|
27
29
|
findOneBy(args: {
|
|
28
30
|
key: string;
|
|
@@ -30,6 +32,7 @@ export declare class Repository<T extends {
|
|
|
30
32
|
fields: SelectField[];
|
|
31
33
|
user?: User;
|
|
32
34
|
groupBy?: SelectField[];
|
|
35
|
+
alias?: string;
|
|
33
36
|
}): Promise<T>;
|
|
34
37
|
findOneWhere(args: {
|
|
35
38
|
fields: SelectField[];
|
|
@@ -38,21 +41,24 @@ export declare class Repository<T extends {
|
|
|
38
41
|
user?: User;
|
|
39
42
|
groupBy?: SelectField[];
|
|
40
43
|
join?: Join[];
|
|
44
|
+
alias?: string;
|
|
41
45
|
}): Promise<T>;
|
|
42
|
-
countBy({ key, value, user, groupBy, }: {
|
|
46
|
+
countBy({ key, value, user, groupBy, alias }: {
|
|
43
47
|
key: string;
|
|
44
48
|
value: any;
|
|
45
49
|
user?: User;
|
|
46
50
|
groupBy?: SelectField[];
|
|
51
|
+
alias?: string;
|
|
47
52
|
}): Promise<number>;
|
|
48
|
-
countWhere({ where, args, join, user, groupBy, }: {
|
|
53
|
+
countWhere({ where, args, join, user, groupBy, alias }: {
|
|
49
54
|
where?: Where;
|
|
50
55
|
args?: Object;
|
|
51
56
|
join?: Join[];
|
|
52
57
|
user?: User;
|
|
53
58
|
groupBy?: SelectField[];
|
|
59
|
+
alias?: string;
|
|
54
60
|
}): Promise<number>;
|
|
55
|
-
findWhere({ args, where, fields, join, page, pageSize, orderBy, totalize, user, groupBy, }: {
|
|
61
|
+
findWhere({ args, where, fields, join, page, pageSize, orderBy, totalize, user, groupBy, alias }: {
|
|
56
62
|
where?: Where;
|
|
57
63
|
args?: any;
|
|
58
64
|
fields: SelectField[];
|
|
@@ -63,8 +69,9 @@ export declare class Repository<T extends {
|
|
|
63
69
|
join?: Join[];
|
|
64
70
|
user?: User;
|
|
65
71
|
groupBy?: SelectField[];
|
|
72
|
+
alias?: string;
|
|
66
73
|
}): Promise<SelectResult<T>>;
|
|
67
|
-
findBy({ key, value, fields, page, pageSize, orderBy, groupBy, totalize, user, }: {
|
|
74
|
+
findBy({ key, value, fields, page, pageSize, orderBy, groupBy, totalize, user, alias }: {
|
|
68
75
|
key: string;
|
|
69
76
|
value: any;
|
|
70
77
|
fields: SelectField[];
|
|
@@ -74,11 +81,12 @@ export declare class Repository<T extends {
|
|
|
74
81
|
groupBy?: SelectField[];
|
|
75
82
|
totalize?: boolean;
|
|
76
83
|
user?: User;
|
|
84
|
+
alias?: string;
|
|
77
85
|
}): Promise<SelectResult<T>>;
|
|
78
86
|
countAll(args?: {
|
|
79
87
|
user?: User;
|
|
80
88
|
}): Promise<number>;
|
|
81
|
-
findAll({ fields, page, pageSize, orderBy, groupBy, totalize, join, user, }: {
|
|
89
|
+
findAll({ fields, page, pageSize, orderBy, groupBy, totalize, join, user, alias }: {
|
|
82
90
|
fields: SelectField[];
|
|
83
91
|
page?: number;
|
|
84
92
|
pageSize?: number;
|
|
@@ -87,6 +95,7 @@ export declare class Repository<T extends {
|
|
|
87
95
|
totalize?: boolean;
|
|
88
96
|
join?: Join[];
|
|
89
97
|
user?: User;
|
|
98
|
+
alias?: string;
|
|
90
99
|
}): Promise<SelectResult<T>>;
|
|
91
100
|
private insert;
|
|
92
101
|
private upinsert;
|
|
@@ -12,7 +12,7 @@ class Repository {
|
|
|
12
12
|
this.table = table;
|
|
13
13
|
this.log = new logger_1.Logger('Repository');
|
|
14
14
|
}
|
|
15
|
-
async findByIn({ key, values, fields, orderBy, user, }) {
|
|
15
|
+
async findByIn({ key, values, fields, orderBy, user, alias }) {
|
|
16
16
|
const retorno = await this.findWhere({
|
|
17
17
|
fields,
|
|
18
18
|
args: values.reduce((r, v, i) => {
|
|
@@ -28,15 +28,17 @@ class Repository {
|
|
|
28
28
|
},
|
|
29
29
|
orderBy,
|
|
30
30
|
user,
|
|
31
|
+
alias
|
|
31
32
|
});
|
|
32
33
|
return retorno.data;
|
|
33
34
|
}
|
|
34
|
-
async findByPucaPid({ pucaPid, fields, user, }) {
|
|
35
|
+
async findByPucaPid({ pucaPid, fields, user, alias }) {
|
|
35
36
|
return await this.findOneBy({
|
|
36
37
|
fields,
|
|
37
38
|
key: 'puca_pid',
|
|
38
39
|
value: pucaPid,
|
|
39
40
|
user,
|
|
41
|
+
alias
|
|
40
42
|
});
|
|
41
43
|
}
|
|
42
44
|
async findOneBy(args) {
|
|
@@ -47,7 +49,7 @@ class Repository {
|
|
|
47
49
|
const result = await this.findWhere(Object.assign(Object.assign({}, args), { pageSize: 1, orderBy: ['puca_created_at'] }));
|
|
48
50
|
return _.get(result.data, 0);
|
|
49
51
|
}
|
|
50
|
-
async countBy({ key, value, user, groupBy, }) {
|
|
52
|
+
async countBy({ key, value, user, groupBy, alias }) {
|
|
51
53
|
return await this.countWhere({
|
|
52
54
|
args: {
|
|
53
55
|
value,
|
|
@@ -59,9 +61,10 @@ class Repository {
|
|
|
59
61
|
},
|
|
60
62
|
user,
|
|
61
63
|
groupBy,
|
|
64
|
+
alias
|
|
62
65
|
});
|
|
63
66
|
}
|
|
64
|
-
async countWhere({ where, args, join, user, groupBy, }) {
|
|
67
|
+
async countWhere({ where, args, join, user, groupBy, alias }) {
|
|
65
68
|
if (!_.isEmpty(groupBy)) {
|
|
66
69
|
const command = this.sqlService.select({
|
|
67
70
|
fields: [
|
|
@@ -71,7 +74,10 @@ class Repository {
|
|
|
71
74
|
alias: 'count',
|
|
72
75
|
},
|
|
73
76
|
],
|
|
74
|
-
from:
|
|
77
|
+
from: {
|
|
78
|
+
table: this.table,
|
|
79
|
+
alias: alias || this.table
|
|
80
|
+
},
|
|
75
81
|
join,
|
|
76
82
|
args,
|
|
77
83
|
where,
|
|
@@ -89,7 +95,10 @@ class Repository {
|
|
|
89
95
|
alias: 'count',
|
|
90
96
|
},
|
|
91
97
|
],
|
|
92
|
-
from:
|
|
98
|
+
from: {
|
|
99
|
+
table: this.table,
|
|
100
|
+
alias: alias || this.table
|
|
101
|
+
},
|
|
93
102
|
join,
|
|
94
103
|
args,
|
|
95
104
|
where,
|
|
@@ -98,12 +107,15 @@ class Repository {
|
|
|
98
107
|
return result[0].count;
|
|
99
108
|
}
|
|
100
109
|
}
|
|
101
|
-
async findWhere({ args, where, fields, join, page, pageSize, orderBy, totalize, user, groupBy, }) {
|
|
110
|
+
async findWhere({ args, where, fields, join, page, pageSize, orderBy, totalize, user, groupBy, alias }) {
|
|
102
111
|
const limit = page ? pageSize || 20 : pageSize;
|
|
103
112
|
const offset = (page - 1) * (pageSize || 20);
|
|
104
113
|
const command = this.sqlService.select({
|
|
105
114
|
fields,
|
|
106
|
-
from:
|
|
115
|
+
from: {
|
|
116
|
+
table: this.table,
|
|
117
|
+
alias: alias || this.table
|
|
118
|
+
},
|
|
107
119
|
join,
|
|
108
120
|
args,
|
|
109
121
|
where,
|
|
@@ -121,6 +133,7 @@ class Repository {
|
|
|
121
133
|
where,
|
|
122
134
|
user,
|
|
123
135
|
groupBy,
|
|
136
|
+
alias
|
|
124
137
|
})
|
|
125
138
|
: new Promise(r => r(null)),
|
|
126
139
|
]);
|
|
@@ -134,7 +147,7 @@ class Repository {
|
|
|
134
147
|
};
|
|
135
148
|
return retorno;
|
|
136
149
|
}
|
|
137
|
-
async findBy({ key, value, fields, page, pageSize, orderBy, groupBy, totalize, user, }) {
|
|
150
|
+
async findBy({ key, value, fields, page, pageSize, orderBy, groupBy, totalize, user, alias }) {
|
|
138
151
|
return await this.findWhere({
|
|
139
152
|
fields,
|
|
140
153
|
args: {
|
|
@@ -151,12 +164,13 @@ class Repository {
|
|
|
151
164
|
orderBy,
|
|
152
165
|
groupBy,
|
|
153
166
|
user,
|
|
167
|
+
alias
|
|
154
168
|
});
|
|
155
169
|
}
|
|
156
170
|
async countAll(args = {}) {
|
|
157
171
|
return await this.countWhere({ user: args.user });
|
|
158
172
|
}
|
|
159
|
-
async findAll({ fields, page, pageSize, orderBy, groupBy, totalize, join, user, }) {
|
|
173
|
+
async findAll({ fields, page, pageSize, orderBy, groupBy, totalize, join, user, alias }) {
|
|
160
174
|
return await this.findWhere({
|
|
161
175
|
fields,
|
|
162
176
|
join,
|
|
@@ -166,6 +180,7 @@ class Repository {
|
|
|
166
180
|
totalize,
|
|
167
181
|
user,
|
|
168
182
|
groupBy,
|
|
183
|
+
alias
|
|
169
184
|
});
|
|
170
185
|
}
|
|
171
186
|
async insert({ entity, user, options, }) {
|
|
@@ -181,7 +196,15 @@ class Repository {
|
|
|
181
196
|
await this.database.serialExec(commands);
|
|
182
197
|
return retorno;
|
|
183
198
|
}
|
|
184
|
-
async upinsert({ entity, user, }) {
|
|
199
|
+
async upinsert({ entity: originalEntity, user, }) {
|
|
200
|
+
const entity = Object.entries(originalEntity)
|
|
201
|
+
.filter(([k, v]) => {
|
|
202
|
+
return v !== undefined;
|
|
203
|
+
})
|
|
204
|
+
.reduce((r, [k, v]) => {
|
|
205
|
+
r[k] = v;
|
|
206
|
+
return r;
|
|
207
|
+
}, {});
|
|
185
208
|
const commands = this.sqlService.update({
|
|
186
209
|
tableName: this.table,
|
|
187
210
|
set: Object.keys(entity)
|