-
Notifications
You must be signed in to change notification settings - Fork 6
/
example.ts
139 lines (120 loc) · 4.78 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* eslint-disable no-console */
/* eslint-disable max-len */
/*
* *
* * Copyright 2022 eBay Inc.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
* *
*/
'use strict';
import bodyParser from 'body-parser';
import { constants } from '../src/constants';
import express, { Request, Response } from 'express';
import * as DigitalSignatureSDK from '../src/index';
import { needsContentDigestValidation } from '../src/helpers/common';
const config: DigitalSignatureSDK.Config = require('./example-config.json');
const configFull: DigitalSignatureSDK.Config = require('./example-config-full.json');
const app = express();
const PORT = process.env.PORT || 8080;
const options = {
inflate: true,
limit: '100kb',
type: 'application/*'
}
app.use(bodyParser.raw(options));
/**
* This endpoint uses `example-config.json` with `signMessage()` to sign the incoming request.
*/
app.post('/sign-request', async (req: Request, res: Response) => {
try {
await DigitalSignatureSDK.signMessage(req, res, config);
res.status(constants.HTTP_STATUS_CODE.OK).send();
} catch (ex) {
console.error(ex);
res.status(constants.HTTP_STATUS_CODE.INTERNAL_SERVER_ERROR).send();
};
});
/**
* This endpoint uses `example-config-full.json` and individual methods to generate signature.
*/
app.post('/sign', async (req: Request, res: Response) => {
try {
const generatedHeaders: any = {};
const payloadBuffer: Buffer = Buffer.from(req.body);
if (needsContentDigestValidation(req.body)) {
const contentDigest = DigitalSignatureSDK.generateDigestHeader(
payloadBuffer,
configFull.digestAlgorithm
);
generatedHeaders[constants.HEADERS.CONTENT_DIGEST] = contentDigest
}
const signatureInput = DigitalSignatureSDK.generateSignatureInput(generatedHeaders, configFull);
generatedHeaders[constants.HEADERS.SIGNATURE_INPUT] = signatureInput
const signatureKey = await DigitalSignatureSDK.generateSignatureKey(configFull);
generatedHeaders[constants.HEADERS.SIGNATURE_KEY] = signatureKey
const signature = DigitalSignatureSDK.generateSignature(generatedHeaders, configFull);
generatedHeaders[constants.HEADERS.SIGNATURE] = signature
res.status(constants.HTTP_STATUS_CODE.OK).send(generatedHeaders);
} catch (ex) {
console.error(ex);
res.status(constants.HTTP_STATUS_CODE.INTERNAL_SERVER_ERROR).send();
};
});
/**
* This endpoint uses `example-config-full.json` with `validateSignature()` to validate the signature.
*/
app.post('/validate-request', async (req: Request, res: Response) => {
try {
let response: boolean = await DigitalSignatureSDK.validateSignature(req, configFull);
if (true === response) {
res.status(constants.HTTP_STATUS_CODE.OK).send();
} else {
console.error(`Signature verification failure`);
res.status(constants.HTTP_STATUS_CODE.BAD_REQUEST).send();
}
} catch (ex) {
console.error(ex);
res.status(constants.HTTP_STATUS_CODE.INTERNAL_SERVER_ERROR).send();
};
});
/**
* This endpoint uses `example-config-full.json` and individual methods to validate the signature.
*/
app.post('/validate', async (req: Request, res: Response) => {
try {
// Validate digest header, if needed.
if (needsContentDigestValidation(req.method)) {
const header = req.headers[constants.HEADERS.CONTENT_DIGEST] as string;
DigitalSignatureSDK.validateDigestHeader(header, req.body);
}
// Validate signature header
let response: boolean = await DigitalSignatureSDK.validateSignatureHeader(
req.headers,
configFull
);
if (true === response) {
res.status(constants.HTTP_STATUS_CODE.OK).send();
} else {
console.error(`Signature verification failure`);
res.status(constants.HTTP_STATUS_CODE.BAD_REQUEST).send();
}
} catch (ex) {
console.error(ex);
res.status(constants.HTTP_STATUS_CODE.INTERNAL_SERVER_ERROR).send();
};
});
app.listen(PORT, () => {
// eslint-disable-next-line no-console
console.log(`Listening at http://localhost:${PORT}`);
});