@ruiapp/rapid-core 0.3.0 → 0.3.2
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/CHANGELOG.md +7 -7
- package/dist/index.d.ts +2 -0
- package/dist/index.js +359 -134
- package/dist/plugins/license/LicensePlugin.d.ts +23 -0
- package/dist/plugins/license/LicensePluginTypes.d.ts +78 -0
- package/dist/plugins/license/LicenseService.d.ts +22 -0
- package/dist/plugins/license/actionHandlers/getLicense.d.ts +6 -0
- package/dist/plugins/license/actionHandlers/index.d.ts +3 -0
- package/dist/plugins/license/helpers/certHelper.d.ts +2 -0
- package/dist/plugins/license/helpers/cryptoHelper.d.ts +8 -0
- package/dist/plugins/license/models/index.d.ts +2 -0
- package/dist/plugins/license/routes/getLicense.d.ts +12 -0
- package/dist/plugins/license/routes/index.d.ts +12 -0
- package/dist/utilities/typeUtility.d.ts +1 -0
- package/package.json +1 -1
- package/src/bootstrapApplicationConfig.ts +631 -631
- package/src/core/response.ts +76 -76
- package/src/core/routeContext.ts +47 -47
- package/src/core/server.ts +142 -142
- package/src/dataAccess/columnTypeMapper.ts +22 -22
- package/src/dataAccess/dataAccessTypes.ts +163 -163
- package/src/dataAccess/dataAccessor.ts +135 -135
- package/src/dataAccess/entityManager.ts +1718 -1718
- package/src/dataAccess/entityMapper.ts +100 -100
- package/src/dataAccess/propertyMapper.ts +28 -28
- package/src/deno-std/http/cookie.ts +372 -372
- package/src/helpers/entityHelpers.ts +76 -76
- package/src/helpers/filterHelper.ts +148 -148
- package/src/helpers/metaHelper.ts +89 -89
- package/src/helpers/runCollectionEntityActionHandler.ts +27 -27
- package/src/index.ts +57 -54
- package/src/plugins/auth/AuthPlugin.ts +85 -85
- package/src/plugins/auth/actionHandlers/changePassword.ts +54 -54
- package/src/plugins/auth/actionHandlers/createSession.ts +75 -63
- package/src/plugins/auth/actionHandlers/resetPassword.ts +38 -38
- package/src/plugins/cronJob/CronJobPlugin.ts +112 -112
- package/src/plugins/dataManage/DataManagePlugin.ts +163 -163
- package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +20 -20
- package/src/plugins/dataManage/actionHandlers/countCollectionEntities.ts +16 -16
- package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +42 -42
- package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +24 -24
- package/src/plugins/dataManage/actionHandlers/deleteCollectionEntities.ts +38 -38
- package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +22 -22
- package/src/plugins/dataManage/actionHandlers/findCollectionEntities.ts +26 -26
- package/src/plugins/dataManage/actionHandlers/findCollectionEntityById.ts +21 -21
- package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +20 -20
- package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +41 -41
- package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +146 -146
- package/src/plugins/fileManage/FileManagePlugin.ts +52 -52
- package/src/plugins/fileManage/actionHandlers/downloadDocument.ts +65 -65
- package/src/plugins/fileManage/actionHandlers/downloadFile.ts +44 -44
- package/src/plugins/license/LicensePlugin.ts +79 -0
- package/src/plugins/license/LicensePluginTypes.ts +95 -0
- package/src/plugins/license/LicenseService.ts +112 -0
- package/src/plugins/license/actionHandlers/getLicense.ts +18 -0
- package/src/plugins/license/actionHandlers/index.ts +4 -0
- package/src/plugins/license/helpers/certHelper.ts +21 -0
- package/src/plugins/license/helpers/cryptoHelper.ts +47 -0
- package/src/plugins/license/models/index.ts +1 -0
- package/src/plugins/license/routes/getLicense.ts +15 -0
- package/src/plugins/license/routes/index.ts +3 -0
- package/src/plugins/mail/MailPlugin.ts +74 -74
- package/src/plugins/mail/MailPluginTypes.ts +27 -27
- package/src/plugins/mail/MailService.ts +38 -38
- package/src/plugins/mail/actionHandlers/index.ts +3 -3
- package/src/plugins/mail/models/index.ts +1 -1
- package/src/plugins/mail/routes/index.ts +1 -1
- package/src/plugins/metaManage/MetaManagePlugin.ts +504 -504
- package/src/plugins/notification/NotificationPlugin.ts +68 -68
- package/src/plugins/notification/NotificationPluginTypes.ts +13 -13
- package/src/plugins/notification/NotificationService.ts +25 -25
- package/src/plugins/notification/actionHandlers/index.ts +3 -3
- package/src/plugins/notification/models/Notification.ts +60 -57
- package/src/plugins/notification/models/index.ts +3 -3
- package/src/plugins/notification/routes/index.ts +1 -1
- package/src/plugins/routeManage/RouteManagePlugin.ts +62 -62
- package/src/plugins/sequence/SequencePlugin.ts +136 -136
- package/src/plugins/sequence/SequencePluginTypes.ts +69 -69
- package/src/plugins/sequence/SequenceService.ts +81 -81
- package/src/plugins/sequence/actionHandlers/generateSn.ts +32 -32
- package/src/plugins/sequence/segments/autoIncrement.ts +78 -78
- package/src/plugins/sequence/segments/dayOfMonth.ts +17 -17
- package/src/plugins/sequence/segments/literal.ts +14 -14
- package/src/plugins/sequence/segments/month.ts +17 -17
- package/src/plugins/sequence/segments/parameter.ts +18 -18
- package/src/plugins/sequence/segments/year.ts +17 -17
- package/src/plugins/setting/SettingPlugin.ts +68 -68
- package/src/plugins/setting/SettingPluginTypes.ts +37 -37
- package/src/plugins/setting/models/SystemSettingItem.ts +48 -42
- package/src/plugins/setting/models/UserSettingItem.ts +55 -49
- package/src/plugins/stateMachine/StateMachinePlugin.ts +186 -186
- package/src/plugins/stateMachine/StateMachinePluginTypes.ts +48 -48
- package/src/plugins/stateMachine/actionHandlers/sendStateMachineEvent.ts +51 -51
- package/src/plugins/webhooks/WebhooksPlugin.ts +148 -148
- package/src/plugins/webhooks/pluginConfig.ts +75 -75
- package/src/queryBuilder/queryBuilder.ts +665 -665
- package/src/server.ts +463 -463
- package/src/types.ts +701 -701
- package/src/utilities/errorUtility.ts +15 -15
- package/src/utilities/pathUtility.ts +14 -14
- package/src/utilities/typeUtility.ts +15 -11
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { RunEntityActionHandlerOptions } from "~/types";
|
|
2
|
-
import { mergeInput } from "./inputHelper";
|
|
3
|
-
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
4
|
-
import EntityManager from "~/dataAccess/entityManager";
|
|
5
|
-
|
|
6
|
-
type EntityActionHandler = (entityManager: EntityManager, input: any) => Promise<any>;
|
|
7
|
-
|
|
8
|
-
export default async function runCollectionEntityActionHandler(
|
|
9
|
-
ctx: ActionHandlerContext,
|
|
10
|
-
options: RunEntityActionHandlerOptions,
|
|
11
|
-
code: string,
|
|
12
|
-
handleEntityAction: EntityActionHandler,
|
|
13
|
-
) {
|
|
14
|
-
const { logger, server, input } = ctx;
|
|
15
|
-
|
|
16
|
-
const { defaultInput, fixedInput } = options;
|
|
17
|
-
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
18
|
-
logger.debug(`Running ${code} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
19
|
-
|
|
20
|
-
const entityManager = server.getEntityManager(options.singularCode);
|
|
21
|
-
const result = handleEntityAction(entityManager, mergedInput);
|
|
22
|
-
if (result instanceof Promise) {
|
|
23
|
-
ctx.output = await result;
|
|
24
|
-
} else {
|
|
25
|
-
ctx.output = result;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
1
|
+
import { RunEntityActionHandlerOptions } from "~/types";
|
|
2
|
+
import { mergeInput } from "./inputHelper";
|
|
3
|
+
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
4
|
+
import EntityManager from "~/dataAccess/entityManager";
|
|
5
|
+
|
|
6
|
+
type EntityActionHandler = (entityManager: EntityManager, input: any) => Promise<any>;
|
|
7
|
+
|
|
8
|
+
export default async function runCollectionEntityActionHandler(
|
|
9
|
+
ctx: ActionHandlerContext,
|
|
10
|
+
options: RunEntityActionHandlerOptions,
|
|
11
|
+
code: string,
|
|
12
|
+
handleEntityAction: EntityActionHandler,
|
|
13
|
+
) {
|
|
14
|
+
const { logger, server, input } = ctx;
|
|
15
|
+
|
|
16
|
+
const { defaultInput, fixedInput } = options;
|
|
17
|
+
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
18
|
+
logger.debug(`Running ${code} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
19
|
+
|
|
20
|
+
const entityManager = server.getEntityManager(options.singularCode);
|
|
21
|
+
const result = handleEntityAction(entityManager, mergedInput);
|
|
22
|
+
if (result instanceof Promise) {
|
|
23
|
+
ctx.output = await result;
|
|
24
|
+
} else {
|
|
25
|
+
ctx.output = result;
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,54 +1,57 @@
|
|
|
1
|
-
import { fixBigIntJSONSerialize } from "./polyfill";
|
|
2
|
-
fixBigIntJSONSerialize();
|
|
3
|
-
|
|
4
|
-
export * from "./types";
|
|
5
|
-
export * from "./server";
|
|
6
|
-
|
|
7
|
-
export * from "./core/request";
|
|
8
|
-
export * from "./core/routeContext";
|
|
9
|
-
export * from "./core/server";
|
|
10
|
-
export * from "./core/http-types";
|
|
11
|
-
export * from "./core/actionHandler";
|
|
12
|
-
|
|
13
|
-
export * from "./utilities/jwtUtility";
|
|
14
|
-
|
|
15
|
-
export { mapDbRowToEntity } from "./dataAccess/entityMapper";
|
|
16
|
-
|
|
17
|
-
export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
|
|
18
|
-
|
|
19
|
-
export { default as MetaManagePlugin } from "./plugins/metaManage/MetaManagePlugin";
|
|
20
|
-
|
|
21
|
-
export { default as DataManagePlugin } from "./plugins/dataManage/DataManagePlugin";
|
|
22
|
-
|
|
23
|
-
export { default as RouteManagePlugin } from "./plugins/routeManage/RouteManagePlugin";
|
|
24
|
-
|
|
25
|
-
export { default as SequencePlugin } from "./plugins/sequence/SequencePlugin";
|
|
26
|
-
export * from "./plugins/sequence/SequencePluginTypes";
|
|
27
|
-
|
|
28
|
-
export { default as WebhooksPlugin } from "./plugins/webhooks/WebhooksPlugin";
|
|
29
|
-
|
|
30
|
-
export { default as AuthPlugin } from "./plugins/auth/AuthPlugin";
|
|
31
|
-
|
|
32
|
-
export { default as FileManagePlugin } from "./plugins/fileManage/FileManagePlugin";
|
|
33
|
-
|
|
34
|
-
export { default as
|
|
35
|
-
export * from "./plugins/
|
|
36
|
-
|
|
37
|
-
export { default as
|
|
38
|
-
export * from "./plugins/
|
|
39
|
-
|
|
40
|
-
export { default as
|
|
41
|
-
export * from "./plugins/
|
|
42
|
-
|
|
43
|
-
export { default as
|
|
44
|
-
export * from "./plugins/
|
|
45
|
-
|
|
46
|
-
export { default as
|
|
47
|
-
export * from "./plugins/
|
|
48
|
-
|
|
49
|
-
export { default as
|
|
50
|
-
export * from "./plugins/
|
|
51
|
-
|
|
52
|
-
export { default as
|
|
53
|
-
|
|
54
|
-
|
|
1
|
+
import { fixBigIntJSONSerialize } from "./polyfill";
|
|
2
|
+
fixBigIntJSONSerialize();
|
|
3
|
+
|
|
4
|
+
export * from "./types";
|
|
5
|
+
export * from "./server";
|
|
6
|
+
|
|
7
|
+
export * from "./core/request";
|
|
8
|
+
export * from "./core/routeContext";
|
|
9
|
+
export * from "./core/server";
|
|
10
|
+
export * from "./core/http-types";
|
|
11
|
+
export * from "./core/actionHandler";
|
|
12
|
+
|
|
13
|
+
export * from "./utilities/jwtUtility";
|
|
14
|
+
|
|
15
|
+
export { mapDbRowToEntity } from "./dataAccess/entityMapper";
|
|
16
|
+
|
|
17
|
+
export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
|
|
18
|
+
|
|
19
|
+
export { default as MetaManagePlugin } from "./plugins/metaManage/MetaManagePlugin";
|
|
20
|
+
|
|
21
|
+
export { default as DataManagePlugin } from "./plugins/dataManage/DataManagePlugin";
|
|
22
|
+
|
|
23
|
+
export { default as RouteManagePlugin } from "./plugins/routeManage/RouteManagePlugin";
|
|
24
|
+
|
|
25
|
+
export { default as SequencePlugin } from "./plugins/sequence/SequencePlugin";
|
|
26
|
+
export * from "./plugins/sequence/SequencePluginTypes";
|
|
27
|
+
|
|
28
|
+
export { default as WebhooksPlugin } from "./plugins/webhooks/WebhooksPlugin";
|
|
29
|
+
|
|
30
|
+
export { default as AuthPlugin } from "./plugins/auth/AuthPlugin";
|
|
31
|
+
|
|
32
|
+
export { default as FileManagePlugin } from "./plugins/fileManage/FileManagePlugin";
|
|
33
|
+
|
|
34
|
+
export { default as LicensePlugin } from "./plugins/license/LicensePlugin";
|
|
35
|
+
export * from "./plugins/license/LicensePluginTypes";
|
|
36
|
+
|
|
37
|
+
export { default as MailPlugin } from "./plugins/mail/MailPlugin";
|
|
38
|
+
export * from "./plugins/mail/MailPluginTypes";
|
|
39
|
+
|
|
40
|
+
export { default as NotificationPlugin } from "./plugins/notification/NotificationPlugin";
|
|
41
|
+
export * from "./plugins/notification/NotificationPluginTypes";
|
|
42
|
+
|
|
43
|
+
export { default as ServerOperationPlugin } from "./plugins/serverOperation/ServerOperationPlugin";
|
|
44
|
+
export * from "./plugins/serverOperation/ServerOperationPluginTypes";
|
|
45
|
+
|
|
46
|
+
export { default as SettingPlugin } from "./plugins/setting/SettingPlugin";
|
|
47
|
+
export * from "./plugins/setting/SettingPluginTypes";
|
|
48
|
+
|
|
49
|
+
export { default as CronJobPlugin } from "./plugins/cronJob/CronJobPlugin";
|
|
50
|
+
export * from "./plugins/cronJob/CronJobPluginTypes";
|
|
51
|
+
|
|
52
|
+
export { default as StateMachinePlugin } from "./plugins/stateMachine/StateMachinePlugin";
|
|
53
|
+
export * from "./plugins/stateMachine/StateMachinePluginTypes";
|
|
54
|
+
|
|
55
|
+
export { default as EntityAccessControlPlugin } from "./plugins/entityAccessControl/EntityAccessControlPlugin";
|
|
56
|
+
|
|
57
|
+
export * from "./facilities/log/LogFacility";
|
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth manager plugin
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { RpdApplicationConfig } from "~/types";
|
|
6
|
-
import {
|
|
7
|
-
IRpdServer,
|
|
8
|
-
RapidPlugin,
|
|
9
|
-
RpdConfigurationItemOptions,
|
|
10
|
-
RpdServerPluginConfigurableTargetOptions,
|
|
11
|
-
RpdServerPluginExtendingAbilities,
|
|
12
|
-
} from "~/core/server";
|
|
13
|
-
|
|
14
|
-
import pluginActionHandlers from "./actionHandlers";
|
|
15
|
-
import pluginModels from "./models";
|
|
16
|
-
import pluginRoutes from "./routes";
|
|
17
|
-
import { RouteContext } from "~/core/routeContext";
|
|
18
|
-
import { verifyJwt } from "~/utilities/jwtUtility";
|
|
19
|
-
|
|
20
|
-
class AuthPlugin implements RapidPlugin {
|
|
21
|
-
get code(): string {
|
|
22
|
-
return "authManager";
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
get description(): string {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
|
|
30
|
-
return [];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
|
|
34
|
-
return [];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get configurations(): RpdConfigurationItemOptions[] {
|
|
38
|
-
return [];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async registerActionHandlers(server: IRpdServer): Promise<any> {
|
|
42
|
-
for (const actionHandler of pluginActionHandlers) {
|
|
43
|
-
server.registerActionHandler(this, actionHandler);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
48
|
-
server.appendApplicationConfig({ models: pluginModels });
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
52
|
-
server.appendApplicationConfig({ routes: pluginRoutes });
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async onPrepareRouteContext(server: IRpdServer, routeContext: RouteContext) {
|
|
56
|
-
const request = routeContext.request;
|
|
57
|
-
let token: string;
|
|
58
|
-
|
|
59
|
-
const headers = request.headers;
|
|
60
|
-
// No Authorization header
|
|
61
|
-
if (headers.has("Authorization")) {
|
|
62
|
-
// Authorization header has no Bearer or no token
|
|
63
|
-
const authHeader = headers.get("Authorization")!;
|
|
64
|
-
if (!authHeader.startsWith("Bearer ") || authHeader.length <= 7) {
|
|
65
|
-
throw new Error("AUTHORIZATION_HEADER_INVALID");
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
token = authHeader.slice(7);
|
|
69
|
-
} else {
|
|
70
|
-
token = request.cookies[server.config.sessionCookieName];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
try {
|
|
74
|
-
const secretKey = Buffer.from(server.config.jwtKey, "base64");
|
|
75
|
-
const tokenPayload = verifyJwt(token, secretKey);
|
|
76
|
-
routeContext.state.userId = tokenPayload.aud as string;
|
|
77
|
-
routeContext.state.userLogin = tokenPayload.act as string;
|
|
78
|
-
} catch (error) {
|
|
79
|
-
const logger = server.getLogger();
|
|
80
|
-
logger.debug("Verify JWT failed.", { error });
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export default AuthPlugin;
|
|
1
|
+
/**
|
|
2
|
+
* Auth manager plugin
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { RpdApplicationConfig } from "~/types";
|
|
6
|
+
import {
|
|
7
|
+
IRpdServer,
|
|
8
|
+
RapidPlugin,
|
|
9
|
+
RpdConfigurationItemOptions,
|
|
10
|
+
RpdServerPluginConfigurableTargetOptions,
|
|
11
|
+
RpdServerPluginExtendingAbilities,
|
|
12
|
+
} from "~/core/server";
|
|
13
|
+
|
|
14
|
+
import pluginActionHandlers from "./actionHandlers";
|
|
15
|
+
import pluginModels from "./models";
|
|
16
|
+
import pluginRoutes from "./routes";
|
|
17
|
+
import { RouteContext } from "~/core/routeContext";
|
|
18
|
+
import { verifyJwt } from "~/utilities/jwtUtility";
|
|
19
|
+
|
|
20
|
+
class AuthPlugin implements RapidPlugin {
|
|
21
|
+
get code(): string {
|
|
22
|
+
return "authManager";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get description(): string {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get configurations(): RpdConfigurationItemOptions[] {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async registerActionHandlers(server: IRpdServer): Promise<any> {
|
|
42
|
+
for (const actionHandler of pluginActionHandlers) {
|
|
43
|
+
server.registerActionHandler(this, actionHandler);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
48
|
+
server.appendApplicationConfig({ models: pluginModels });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
52
|
+
server.appendApplicationConfig({ routes: pluginRoutes });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async onPrepareRouteContext(server: IRpdServer, routeContext: RouteContext) {
|
|
56
|
+
const request = routeContext.request;
|
|
57
|
+
let token: string;
|
|
58
|
+
|
|
59
|
+
const headers = request.headers;
|
|
60
|
+
// No Authorization header
|
|
61
|
+
if (headers.has("Authorization")) {
|
|
62
|
+
// Authorization header has no Bearer or no token
|
|
63
|
+
const authHeader = headers.get("Authorization")!;
|
|
64
|
+
if (!authHeader.startsWith("Bearer ") || authHeader.length <= 7) {
|
|
65
|
+
throw new Error("AUTHORIZATION_HEADER_INVALID");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
token = authHeader.slice(7);
|
|
69
|
+
} else {
|
|
70
|
+
token = request.cookies[server.config.sessionCookieName];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const secretKey = Buffer.from(server.config.jwtKey, "base64");
|
|
75
|
+
const tokenPayload = verifyJwt(token, secretKey);
|
|
76
|
+
routeContext.state.userId = tokenPayload.aud as string;
|
|
77
|
+
routeContext.state.userLogin = tokenPayload.act as string;
|
|
78
|
+
} catch (error) {
|
|
79
|
+
const logger = server.getLogger();
|
|
80
|
+
logger.debug("Verify JWT failed.", { error });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default AuthPlugin;
|
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
import bcrypt from "bcrypt";
|
|
2
|
-
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
3
|
-
import { RapidPlugin } from "~/core/server";
|
|
4
|
-
|
|
5
|
-
export const code = "changePassword";
|
|
6
|
-
|
|
7
|
-
export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
|
|
8
|
-
const { server, input, routerContext } = ctx;
|
|
9
|
-
const { response } = routerContext;
|
|
10
|
-
const { id, oldPassword, newPassword } = input;
|
|
11
|
-
|
|
12
|
-
const userId = routerContext.state.userId;
|
|
13
|
-
if (!userId) {
|
|
14
|
-
ctx.status = 401;
|
|
15
|
-
ctx.output = {
|
|
16
|
-
error: {
|
|
17
|
-
message: "You are not signed in.",
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const userDataAccessor = server.getDataAccessor({
|
|
24
|
-
singularCode: "oc_user",
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
const user = await userDataAccessor.findOne({
|
|
28
|
-
filters: [
|
|
29
|
-
{
|
|
30
|
-
operator: "eq",
|
|
31
|
-
field: "id",
|
|
32
|
-
value: userId,
|
|
33
|
-
},
|
|
34
|
-
],
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
if (!user) {
|
|
38
|
-
throw new Error("User not found.");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const isMatch = await bcrypt.compare(oldPassword, user.password);
|
|
42
|
-
if (!isMatch) {
|
|
43
|
-
throw new Error("旧密码错误。");
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const saltRounds = 10;
|
|
47
|
-
const passwordHash = await bcrypt.hash(newPassword, saltRounds);
|
|
48
|
-
|
|
49
|
-
await userDataAccessor.updateById(user.id, {
|
|
50
|
-
password: passwordHash,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
ctx.output = {};
|
|
54
|
-
}
|
|
1
|
+
import bcrypt from "bcrypt";
|
|
2
|
+
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
3
|
+
import { RapidPlugin } from "~/core/server";
|
|
4
|
+
|
|
5
|
+
export const code = "changePassword";
|
|
6
|
+
|
|
7
|
+
export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
|
|
8
|
+
const { server, input, routerContext } = ctx;
|
|
9
|
+
const { response } = routerContext;
|
|
10
|
+
const { id, oldPassword, newPassword } = input;
|
|
11
|
+
|
|
12
|
+
const userId = routerContext.state.userId;
|
|
13
|
+
if (!userId) {
|
|
14
|
+
ctx.status = 401;
|
|
15
|
+
ctx.output = {
|
|
16
|
+
error: {
|
|
17
|
+
message: "You are not signed in.",
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const userDataAccessor = server.getDataAccessor({
|
|
24
|
+
singularCode: "oc_user",
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const user = await userDataAccessor.findOne({
|
|
28
|
+
filters: [
|
|
29
|
+
{
|
|
30
|
+
operator: "eq",
|
|
31
|
+
field: "id",
|
|
32
|
+
value: userId,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (!user) {
|
|
38
|
+
throw new Error("User not found.");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const isMatch = await bcrypt.compare(oldPassword, user.password);
|
|
42
|
+
if (!isMatch) {
|
|
43
|
+
throw new Error("旧密码错误。");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const saltRounds = 10;
|
|
47
|
+
const passwordHash = await bcrypt.hash(newPassword, saltRounds);
|
|
48
|
+
|
|
49
|
+
await userDataAccessor.updateById(user.id, {
|
|
50
|
+
password: passwordHash,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
ctx.output = {};
|
|
54
|
+
}
|
|
@@ -1,63 +1,75 @@
|
|
|
1
|
-
import bcrypt from "bcrypt";
|
|
2
|
-
import { setCookie } from "~/deno-std/http/cookie";
|
|
3
|
-
import { createJwt } from "~/utilities/jwtUtility";
|
|
4
|
-
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
5
|
-
import { RapidPlugin } from "~/core/server";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const {
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
1
|
+
import bcrypt from "bcrypt";
|
|
2
|
+
import { setCookie } from "~/deno-std/http/cookie";
|
|
3
|
+
import { createJwt } from "~/utilities/jwtUtility";
|
|
4
|
+
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
5
|
+
import { RapidPlugin } from "~/core/server";
|
|
6
|
+
import LicenseService from "~/plugins/license/LicenseService";
|
|
7
|
+
import { get } from "lodash";
|
|
8
|
+
|
|
9
|
+
export interface UserAccessToken {
|
|
10
|
+
sub: "userAccessToken";
|
|
11
|
+
aud: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const code = "createSession";
|
|
15
|
+
|
|
16
|
+
export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
|
|
17
|
+
const { server, input, routerContext } = ctx;
|
|
18
|
+
const { response } = routerContext;
|
|
19
|
+
const { account, password } = input;
|
|
20
|
+
|
|
21
|
+
const licenseService = server.getService<LicenseService>("licenseService");
|
|
22
|
+
const license = licenseService.getLicense();
|
|
23
|
+
if (!license) {
|
|
24
|
+
throw new Error(`登录失败,无法获取系统授权信息。`);
|
|
25
|
+
}
|
|
26
|
+
if (licenseService.isExpired()) {
|
|
27
|
+
const expireDate = get(license.authority, "expireDate");
|
|
28
|
+
throw new Error(`登录失败,系统授权已于${expireDate}过期。`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const userDataAccessor = server.getDataAccessor({
|
|
32
|
+
singularCode: "oc_user",
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const user = await userDataAccessor.findOne({
|
|
36
|
+
filters: [
|
|
37
|
+
{
|
|
38
|
+
operator: "eq",
|
|
39
|
+
field: "login",
|
|
40
|
+
value: account,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (!user) {
|
|
46
|
+
throw new Error("用户名或密码错误。");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const isMatch = await bcrypt.compare(password, user.password);
|
|
50
|
+
if (!isMatch) {
|
|
51
|
+
throw new Error("用户名或密码错误。");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const secretKey = Buffer.from(server.config.jwtKey, "base64");
|
|
55
|
+
const token = createJwt(
|
|
56
|
+
{
|
|
57
|
+
iss: "authManager",
|
|
58
|
+
sub: "userAccessToken",
|
|
59
|
+
aud: "" + user.id,
|
|
60
|
+
iat: Math.floor(Date.now() / 1000),
|
|
61
|
+
act: user.login,
|
|
62
|
+
} as UserAccessToken,
|
|
63
|
+
secretKey,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
setCookie(response.headers, {
|
|
67
|
+
name: ctx.server.config.sessionCookieName,
|
|
68
|
+
value: token,
|
|
69
|
+
path: "/",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
ctx.output = {
|
|
73
|
+
token,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import bcrypt from "bcrypt";
|
|
2
|
-
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
3
|
-
import { RapidPlugin } from "~/core/server";
|
|
4
|
-
|
|
5
|
-
export const code = "resetPassword";
|
|
6
|
-
|
|
7
|
-
export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
|
|
8
|
-
const { server, input, routerContext } = ctx;
|
|
9
|
-
const { response } = routerContext;
|
|
10
|
-
const { userId, password } = input;
|
|
11
|
-
|
|
12
|
-
const userDataAccessor = server.getDataAccessor({
|
|
13
|
-
singularCode: "oc_user",
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
const user = await userDataAccessor.findOne({
|
|
17
|
-
filters: [
|
|
18
|
-
{
|
|
19
|
-
operator: "eq",
|
|
20
|
-
field: "id",
|
|
21
|
-
value: userId,
|
|
22
|
-
},
|
|
23
|
-
],
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
if (!user) {
|
|
27
|
-
throw new Error("User not found.");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const saltRounds = 10;
|
|
31
|
-
const passwordHash = await bcrypt.hash(password, saltRounds);
|
|
32
|
-
|
|
33
|
-
await userDataAccessor.updateById(user.id, {
|
|
34
|
-
password: passwordHash,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
ctx.output = {};
|
|
38
|
-
}
|
|
1
|
+
import bcrypt from "bcrypt";
|
|
2
|
+
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
3
|
+
import { RapidPlugin } from "~/core/server";
|
|
4
|
+
|
|
5
|
+
export const code = "resetPassword";
|
|
6
|
+
|
|
7
|
+
export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
|
|
8
|
+
const { server, input, routerContext } = ctx;
|
|
9
|
+
const { response } = routerContext;
|
|
10
|
+
const { userId, password } = input;
|
|
11
|
+
|
|
12
|
+
const userDataAccessor = server.getDataAccessor({
|
|
13
|
+
singularCode: "oc_user",
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const user = await userDataAccessor.findOne({
|
|
17
|
+
filters: [
|
|
18
|
+
{
|
|
19
|
+
operator: "eq",
|
|
20
|
+
field: "id",
|
|
21
|
+
value: userId,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
if (!user) {
|
|
27
|
+
throw new Error("User not found.");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const saltRounds = 10;
|
|
31
|
+
const passwordHash = await bcrypt.hash(password, saltRounds);
|
|
32
|
+
|
|
33
|
+
await userDataAccessor.updateById(user.id, {
|
|
34
|
+
password: passwordHash,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
ctx.output = {};
|
|
38
|
+
}
|