@ruiapp/rapid-core 0.4.0 → 0.5.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/index.d.ts +2 -0
- package/dist/index.js +343 -121
- 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/index.ts +3 -0
- package/src/plugins/auth/actionHandlers/createSession.ts +12 -0
- 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/setting/models/SystemSettingItem.ts +6 -0
- package/src/plugins/setting/models/UserSettingItem.ts +6 -0
- package/src/utilities/typeUtility.ts +4 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* License plugin
|
|
3
|
+
*/
|
|
4
|
+
import { RpdApplicationConfig } from "../../types";
|
|
5
|
+
import { IRpdServer, RapidPlugin, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "../../core/server";
|
|
6
|
+
import LicenseService from "./LicenseService";
|
|
7
|
+
import { LicensePluginInitOptions } from "./LicensePluginTypes";
|
|
8
|
+
declare class LicensePlugin implements RapidPlugin {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(options: LicensePluginInitOptions);
|
|
11
|
+
get licenseService(): LicenseService;
|
|
12
|
+
get code(): string;
|
|
13
|
+
get description(): string;
|
|
14
|
+
get extendingAbilities(): RpdServerPluginExtendingAbilities[];
|
|
15
|
+
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[];
|
|
16
|
+
get configurations(): RpdConfigurationItemOptions[];
|
|
17
|
+
registerActionHandlers(server: IRpdServer): Promise<any>;
|
|
18
|
+
configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
19
|
+
configureServices(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
20
|
+
configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
21
|
+
onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export default LicensePlugin;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export type LicensePluginInitOptions = {
|
|
2
|
+
encryptionKey: string;
|
|
3
|
+
};
|
|
4
|
+
export type RpdCert = {
|
|
5
|
+
/**
|
|
6
|
+
* 许可证id
|
|
7
|
+
*/
|
|
8
|
+
id: string;
|
|
9
|
+
/**
|
|
10
|
+
* 公钥
|
|
11
|
+
*/
|
|
12
|
+
pub: string;
|
|
13
|
+
/**
|
|
14
|
+
* 加密后的授权信息
|
|
15
|
+
*/
|
|
16
|
+
lic: string;
|
|
17
|
+
/**
|
|
18
|
+
* authtag
|
|
19
|
+
*/
|
|
20
|
+
tag: string;
|
|
21
|
+
/**
|
|
22
|
+
* 证书签名
|
|
23
|
+
*/
|
|
24
|
+
sig: string;
|
|
25
|
+
};
|
|
26
|
+
export type RpdLicense = {
|
|
27
|
+
/**
|
|
28
|
+
* 颁发日期
|
|
29
|
+
*/
|
|
30
|
+
issueDate: string;
|
|
31
|
+
/**
|
|
32
|
+
* 产品信息
|
|
33
|
+
*/
|
|
34
|
+
product: RpdLicenseProduct;
|
|
35
|
+
/**
|
|
36
|
+
* 被授权人,客户信息
|
|
37
|
+
*/
|
|
38
|
+
grantTo: RpdLicenseOwner;
|
|
39
|
+
/**
|
|
40
|
+
* 权限信息
|
|
41
|
+
*/
|
|
42
|
+
authority: RpdLicenseAuthority;
|
|
43
|
+
};
|
|
44
|
+
export type RpdLicenseProduct = {
|
|
45
|
+
name: string;
|
|
46
|
+
version: string;
|
|
47
|
+
};
|
|
48
|
+
export type RpdLicenseOwner = {
|
|
49
|
+
name: string;
|
|
50
|
+
};
|
|
51
|
+
export type RpdLicenseAuthority = {
|
|
52
|
+
neverExpire: boolean;
|
|
53
|
+
expireDate: string;
|
|
54
|
+
quota?: Record<string, number | string | boolean>;
|
|
55
|
+
functions?: string[];
|
|
56
|
+
};
|
|
57
|
+
export type RpdLicenseApplication = {
|
|
58
|
+
/**
|
|
59
|
+
* 部署识别码
|
|
60
|
+
*/
|
|
61
|
+
deployId: string;
|
|
62
|
+
/**
|
|
63
|
+
* 产品信息
|
|
64
|
+
*/
|
|
65
|
+
product: RpdLicenseProduct;
|
|
66
|
+
/**
|
|
67
|
+
* 申请人,客户信息
|
|
68
|
+
*/
|
|
69
|
+
applicant: RpdLicenseOwner;
|
|
70
|
+
/**
|
|
71
|
+
* 权限信息
|
|
72
|
+
*/
|
|
73
|
+
authority: RpdLicenseAuthority;
|
|
74
|
+
};
|
|
75
|
+
export type LicenseSettings = {
|
|
76
|
+
deployId: string;
|
|
77
|
+
cert: string;
|
|
78
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { IRpdServer } from "../../core/server";
|
|
2
|
+
import { RpdLicense } from "./LicensePluginTypes";
|
|
3
|
+
export interface GetSystemSettingValuesInput {
|
|
4
|
+
groupCode: string;
|
|
5
|
+
}
|
|
6
|
+
export interface SetSystemSettingValuesInput {
|
|
7
|
+
groupCode: string;
|
|
8
|
+
values: Record<string, any>;
|
|
9
|
+
}
|
|
10
|
+
export interface GetUserSettingValuesInput {
|
|
11
|
+
groupCode: string;
|
|
12
|
+
}
|
|
13
|
+
export default class LicenseService {
|
|
14
|
+
#private;
|
|
15
|
+
constructor(server: IRpdServer, encryptionKey: string);
|
|
16
|
+
loadLicense(): Promise<void>;
|
|
17
|
+
getLicense(): RpdLicense;
|
|
18
|
+
isExpired(): boolean;
|
|
19
|
+
getQuota(name: string): any;
|
|
20
|
+
isOutOfQuota(name: string, currentAmount: number): boolean;
|
|
21
|
+
isFunctionAllowed(name: string): boolean;
|
|
22
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ActionHandlerContext } from "../../../core/actionHandler";
|
|
2
|
+
import { RapidPlugin } from "../../../core/server";
|
|
3
|
+
export interface GetLicenseOptions {
|
|
4
|
+
}
|
|
5
|
+
export declare const code = "getLicense";
|
|
6
|
+
export declare function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: GetLicenseOptions): Promise<void>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { BinaryToTextEncoding } from "crypto";
|
|
4
|
+
export declare function getEncryptionIV(input: string): string;
|
|
5
|
+
export declare function generatePubKeyFileContent(base64EncodedPubKey: string): string;
|
|
6
|
+
export declare function decryptData(encryptedData: string, encryptedDataEncoding: BufferEncoding, decryptedDataEncoding: BufferEncoding, encryptionKey: string, encryptionIV: string, authTag: string): string;
|
|
7
|
+
export declare function generateDigitalSignature(data: string, dataEncoding: BufferEncoding, privateKey: string): string;
|
|
8
|
+
export declare function validateDigitalSignature(data: string, dataEncoding: BufferEncoding, signature: string, signatureEncoding: BinaryToTextEncoding, publicKey: string): boolean;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -31,6 +31,9 @@ export { default as AuthPlugin } from "./plugins/auth/AuthPlugin";
|
|
|
31
31
|
|
|
32
32
|
export { default as FileManagePlugin } from "./plugins/fileManage/FileManagePlugin";
|
|
33
33
|
|
|
34
|
+
export { default as LicensePlugin } from "./plugins/license/LicensePlugin";
|
|
35
|
+
export * from "./plugins/license/LicensePluginTypes";
|
|
36
|
+
|
|
34
37
|
export { default as MailPlugin } from "./plugins/mail/MailPlugin";
|
|
35
38
|
export * from "./plugins/mail/MailPluginTypes";
|
|
36
39
|
|
|
@@ -3,6 +3,8 @@ import { setCookie } from "~/deno-std/http/cookie";
|
|
|
3
3
|
import { createJwt } from "~/utilities/jwtUtility";
|
|
4
4
|
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
5
5
|
import { RapidPlugin } from "~/core/server";
|
|
6
|
+
import LicenseService from "~/plugins/license/LicenseService";
|
|
7
|
+
import { get } from "lodash";
|
|
6
8
|
|
|
7
9
|
export interface UserAccessToken {
|
|
8
10
|
sub: "userAccessToken";
|
|
@@ -16,6 +18,16 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
|
|
|
16
18
|
const { response } = routeContext;
|
|
17
19
|
const { account, password } = input;
|
|
18
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
|
+
|
|
19
31
|
const userDataAccessor = server.getDataAccessor({
|
|
20
32
|
singularCode: "oc_user",
|
|
21
33
|
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* License 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 LicenseService from "./LicenseService";
|
|
18
|
+
import { LicensePluginInitOptions } from "./LicensePluginTypes";
|
|
19
|
+
|
|
20
|
+
class LicensePlugin implements RapidPlugin {
|
|
21
|
+
#licenseService!: LicenseService;
|
|
22
|
+
#encryptionKey: string;
|
|
23
|
+
|
|
24
|
+
constructor(options: LicensePluginInitOptions) {
|
|
25
|
+
if (!options.encryptionKey) {
|
|
26
|
+
throw new Error(`"encryptionKey" must be provided.`);
|
|
27
|
+
}
|
|
28
|
+
this.#encryptionKey = options.encryptionKey;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get licenseService() {
|
|
32
|
+
return this.#licenseService;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get code(): string {
|
|
36
|
+
return "license";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get description(): string {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get configurations(): RpdConfigurationItemOptions[] {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async registerActionHandlers(server: IRpdServer): Promise<any> {
|
|
56
|
+
for (const actionHandler of pluginActionHandlers) {
|
|
57
|
+
server.registerActionHandler(this, actionHandler);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
62
|
+
server.appendApplicationConfig({ models: pluginModels });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async configureServices(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
66
|
+
this.#licenseService = new LicenseService(server, this.#encryptionKey);
|
|
67
|
+
server.registerService("licenseService", this.#licenseService);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
71
|
+
server.appendApplicationConfig({ routes: pluginRoutes });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig) {
|
|
75
|
+
await this.#licenseService.loadLicense();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default LicensePlugin;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export type LicensePluginInitOptions = {
|
|
2
|
+
encryptionKey: string;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export type RpdCert = {
|
|
6
|
+
/**
|
|
7
|
+
* 许可证id
|
|
8
|
+
*/
|
|
9
|
+
id: string;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 公钥
|
|
13
|
+
*/
|
|
14
|
+
pub: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 加密后的授权信息
|
|
18
|
+
*/
|
|
19
|
+
lic: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* authtag
|
|
23
|
+
*/
|
|
24
|
+
tag: string;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 证书签名
|
|
28
|
+
*/
|
|
29
|
+
sig: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type RpdLicense = {
|
|
33
|
+
/**
|
|
34
|
+
* 颁发日期
|
|
35
|
+
*/
|
|
36
|
+
issueDate: string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 产品信息
|
|
40
|
+
*/
|
|
41
|
+
product: RpdLicenseProduct;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 被授权人,客户信息
|
|
45
|
+
*/
|
|
46
|
+
grantTo: RpdLicenseOwner;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 权限信息
|
|
50
|
+
*/
|
|
51
|
+
authority: RpdLicenseAuthority;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type RpdLicenseProduct = {
|
|
55
|
+
name: string;
|
|
56
|
+
version: string;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export type RpdLicenseOwner = {
|
|
60
|
+
name: string;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type RpdLicenseAuthority = {
|
|
64
|
+
neverExpire: boolean;
|
|
65
|
+
expireDate: string;
|
|
66
|
+
quota?: Record<string, number | string | boolean>;
|
|
67
|
+
functions?: string[];
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export type RpdLicenseApplication = {
|
|
71
|
+
/**
|
|
72
|
+
* 部署识别码
|
|
73
|
+
*/
|
|
74
|
+
deployId: string;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 产品信息
|
|
78
|
+
*/
|
|
79
|
+
product: RpdLicenseProduct;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 申请人,客户信息
|
|
83
|
+
*/
|
|
84
|
+
applicant: RpdLicenseOwner;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 权限信息
|
|
88
|
+
*/
|
|
89
|
+
authority: RpdLicenseAuthority;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export type LicenseSettings = {
|
|
93
|
+
deployId: string;
|
|
94
|
+
cert: string;
|
|
95
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { IRpdServer } from "~/core/server";
|
|
2
|
+
import EntityManager from "~/dataAccess/entityManager";
|
|
3
|
+
import { LicenseSettings, RpdCert, RpdLicense } from "./LicensePluginTypes";
|
|
4
|
+
import { SystemSettingItem } from "../setting/SettingPluginTypes";
|
|
5
|
+
import SettingService from "../setting/SettingService";
|
|
6
|
+
import { extractCertLicense } from "./helpers/certHelper";
|
|
7
|
+
import dayjs from "dayjs";
|
|
8
|
+
import { get, isString } from "lodash";
|
|
9
|
+
import { isNullOrUndefinedOrEmpty } from "~/utilities/typeUtility";
|
|
10
|
+
|
|
11
|
+
export interface GetSystemSettingValuesInput {
|
|
12
|
+
groupCode: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SetSystemSettingValuesInput {
|
|
16
|
+
groupCode: string;
|
|
17
|
+
values: Record<string, any>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface GetUserSettingValuesInput {
|
|
21
|
+
groupCode: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default class LicenseService {
|
|
25
|
+
#server: IRpdServer;
|
|
26
|
+
#systemSettingItemManager: EntityManager<SystemSettingItem>;
|
|
27
|
+
#encryptionKey: string;
|
|
28
|
+
#license: RpdLicense;
|
|
29
|
+
|
|
30
|
+
constructor(server: IRpdServer, encryptionKey: string) {
|
|
31
|
+
this.#server = server;
|
|
32
|
+
this.#encryptionKey = encryptionKey;
|
|
33
|
+
|
|
34
|
+
this.#systemSettingItemManager = server.getEntityManager("system_setting_item");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async loadLicense(): Promise<void> {
|
|
38
|
+
const settingService = this.#server.getService<SettingService>("settingService");
|
|
39
|
+
const licenseSettings = await settingService.getSystemSettingValues("license");
|
|
40
|
+
const { deployId } = licenseSettings as LicenseSettings;
|
|
41
|
+
const certText = licenseSettings.cert;
|
|
42
|
+
const certJSON = Buffer.from(certText, "base64").toString();
|
|
43
|
+
const cert: RpdCert = JSON.parse(certJSON);
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const license = extractCertLicense(this.#encryptionKey, deployId, cert);
|
|
47
|
+
this.#license = license;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
this.#server.getLogger().error("Loading license failed.", error);
|
|
50
|
+
throw new Error("Loading license failed.");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getLicense() {
|
|
55
|
+
return this.#license;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
isExpired() {
|
|
59
|
+
if (!this.#license) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const { neverExpire, expireDate } = this.#license.authority;
|
|
64
|
+
|
|
65
|
+
if (neverExpire) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!expireDate) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const today = dayjs(dayjs().format("YYYY-MM-DD"));
|
|
74
|
+
return today.isAfter(dayjs(expireDate));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
getQuota(name: string) {
|
|
78
|
+
if (!this.#license) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return get(this.#license.authority, `quota.${name}`, null);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
isOutOfQuota(name: string, currentAmount: number) {
|
|
86
|
+
const quotaLimit = this.getQuota(name);
|
|
87
|
+
|
|
88
|
+
if (isNullOrUndefinedOrEmpty(quotaLimit)) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let quotaLimitAmount: string | number = quotaLimit;
|
|
93
|
+
if (isString(quotaLimitAmount)) {
|
|
94
|
+
quotaLimitAmount = parseInt(quotaLimit, 10);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (quotaLimitAmount === -1) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return currentAmount > quotaLimitAmount;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
isFunctionAllowed(name: string) {
|
|
105
|
+
if (!this.#license) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const functions = get(this.#license.authority, "functions", []);
|
|
110
|
+
return functions.includes(name);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
2
|
+
import { RapidPlugin } from "~/core/server";
|
|
3
|
+
import LicenseService from "../LicenseService";
|
|
4
|
+
|
|
5
|
+
export interface GetLicenseOptions {}
|
|
6
|
+
|
|
7
|
+
export const code = "getLicense";
|
|
8
|
+
|
|
9
|
+
export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: GetLicenseOptions) {
|
|
10
|
+
const { server, routerContext } = ctx;
|
|
11
|
+
const { response } = routerContext;
|
|
12
|
+
|
|
13
|
+
const licenseService = server.getService<LicenseService>("licenseService");
|
|
14
|
+
|
|
15
|
+
const license = licenseService.getLicense();
|
|
16
|
+
|
|
17
|
+
ctx.output = license;
|
|
18
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { RpdCert } from "../LicensePluginTypes";
|
|
2
|
+
import { decryptData, getEncryptionIV, validateDigitalSignature, generatePubKeyFileContent } from "./cryptoHelper";
|
|
3
|
+
|
|
4
|
+
export function extractCertLicense(encryptionKey: string, deployId: string, cert: RpdCert) {
|
|
5
|
+
const iv = getEncryptionIV(deployId);
|
|
6
|
+
const signature = cert.sig;
|
|
7
|
+
const pub = generatePubKeyFileContent(cert.pub);
|
|
8
|
+
const valid = validateDigitalSignature(cert.lic, "base64", signature, "base64", pub);
|
|
9
|
+
if (!valid) {
|
|
10
|
+
throw new Error("Certification validate failed.");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const licenseText = decryptData(cert.lic, "base64", "utf-8", encryptionKey, iv, cert.tag);
|
|
15
|
+
return JSON.parse(licenseText);
|
|
16
|
+
} catch (ex) {
|
|
17
|
+
throw new Error("Certification parse failed.", {
|
|
18
|
+
cause: ex,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import crypto, { BinaryToTextEncoding } from "crypto";
|
|
2
|
+
|
|
3
|
+
export function getEncryptionIV(input: string) {
|
|
4
|
+
const hash = crypto.createHash("sha512").update(input).digest("hex");
|
|
5
|
+
return hash.substring(0, 12);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function generatePubKeyFileContent(base64EncodedPubKey: string) {
|
|
9
|
+
return `-----BEGIN PUBLIC KEY-----\n${base64EncodedPubKey}\n-----END PUBLIC KEY-----`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const ENC_ALGORITHM: crypto.CipherGCMTypes = "aes-256-gcm";
|
|
13
|
+
|
|
14
|
+
// Decrypt data
|
|
15
|
+
export function decryptData(
|
|
16
|
+
encryptedData: string,
|
|
17
|
+
encryptedDataEncoding: BufferEncoding,
|
|
18
|
+
decryptedDataEncoding: BufferEncoding,
|
|
19
|
+
encryptionKey: string,
|
|
20
|
+
encryptionIV: string,
|
|
21
|
+
authTag: string,
|
|
22
|
+
) {
|
|
23
|
+
const decipher = crypto.createDecipheriv(ENC_ALGORITHM, Buffer.from(encryptionKey, "base64"), encryptionIV);
|
|
24
|
+
decipher.setAuthTag(Buffer.from(authTag, "base64"));
|
|
25
|
+
const buff = Buffer.from(encryptedData, encryptedDataEncoding);
|
|
26
|
+
return Buffer.concat([decipher.update(buff), decipher.final()]).toString(decryptedDataEncoding); // Decrypts data and converts to utf8
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function generateDigitalSignature(data: string, dataEncoding: BufferEncoding, privateKey: string): string {
|
|
30
|
+
const sign = crypto.createSign("RSA-SHA256");
|
|
31
|
+
const buffer = Buffer.from(data, dataEncoding);
|
|
32
|
+
sign.update(buffer);
|
|
33
|
+
return sign.sign(privateKey, "base64");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function validateDigitalSignature(
|
|
37
|
+
data: string,
|
|
38
|
+
dataEncoding: BufferEncoding,
|
|
39
|
+
signature: string,
|
|
40
|
+
signatureEncoding: BinaryToTextEncoding,
|
|
41
|
+
publicKey: string,
|
|
42
|
+
): boolean {
|
|
43
|
+
const verify = crypto.createVerify("RSA-SHA256");
|
|
44
|
+
const buffer = Buffer.from(data, dataEncoding);
|
|
45
|
+
verify.update(buffer);
|
|
46
|
+
return verify.verify(publicKey, signature, signatureEncoding);
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default [];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RpdRoute } from "~/types";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
namespace: "svc",
|
|
5
|
+
name: "svc.getLicense",
|
|
6
|
+
code: "svc.getLicense",
|
|
7
|
+
type: "RESTful",
|
|
8
|
+
method: "GET",
|
|
9
|
+
endpoint: "/svc/license",
|
|
10
|
+
actions: [
|
|
11
|
+
{
|
|
12
|
+
code: "getLicense",
|
|
13
|
+
},
|
|
14
|
+
],
|
|
15
|
+
} satisfies RpdRoute;
|