@universis/signer-worker 1.1.0 → 1.6.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/README.md CHANGED
@@ -8,7 +8,7 @@ Install `@universis/signer-worker`
8
8
 
9
9
  npm i @universis/signer-worker
10
10
 
11
- Important note: Verify that [Universis Signer](https://gitlab.com/universis/universis-signer) is already installed as aservice. Follow [installation instructions](https://gitlab.com/universis/universis-signer#installation) to install and configure Universis Signer.
11
+ Important note: Verify that [Universis Signer](https://gitlab.com/universis/universis-signer) is already installed as a service. Follow [installation instructions](https://gitlab.com/universis/universis-signer#installation) to install and configure Universis Signer.
12
12
 
13
13
  ## Configuration
14
14
 
@@ -16,10 +16,129 @@ Configure [Universis api server](https://gitlab.com/universis/universis-api) to
16
16
 
17
17
  {
18
18
  "services": [
19
- ...,
20
19
  {
21
20
  "serviceType": "@universis/signer-worker#SignerWorker"
22
21
  }
23
22
  ]
24
- ...
25
23
  }
24
+
25
+ or configure [Universis api server](https://gitlab.com/universis/universis-api) to use `SignerService` service.
26
+
27
+ {
28
+ "services": [
29
+ {
30
+ "serviceType": "@universis/signer-worker#SignerService"
31
+ }
32
+ ]
33
+ }
34
+
35
+ ## Sign documents
36
+
37
+ > `SignerService.signFile(context: DataContext, file: string, contentType?: string, options?: { timestampServer?: string, position?: string, reason?: string })`
38
+
39
+ `SignerService.signFile` can be used for signing *.pdf files using a server instance of [Universis Signer](https://gitlab.com/universis/universis-signer#use-docker-image)
40
+
41
+ For completing sign process, the signer service instance should be configured by adding `settings/universis/signerService` section at application configuration
42
+
43
+ ```json
44
+
45
+ {
46
+ "settings": {
47
+ "universis": {
48
+ "signerService": {
49
+ "user": "user",
50
+ "password": "password",
51
+ "host": "http://localhost:2465"
52
+ }
53
+ }
54
+ }
55
+ }
56
+
57
+ ```
58
+
59
+ where `host` is the URL of the Universis Signer service, `user` and `password` are the credentials for the Universis Signer service.
60
+
61
+ Normally, the Universis Signer service which is running on `http://localhost:2465`, uses a PKCS12 store with secret key and certificate for signing.
62
+ In this case, the `user` and `password` are used to access the PKCS12 store. `SignerService` service will try to get valid certificates,
63
+ and select the first one that is valid for signing. Use the configuration setting `thumbnail` if you want to explicitly define
64
+ the certificate that should be used for signing.
65
+
66
+ ```json
67
+ {
68
+ "settings": {
69
+ "universis": {
70
+ "signerService": {
71
+ "user": "user",
72
+ "password": "password",
73
+ "host": "http://localhost:2465",
74
+ "thumbnail": "2d49be3b03db5d176482640549d5aaede84c925d"
75
+ }
76
+ }
77
+ }
78
+ }
79
+ ```
80
+
81
+ ## Sign data
82
+
83
+ > `SignerService.sign(context: DataContext, data: unknown, options?: { timestampServer?: string, reason?: string })`
84
+
85
+ `SignerService.sign` method can be used for signing data.
86
+ The data can be any JSON-serializable object, and the method will return a signed version of the data.
87
+
88
+ The `options` parameter can include a `timestampServer` URL for timestamping the signature, a `position` string to specify the position of the signature in the document,
89
+ and a `reason` string to specify the reason for signing.
90
+
91
+ ### Example
92
+
93
+ ```typescript
94
+ import {SignerService} from '@universis/signer-worker';
95
+
96
+ function signJson(context: DataContext, data: unknown) {
97
+ const signerService = context.getApplication().getService(SignerService);
98
+ return signerService.sign(context, data, {
99
+ timestampServer: "http://timestamp.server.com",
100
+ reason: "Signing example data"
101
+ });
102
+ }
103
+
104
+ signJson(context, { message: "Hello, World!" })
105
+ .then(signedData => {
106
+ console.log("Signed data:", signedData);
107
+ })
108
+ .catch(error => {
109
+ console.error("Error signing data:", error);
110
+ });
111
+ ```
112
+
113
+ ## Remote Signer Worker
114
+
115
+ To use the remote signer worker, you need to configure the Universis API server to use the `RemoteSignerWorker` service. This service will communicate with the Universis Signer service running on a remote server. Register `@universis/signer-worker#RemoteSignerWorker` under universis api server services section of application configuration:
116
+
117
+ ```json
118
+ {
119
+ "services": [
120
+ {
121
+ "serviceType": "@universis/signer-worker#RemoteSignerWorker"
122
+ }
123
+ ]
124
+ }
125
+ ```
126
+ This will allow the Universis API server to use the remote signer worker for signing operations. The remote signer worker will handle the communication with the Universis Signer service and perform the signing operations as needed.
127
+
128
+ The remote signer service host should be configured in the application settings:
129
+
130
+ ```json
131
+ {
132
+ "settings": {
133
+ "universis": {
134
+ "remoteSignerWorker": {
135
+ "host": "http://remote-signer-server:8480"
136
+ }
137
+ }
138
+ }
139
+ }
140
+ ```
141
+
142
+ Where `host` is the URL of the remote Universis Signer service.
143
+
144
+ Remote signer worker will expose the same methods under `/services/remoteSigner` as the local signer worker, allowing you to sign files and data in the same way as described above.
@@ -0,0 +1,7 @@
1
+ import { ApplicationBase } from '@themost/common';
2
+ import { SignerWorker } from './SignerWorker';
3
+ declare class RemoteSignerWorker extends SignerWorker {
4
+ constructor(app: ApplicationBase);
5
+ install(): void;
6
+ }
7
+ export { RemoteSignerWorker };
@@ -0,0 +1,72 @@
1
+ /// <reference types="node" />
2
+ import { ApplicationBase, ApplicationService } from '@themost/common';
3
+ import { ExpressDataContext } from '@themost/express';
4
+ import { Certificate, JsonWebKey, VerifySignatureResult, SignJsonSignatureResult, SignerServiceImpl } from './SignerServiceImpl';
5
+ import { TOTPOptions } from 'xotp';
6
+ export declare class SignerService extends ApplicationService implements SignerServiceImpl {
7
+ protected options?: {
8
+ host?: string;
9
+ user?: string;
10
+ password?: string;
11
+ secret?: string;
12
+ thumbprint?: string;
13
+ inspector?: {
14
+ text?: string[];
15
+ textPosition: string;
16
+ width?: number;
17
+ height?: number;
18
+ };
19
+ };
20
+ constructor(app: ApplicationBase);
21
+ /**
22
+ * @param {ExpressDataContext} context The underlying data context
23
+ * @param {*} data The data to be verified
24
+ * @param {{filename: string, contentType: string}=} options Extra attributes (optional)
25
+ * @returns {Promise<VerifySignatureResult[]>}
26
+ */
27
+ verify(context: ExpressDataContext, data: unknown, options?: {
28
+ filename: string;
29
+ contentType: string;
30
+ }): Promise<VerifySignatureResult[]>;
31
+ getJwks(): Promise<JsonWebKey[]>;
32
+ getCerts(): Promise<Certificate[]>;
33
+ /**
34
+ * Generates a one-time password using the configured secret.
35
+ */
36
+ generateVerificationCode(options?: TOTPOptions): string;
37
+ /**
38
+ * @param {ExpressDataContext} context The underlying data context
39
+ * @param {*} data Any JSON object to be signed
40
+ * @param {{timestampServer?:string}} extraOptions
41
+ * @returns {Promise<SignJsonSignatureResult>} An array of signatures
42
+ */
43
+ sign(context: ExpressDataContext, data: unknown, extraOptions?: {
44
+ timestampServer?: string;
45
+ reason?: string;
46
+ }): Promise<SignJsonSignatureResult>;
47
+ signFile(context: ExpressDataContext, file: string, contentType?: string, extraOptions?: {
48
+ page?: number;
49
+ position?: number[];
50
+ timestampServer?: string;
51
+ reason?: string;
52
+ }): Promise<Buffer>;
53
+ verifyFile(context: ExpressDataContext, file: string, options?: {
54
+ filename: string;
55
+ contentType: string;
56
+ }): Promise<VerifySignatureResult[]>;
57
+ inspect(context: ExpressDataContext, file: string, options?: {
58
+ filename: string;
59
+ contentType: string;
60
+ }): Promise<{
61
+ page: number;
62
+ position: number[];
63
+ text: string;
64
+ }>;
65
+ requiresVerificationCode(): boolean;
66
+ protected signFinal<T>(context: ExpressDataContext, file: string, contentType?: string, extraOptions?: {
67
+ position?: number[];
68
+ page?: number;
69
+ timestampServer?: string;
70
+ reason?: string;
71
+ }): Promise<T>;
72
+ }
@@ -0,0 +1,61 @@
1
+ /// <reference types="node" />
2
+ import { ExpressDataContext } from '@themost/express';
3
+ export declare interface Certificate {
4
+ version: number;
5
+ subjectDN: string;
6
+ sigAlgName?: string;
7
+ sigAlgOID?: string;
8
+ issuerDN: string;
9
+ serialNumber: any;
10
+ notAfter: string;
11
+ notBefore: string;
12
+ expired: boolean;
13
+ thumbprint: string;
14
+ commonName: string;
15
+ }
16
+ export declare interface JsonWebKey {
17
+ kty: string;
18
+ use: string;
19
+ kid: string;
20
+ alg: string;
21
+ n: string;
22
+ e: string;
23
+ }
24
+ export declare interface VerifySignatureResult {
25
+ valid: boolean;
26
+ certificates: Certificate[];
27
+ signatureProperties?: {
28
+ signingDate: string;
29
+ reason: string;
30
+ signingCertificate: Certificate;
31
+ };
32
+ }
33
+ export declare interface SignJsonSignatureResult {
34
+ payload: string;
35
+ signatures: {
36
+ protected: string;
37
+ signature: string;
38
+ }[];
39
+ }
40
+ export declare interface SignerServiceImpl {
41
+ getCerts(): Promise<Certificate[]>;
42
+ getJwks(): Promise<JsonWebKey[]>;
43
+ verify(context: ExpressDataContext, document: string, options?: {
44
+ filename: string;
45
+ contentType: string;
46
+ }): Promise<VerifySignatureResult[]>;
47
+ verifyFile(context: ExpressDataContext, file: string, options?: {
48
+ filename: string;
49
+ contentType: string;
50
+ }): Promise<VerifySignatureResult[]>;
51
+ signFile(context: ExpressDataContext, file: string, contentType?: string, extraOptions?: {
52
+ page?: number;
53
+ position?: number[];
54
+ timestampServer?: string;
55
+ reason?: string;
56
+ }): Promise<Buffer>;
57
+ sign(context: ExpressDataContext, data: unknown, extraOptions?: {
58
+ timestampServer?: string;
59
+ reason?: string;
60
+ }): Promise<SignJsonSignatureResult>;
61
+ }
@@ -1,39 +1,26 @@
1
1
  import { ApplicationBase, ApplicationService } from '@themost/common';
2
+ import { BehaviorSubject } from 'rxjs';
3
+ import { Application } from 'express';
2
4
  import { ExpressDataContext } from '@themost/express';
3
- declare interface Certificate {
4
- version: number;
5
- subjectDN: string;
6
- sigAlgName?: string;
7
- sigAlgOID?: string;
8
- issuerDN: string;
9
- serialNumber: any;
10
- notAfter: string;
11
- notBefore: string;
12
- expired: boolean;
13
- thumbprint: string;
14
- commonName: string;
15
- }
16
- declare interface VerifySignatureResult {
17
- valid: boolean;
18
- certificates: Certificate[];
19
- signatureProperties?: {
20
- signingDate: string;
21
- reason: string;
22
- signingCertificate: Certificate;
23
- };
5
+ import { VerifySignatureResult } from './SignerServiceImpl';
6
+ export declare interface ApplicationBaseWithContainer extends ApplicationBase {
7
+ container?: BehaviorSubject<Application>;
24
8
  }
25
9
  export declare class SignerWorker extends ApplicationService {
10
+ protected options?: {
11
+ host?: string;
12
+ };
26
13
  constructor(app: ApplicationBase);
14
+ install(): void;
27
15
  /**
28
16
  *
29
17
  * @param {ExpressDataContext} context The underlying data context
30
18
  * @param {string} document The file path
31
19
  * @param {{filename: string, contentType: string}=} options Extra attributes (optional)
32
- * @returns
20
+ * @returns Promise<VerifySignatureResult[]>
33
21
  */
34
22
  verify(context: ExpressDataContext, document: string, options?: {
35
23
  filename: string;
36
24
  contentType: string;
37
25
  }): Promise<VerifySignatureResult[]>;
38
26
  }
39
- export {};
package/dist/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
1
  export * from './SignerWorker';
2
+ export * from './SignerServiceImpl';
2
3
  export * from './signerWorkerRouter';
4
+ export * from './SignerService';
5
+ export * from './RemoteSignerWorker';
6
+ export * from './remoteSignerWorkerRouter';