@canva/cli 0.0.1-beta.26 → 0.0.1-beta.29
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/cli.js +469 -443
- package/package.json +7 -2
- package/templates/base/webpack.config.ts +12 -4
- package/templates/dam/eslint.config.mjs +1 -2
- package/templates/dam/src/config.ts +3 -0
- package/templates/dam/webpack.config.ts +12 -4
- package/templates/data_connector/README.md +84 -0
- package/templates/data_connector/declarations/declarations.d.ts +29 -0
- package/templates/data_connector/eslint.config.mjs +14 -0
- package/templates/data_connector/package.json +91 -0
- package/templates/data_connector/scripts/copy_env.ts +10 -0
- package/templates/data_connector/scripts/ssl/ssl.ts +131 -0
- package/templates/data_connector/scripts/start/app_runner.ts +201 -0
- package/templates/data_connector/scripts/start/context.ts +171 -0
- package/templates/data_connector/scripts/start/start.ts +46 -0
- package/templates/data_connector/scripts/start/tests/start.tests.ts +61 -0
- package/templates/data_connector/src/api/connect_client.ts +6 -0
- package/templates/data_connector/src/api/data_source.ts +96 -0
- package/templates/data_connector/src/api/data_sources/designs.tsx +253 -0
- package/templates/data_connector/src/api/data_sources/index.ts +4 -0
- package/templates/data_connector/src/api/data_sources/templates.tsx +287 -0
- package/templates/data_connector/src/api/fetch_data_table.ts +51 -0
- package/templates/data_connector/src/api/index.ts +4 -0
- package/templates/data_connector/src/api/oauth.ts +8 -0
- package/templates/data_connector/src/api/tests/data_source.test.tsx +99 -0
- package/templates/data_connector/src/app.tsx +24 -0
- package/templates/data_connector/src/components/app_error.tsx +15 -0
- package/templates/data_connector/src/components/footer.tsx +26 -0
- package/templates/data_connector/src/components/header.tsx +40 -0
- package/templates/data_connector/src/components/index.ts +3 -0
- package/templates/data_connector/src/components/inputs/messages.tsx +80 -0
- package/templates/data_connector/src/components/inputs/select_field.tsx +26 -0
- package/templates/data_connector/src/context/app_context.tsx +124 -0
- package/templates/data_connector/src/context/index.ts +2 -0
- package/templates/data_connector/src/context/use_app_context.ts +17 -0
- package/templates/data_connector/src/entrypoint.tsx +73 -0
- package/templates/data_connector/src/home.tsx +21 -0
- package/templates/data_connector/src/index.tsx +69 -0
- package/templates/data_connector/src/pages/data_source_config.tsx +9 -0
- package/templates/data_connector/src/pages/error.tsx +37 -0
- package/templates/data_connector/src/pages/index.ts +4 -0
- package/templates/data_connector/src/pages/login.tsx +145 -0
- package/templates/data_connector/src/pages/select_source.tsx +24 -0
- package/templates/data_connector/src/routes/index.ts +2 -0
- package/templates/data_connector/src/routes/protected_route.tsx +25 -0
- package/templates/data_connector/src/routes/routes.tsx +46 -0
- package/templates/data_connector/src/utils/data_params.ts +17 -0
- package/templates/data_connector/src/utils/data_table.ts +100 -0
- package/templates/data_connector/src/utils/fetch_result.ts +36 -0
- package/templates/data_connector/src/utils/index.ts +2 -0
- package/templates/data_connector/src/utils/tests/data_table.test.ts +133 -0
- package/templates/data_connector/styles/components.css +38 -0
- package/templates/data_connector/tsconfig.json +54 -0
- package/templates/data_connector/webpack.config.ts +270 -0
- package/templates/gen_ai/webpack.config.ts +12 -4
- package/templates/hello_world/webpack.config.ts +12 -4
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canva/cli",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.29",
|
|
4
4
|
"description": "The official Canva CLI.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
6
6
|
"author": "Canva Pty Ltd.",
|
|
7
|
+
"repository": {
|
|
8
|
+
"directory": "git+https://github.com/canva-sdks/canva-cli.git",
|
|
9
|
+
"type": "git"
|
|
10
|
+
},
|
|
7
11
|
"type": "module",
|
|
8
12
|
"main": "./lib/cjs/index.cjs",
|
|
9
13
|
"module": "./lib/esm/index.mjs",
|
|
@@ -26,7 +30,8 @@
|
|
|
26
30
|
"dependencies": {
|
|
27
31
|
"ink": "5.1.0",
|
|
28
32
|
"react": "18.3.1",
|
|
29
|
-
"@modelcontextprotocol/sdk": "1.8.0"
|
|
33
|
+
"@modelcontextprotocol/sdk": "1.8.0",
|
|
34
|
+
"react-docgen-typescript": "2.2.2"
|
|
30
35
|
},
|
|
31
36
|
"keywords": [
|
|
32
37
|
"apps sdk",
|
|
@@ -23,10 +23,13 @@ export function buildConfig({
|
|
|
23
23
|
devConfig,
|
|
24
24
|
appEntry = path.join(process.cwd(), "src", "index.tsx"),
|
|
25
25
|
backendHost = process.env.CANVA_BACKEND_HOST,
|
|
26
|
+
// For IN_HARNESS, refer to the following docs for more information: https://www.canva.dev/docs/apps/mcp-server/harness-setup/
|
|
27
|
+
inHarness = process.env.IN_HARNESS === "true",
|
|
26
28
|
}: {
|
|
27
29
|
devConfig?: DevConfig;
|
|
28
30
|
appEntry?: string;
|
|
29
31
|
backendHost?: string;
|
|
32
|
+
inHarness?: boolean;
|
|
30
33
|
} = {}): Configuration & DevServerConfiguration {
|
|
31
34
|
const mode = devConfig ? "development" : "production";
|
|
32
35
|
|
|
@@ -48,9 +51,14 @@ export function buildConfig({
|
|
|
48
51
|
return {
|
|
49
52
|
mode,
|
|
50
53
|
context: path.resolve(process.cwd(), "./"),
|
|
51
|
-
entry:
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
entry: inHarness
|
|
55
|
+
? {
|
|
56
|
+
harness: path.join(process.cwd(), "harness", "harness.tsx"),
|
|
57
|
+
init: path.join(process.cwd(), "harness", "init.ts"),
|
|
58
|
+
}
|
|
59
|
+
: {
|
|
60
|
+
app: appEntry,
|
|
61
|
+
},
|
|
54
62
|
target: "web",
|
|
55
63
|
resolve: {
|
|
56
64
|
alias: {
|
|
@@ -62,7 +70,7 @@ export function buildConfig({
|
|
|
62
70
|
extensions: [".ts", ".tsx", ".js", ".css", ".svg", ".woff", ".woff2"],
|
|
63
71
|
},
|
|
64
72
|
infrastructureLogging: {
|
|
65
|
-
level: "none",
|
|
73
|
+
level: inHarness ? "info" : "none",
|
|
66
74
|
},
|
|
67
75
|
module: {
|
|
68
76
|
rules: [
|
|
@@ -22,11 +22,14 @@ export const useConfig = (): Config<ContainerTypes> => {
|
|
|
22
22
|
description: "Label of filters for file type",
|
|
23
23
|
}),
|
|
24
24
|
key: "fileType",
|
|
25
|
+
// These options do not need to be translated as they are universal technical terms
|
|
26
|
+
/* eslint-disable formatjs/no-literal-string-in-object */
|
|
25
27
|
options: [
|
|
26
28
|
{ value: "mp4", label: "MP4" },
|
|
27
29
|
{ value: "png", label: "PNG" },
|
|
28
30
|
{ value: "jpeg", label: "JPEG" },
|
|
29
31
|
],
|
|
32
|
+
/* eslint-enable formatjs/no-literal-string-in-object */
|
|
30
33
|
allowCustomValue: true,
|
|
31
34
|
},
|
|
32
35
|
{
|
|
@@ -23,10 +23,13 @@ export function buildConfig({
|
|
|
23
23
|
devConfig,
|
|
24
24
|
appEntry = path.join(process.cwd(), "src", "index.tsx"),
|
|
25
25
|
backendHost = process.env.CANVA_BACKEND_HOST,
|
|
26
|
+
// For IN_HARNESS, refer to the following docs for more information: https://www.canva.dev/docs/apps/mcp-server/harness-setup/
|
|
27
|
+
inHarness = process.env.IN_HARNESS === "true",
|
|
26
28
|
}: {
|
|
27
29
|
devConfig?: DevConfig;
|
|
28
30
|
appEntry?: string;
|
|
29
31
|
backendHost?: string;
|
|
32
|
+
inHarness?: boolean;
|
|
30
33
|
} = {}): Configuration & DevServerConfiguration {
|
|
31
34
|
const mode = devConfig ? "development" : "production";
|
|
32
35
|
|
|
@@ -48,9 +51,14 @@ export function buildConfig({
|
|
|
48
51
|
return {
|
|
49
52
|
mode,
|
|
50
53
|
context: path.resolve(process.cwd(), "./"),
|
|
51
|
-
entry:
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
entry: inHarness
|
|
55
|
+
? {
|
|
56
|
+
harness: path.join(process.cwd(), "harness", "harness.tsx"),
|
|
57
|
+
init: path.join(process.cwd(), "harness", "init.ts"),
|
|
58
|
+
}
|
|
59
|
+
: {
|
|
60
|
+
app: appEntry,
|
|
61
|
+
},
|
|
54
62
|
target: "web",
|
|
55
63
|
resolve: {
|
|
56
64
|
alias: {
|
|
@@ -62,7 +70,7 @@ export function buildConfig({
|
|
|
62
70
|
extensions: [".ts", ".tsx", ".js", ".css", ".svg", ".woff", ".woff2"],
|
|
63
71
|
},
|
|
64
72
|
infrastructureLogging: {
|
|
65
|
-
level: "none",
|
|
73
|
+
level: inHarness ? "info" : "none",
|
|
66
74
|
},
|
|
67
75
|
module: {
|
|
68
76
|
rules: [
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Data Connector Template
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
This template provides a working example of a data connector app which uses the Canva Connect API as a data source. It demonstrates how to log in with OAuth and then fetch data from an external source, using Canva's API for listing Designs and Brand Templates. There are instructions below for how to configure your authentication settings to try it out.
|
|
6
|
+
|
|
7
|
+
The template provides a list / detail interface for choosing a data source. The user would first select the data source they want to import from a list. Next, they configure the data source to select the desired query by applying search filters or sort criteria. The app turns their configured data source into an API request and then returns the data in a table format for use in a Canva Design.
|
|
8
|
+
|
|
9
|
+
## Best Practices
|
|
10
|
+
|
|
11
|
+
This template captures best practices for improving user experience in your application.
|
|
12
|
+
|
|
13
|
+
### State Management
|
|
14
|
+
|
|
15
|
+
In this template, we've set up state management using `React Context`. It's just one way to do it, not a strict rule. If your app gets more complicated, you might want to check out other options like `Redux` or `MobX`.
|
|
16
|
+
|
|
17
|
+
### Routing
|
|
18
|
+
|
|
19
|
+
As your application evolves, you may find the need for routing to manage multiple views or pages. In this template, we've integrated React Router to illustrate how routing can facilitate seamless navigation between various components.
|
|
20
|
+
|
|
21
|
+
### App UI Kit
|
|
22
|
+
|
|
23
|
+
The App UI Kit is a React-based component library designed for creating apps that emulate Canva's look and feel. We strongly recommend using the App UI Kit if you're planning to release an app to the public, because this makes it easier to comply with our [design guidelines](https://www.canva.dev/docs/apps/design-guidelines/).
|
|
24
|
+
|
|
25
|
+
### Data Sources
|
|
26
|
+
|
|
27
|
+
A data source is a type of data that this connector can retrieve. It has:
|
|
28
|
+
|
|
29
|
+
- properties that define the configuration for this source - e.g. search criteria, ordering conditions, selection filters
|
|
30
|
+
- UI for how to edit this configuration
|
|
31
|
+
- columns to display for each data item
|
|
32
|
+
- logic for how to fetch the data from an external API
|
|
33
|
+
|
|
34
|
+
The initial data sources for the Canva Connect API (Designs and Brand Templates) in this template are located in `src/api/data_sources/`.
|
|
35
|
+
|
|
36
|
+
This template provides a `DataSourceHandler` class in `src/api/data_source.ts` as a suggested starting point for defining a reusable concept for a data source and convert API responses into data table outputs.
|
|
37
|
+
|
|
38
|
+
## Setup - Authentication
|
|
39
|
+
|
|
40
|
+
This template is a working app that reads the [Canva Connect API](https://www.canva.dev/docs/connect/).
|
|
41
|
+
|
|
42
|
+
To run it and authenticate with the Canva Connect API via OAuth you must first complete some authentication setup steps.
|
|
43
|
+
|
|
44
|
+
### 0. Set up an App
|
|
45
|
+
|
|
46
|
+
- If not already handled by the Canva CLI, you need to create an app.
|
|
47
|
+
Go to [Your Apps](https://www.canva.com/developers/apps) and create an app.
|
|
48
|
+
- On the **Configuration** page, enable the `Data Connector` intent.
|
|
49
|
+
|
|
50
|
+
### 1. Set up a Connect API Integration
|
|
51
|
+
|
|
52
|
+
- Go to [Your Integrations](https://www.canva.com/developers/integrations/connect-api) and create a new integration
|
|
53
|
+
- On the **Configuration** page, generate a client secret - you need the client ID and client secret in the app setup.
|
|
54
|
+
- On the **Scopes** page, add the `designs:meta` and `brandtemplate:meta` read scopes
|
|
55
|
+
- On the **Authentication** page, set `https://www.canva.com/apps/oauth/authorized` as an Authorized redirect
|
|
56
|
+
|
|
57
|
+
### 2. Set up OAuth for the app
|
|
58
|
+
|
|
59
|
+
- Go to [Your Apps](https://www.canva.com/developers/apps) and open your data connector app.
|
|
60
|
+
- On the **Authentication** page, add an OAuth 2.0 provider with the following settings:
|
|
61
|
+
> Provider: `CanvaConnect`
|
|
62
|
+
>
|
|
63
|
+
> Client ID: `(generated in step 1)
|
|
64
|
+
>
|
|
65
|
+
> Client secret: `(generated in step 1)
|
|
66
|
+
>
|
|
67
|
+
> Credential transfer mode: `Headers (default)`
|
|
68
|
+
>
|
|
69
|
+
> Authorization server URL: `https://www.canva.com/api/oauth/authorize`
|
|
70
|
+
>
|
|
71
|
+
> Token exchange URL: `https://api.canva.com/rest/v1/oauth/token`
|
|
72
|
+
>
|
|
73
|
+
> Proof Key for Code Exchange (PKCE): `Enabled`
|
|
74
|
+
|
|
75
|
+
### 3. Run your app
|
|
76
|
+
|
|
77
|
+
- Run `npm start` to run the app.
|
|
78
|
+
- You should be able to log in and then choose from the available data sources.
|
|
79
|
+
|
|
80
|
+
### To use another OAuth source
|
|
81
|
+
|
|
82
|
+
- `src/api/oauth.ts` has a `scope` property that will be used in the oauth flow.
|
|
83
|
+
- This template starts with the scope for the Canva Connect API login set to `["design:meta:read", "brandtemplate:meta:read"]`
|
|
84
|
+
- It should match the scopes set in Step 1.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare module "*.css" {
|
|
2
|
+
const styles: { [className: string]: string };
|
|
3
|
+
export = styles;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
declare module "*.jpg" {
|
|
7
|
+
const content: string;
|
|
8
|
+
export default content;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare module "*.jpeg" {
|
|
12
|
+
const content: string;
|
|
13
|
+
export default content;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare module "*.png" {
|
|
17
|
+
const content: string;
|
|
18
|
+
export default content;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare module "*.svg" {
|
|
22
|
+
const content: React.FunctionComponent<{
|
|
23
|
+
size?: "tiny" | "small" | "medium" | "large";
|
|
24
|
+
className?: string;
|
|
25
|
+
}>;
|
|
26
|
+
export default content;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare const BACKEND_HOST: string;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"private": true,
|
|
3
|
+
"name": "data_connector",
|
|
4
|
+
"description": "An example Canva Data Connector App",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"extract": "formatjs extract \"src/**/*.{ts,tsx}\" --out-file dist/messages_en.json",
|
|
7
|
+
"build": "webpack --config webpack.config.ts --mode production && npm run extract",
|
|
8
|
+
"format": "prettier '**/*.{css,ts,tsx}' --no-config --write",
|
|
9
|
+
"format:check": "prettier '**/*.{css,ts,tsx}' --no-config --check --ignore-path",
|
|
10
|
+
"format:file": "prettier $1 --no-config --write",
|
|
11
|
+
"lint": "eslint .",
|
|
12
|
+
"lint:fix": "eslint . --fix",
|
|
13
|
+
"lint:types": "tsc",
|
|
14
|
+
"start": "ts-node ./scripts/start/start.ts",
|
|
15
|
+
"start:preview": "npm run start -- --preview",
|
|
16
|
+
"test": "jest --no-cache",
|
|
17
|
+
"test:watch": "jest --watchAll",
|
|
18
|
+
"test:update": "npm run test -- -u",
|
|
19
|
+
"postinstall": "ts-node ./scripts/copy_env.ts"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@canva/app-i18n-kit": "^1.0.2",
|
|
23
|
+
"@canva/app-ui-kit": "^4.9.0",
|
|
24
|
+
"@canva/asset": "^2.2.0",
|
|
25
|
+
"@canva/design": "^2.4.1",
|
|
26
|
+
"@canva/error": "^2.1.0",
|
|
27
|
+
"@canva/intents": "^0.0.0-beta.2",
|
|
28
|
+
"@canva/platform": "^2.1.0",
|
|
29
|
+
"@canva/user": "^2.1.0",
|
|
30
|
+
"react": "18.3.1",
|
|
31
|
+
"react-dom": "18.3.1",
|
|
32
|
+
"react-error-boundary": "4.1.2",
|
|
33
|
+
"react-intl": "6.8.7",
|
|
34
|
+
"react-router-dom": "6.28.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@canva/app-eslint-plugin": "^1.0.0-beta.3",
|
|
38
|
+
"@canva/cli": ">= 0.0.1-beta.13 < 0.0.2",
|
|
39
|
+
"@formatjs/cli": "6.3.15",
|
|
40
|
+
"@formatjs/ts-transformer": "3.13.27",
|
|
41
|
+
"@ngrok/ngrok": "1.4.1",
|
|
42
|
+
"@pmmmwh/react-refresh-webpack-plugin": "0.5.15",
|
|
43
|
+
"@svgr/webpack": "8.1.0",
|
|
44
|
+
"@testing-library/react": "16.1.0",
|
|
45
|
+
"@types/express": "4.17.21",
|
|
46
|
+
"@types/express-serve-static-core": "4.19.6",
|
|
47
|
+
"@types/jest": "29.5.14",
|
|
48
|
+
"@types/jsonwebtoken": "9.0.7",
|
|
49
|
+
"@types/node": "20.10.0",
|
|
50
|
+
"@types/node-fetch": "2.6.12",
|
|
51
|
+
"@types/node-forge": "1.3.11",
|
|
52
|
+
"@types/nodemon": "1.19.6",
|
|
53
|
+
"@types/react": "18.3.12",
|
|
54
|
+
"@types/react-dom": "18.3.1",
|
|
55
|
+
"@types/webpack-env": "1.18.5",
|
|
56
|
+
"chalk": "4.1.2",
|
|
57
|
+
"cli-table3": "0.6.5",
|
|
58
|
+
"css-loader": "7.1.2",
|
|
59
|
+
"css-modules-typescript-loader": "4.0.1",
|
|
60
|
+
"cssnano": "7.0.6",
|
|
61
|
+
"debug": "4.4.0",
|
|
62
|
+
"dotenv": "16.4.7",
|
|
63
|
+
"express": "4.21.2",
|
|
64
|
+
"express-basic-auth": "1.2.1",
|
|
65
|
+
"jest": "29.7.0",
|
|
66
|
+
"jest-css-modules-transform": "4.4.2",
|
|
67
|
+
"jest-environment-jsdom": "29.7.0",
|
|
68
|
+
"jsonwebtoken": "9.0.2",
|
|
69
|
+
"jwks-rsa": "3.1.0",
|
|
70
|
+
"mini-css-extract-plugin": "2.9.2",
|
|
71
|
+
"node-fetch": "3.3.2",
|
|
72
|
+
"node-forge": "1.3.1",
|
|
73
|
+
"nodemon": "3.0.1",
|
|
74
|
+
"open": "8.4.2",
|
|
75
|
+
"postcss-loader": "8.1.1",
|
|
76
|
+
"prettier": "3.4.2",
|
|
77
|
+
"react-refresh": "0.16.0",
|
|
78
|
+
"style-loader": "4.0.0",
|
|
79
|
+
"terser-webpack-plugin": "5.3.11",
|
|
80
|
+
"tree-kill": "1.2.2",
|
|
81
|
+
"ts-jest": "29.2.5",
|
|
82
|
+
"ts-loader": "9.5.2",
|
|
83
|
+
"ts-node": "10.9.2",
|
|
84
|
+
"typescript": "5.5.4",
|
|
85
|
+
"url-loader": "4.1.1",
|
|
86
|
+
"webpack": "5.97.1",
|
|
87
|
+
"webpack-cli": "5.1.4",
|
|
88
|
+
"webpack-dev-server": "5.2.0",
|
|
89
|
+
"yargs": "17.7.2"
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
|
|
5
|
+
const envPath = path.resolve(__dirname, "..", ".env");
|
|
6
|
+
const templatePath = path.resolve(__dirname, "..", ".env.template");
|
|
7
|
+
|
|
8
|
+
if (!fs.existsSync(envPath)) {
|
|
9
|
+
fs.copyFileSync(templatePath, envPath);
|
|
10
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import * as crypto from "crypto";
|
|
2
|
+
import { pki } from "node-forge";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import * as fs from "fs/promises";
|
|
5
|
+
|
|
6
|
+
const SSL_CERT_DIR = path.resolve(process.cwd(), "..", "..", ".ssl");
|
|
7
|
+
const CERT_FILE = path.resolve(SSL_CERT_DIR, "certificate.pem");
|
|
8
|
+
const KEY_FILE = path.resolve(SSL_CERT_DIR, "private-key.pem");
|
|
9
|
+
|
|
10
|
+
export interface Certificate {
|
|
11
|
+
keyFile: string;
|
|
12
|
+
certFile: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const CERT_ATTRS: { name: string; value: string }[] = [
|
|
16
|
+
{
|
|
17
|
+
name: "commonName",
|
|
18
|
+
value: "localhost",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: "countryName",
|
|
22
|
+
value: "AU",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "stateOrProvinceName",
|
|
26
|
+
value: "New South Wales",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: "localityName",
|
|
30
|
+
value: "Sydney",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "organizationName",
|
|
34
|
+
value: "Test",
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "organizationalUnitName",
|
|
38
|
+
value: "Test",
|
|
39
|
+
},
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
const generateRsaKeys = async (): Promise<{
|
|
43
|
+
publicKey: string;
|
|
44
|
+
privateKey: string;
|
|
45
|
+
}> =>
|
|
46
|
+
new Promise((resolve, reject) => {
|
|
47
|
+
crypto.generateKeyPair(
|
|
48
|
+
"rsa",
|
|
49
|
+
{
|
|
50
|
+
modulusLength: 2096,
|
|
51
|
+
publicKeyEncoding: {
|
|
52
|
+
type: "spki",
|
|
53
|
+
format: "pem",
|
|
54
|
+
},
|
|
55
|
+
privateKeyEncoding: {
|
|
56
|
+
type: "pkcs8",
|
|
57
|
+
format: "pem",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
(err, publicKey, privateKey) => {
|
|
61
|
+
if (err) {
|
|
62
|
+
reject(err);
|
|
63
|
+
} else {
|
|
64
|
+
resolve({ publicKey, privateKey });
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const generateCertificate = (opts: {
|
|
71
|
+
privateKey: string;
|
|
72
|
+
publicKey: string;
|
|
73
|
+
}): string => {
|
|
74
|
+
const privateKey = pki.privateKeyFromPem(opts.privateKey);
|
|
75
|
+
const publicKey = pki.publicKeyFromPem(opts.publicKey);
|
|
76
|
+
|
|
77
|
+
const cert = pki.createCertificate();
|
|
78
|
+
|
|
79
|
+
cert.publicKey = publicKey;
|
|
80
|
+
cert.serialNumber = "01";
|
|
81
|
+
cert.validity.notBefore = new Date();
|
|
82
|
+
cert.validity.notAfter = new Date();
|
|
83
|
+
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
|
|
84
|
+
|
|
85
|
+
cert.setSubject(CERT_ATTRS);
|
|
86
|
+
cert.setIssuer(CERT_ATTRS);
|
|
87
|
+
|
|
88
|
+
// the actual certificate signing
|
|
89
|
+
cert.sign(privateKey);
|
|
90
|
+
|
|
91
|
+
// now convert the Forge certificate to PEM format
|
|
92
|
+
return pki.certificateToPem(cert);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const writeCertFiles = async (opts: {
|
|
96
|
+
cert: string;
|
|
97
|
+
privateKey: string;
|
|
98
|
+
}): Promise<void> => {
|
|
99
|
+
const { cert, privateKey } = opts;
|
|
100
|
+
|
|
101
|
+
await fs.mkdir(SSL_CERT_DIR, { recursive: true });
|
|
102
|
+
await Promise.all([
|
|
103
|
+
fs.writeFile(CERT_FILE, cert, { encoding: "utf8" }),
|
|
104
|
+
fs.writeFile(KEY_FILE, privateKey, { encoding: "utf8" }),
|
|
105
|
+
]);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const cerfFilesExist = async (): Promise<boolean> => {
|
|
109
|
+
try {
|
|
110
|
+
await Promise.all([
|
|
111
|
+
fs.access(CERT_FILE, fs.constants.R_OK | fs.constants.W_OK),
|
|
112
|
+
fs.access(KEY_FILE, fs.constants.R_OK | fs.constants.W_OK),
|
|
113
|
+
]);
|
|
114
|
+
return true;
|
|
115
|
+
} catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const createOrRetrieveCertificate = async (): Promise<Certificate> => {
|
|
121
|
+
if (!(await cerfFilesExist())) {
|
|
122
|
+
const keys = await generateRsaKeys();
|
|
123
|
+
const cert = generateCertificate(keys);
|
|
124
|
+
await writeCertFiles({ cert, privateKey: keys.privateKey });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
certFile: CERT_FILE,
|
|
129
|
+
keyFile: KEY_FILE,
|
|
130
|
+
};
|
|
131
|
+
};
|