Intestazione firma
Il seguente codice di esempio crea un valore di intestazione della firma HTTP. Il codice implementa la specifica IETF draft-cavage-http-signatures-09 , che viene utilizzata per convalidare il codice.
Come utilizzare l'intestazione della firma
Per utilizzare correttamente l'intestazione della firma, completare la seguente procedura per registrare una chiave pubblica e generare un'intestazione della firma per inoltrare i propri dati.
Registrare una chiave pubblica:
- Generare una coppia di chiavi rsa-sha256 o ecdsa-sha256 .
- Invia il documento public signing key XML all'API del connettore per registrare la chiave pubblica.
Generare l'intestazione della firma per inoltrare i dati con l'API del connettore:
- Utilizzare la chiave privata per creare un'intestazione di firma. Consultare l'esempio di codice nella seguente sezione.
- Creare le intestazioni per la chiamata API del connettore.
- Inoltra i dati con Connector API.
Esempio di codice
const crypto = require('crypto');
///// constants taken from IETF draft-cavage-http-signatures-09 /////
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
<public key value>
-----END PUBLIC KEY-----`;
const PRIVATE_KEY = -----BEGIN RSA PRIVATE KEY----- <private key value> -----END RSA PRIVATE KEY-----; # pragma: whitelist secret
const ALGORITHM = 'rsa-sha256';
const METHOD = 'POST';
const PATH = '/foo?param=value&pet=dog';
const HEADERS = {
Host: 'example.com',
Date: 'Sun, 05 Jan 2014 21:31:40 GMT',
'Content-Type': 'application/json',
Digest: '<hash value>',
'Content-Length': 18,
};
const PAYLOAD = '{"hello": "world"}';
const KEY_ID = 'Test';
const EXPECTED_BASIC_TEST_SIGNATURE = '<hash value>'
const EXPECTED_ALL_HEADERS_TEST_SIGNATURE = '<hash value>'
///// signature methods /////
function get_signing_string(method: string, path: string, headers: string[]) {
let signing_string = '(request-target): ' + method.toLowerCase() + ' ' + path;
for (const key in headers) {
const value = headers[key];
signing_string += '\n' + key.toLowerCase() + ': ' + String(value);
}
return signing_string;
}
function get_signature(signing_string: string, private_key: string) {
const signer = crypto.createSign('RSA-SHA256');
signer.update(signing_string);
return signer.sign(private_key, 'base64');
}
function get_signature_header_value(key_id: string, algorithm: string, headers: string[], signature: string) {
let header_names = '(request-target)';
for (const name in headers) {
header_names += ' ' + name.toLowerCase();
}
let header_value = '';
header_value += 'keyId="' + key_id + '",';
header_value += 'algorithm="' + algorithm + '",';
header_value += 'headers="' + header_names + '",';
header_value += 'signature="' + signature + '"';
return header_value;
}
///// test methods /////
// uses the (request-target), host, and date headers
function basic_test() {
const headers = {};
headers['Host'] = HEADERS['Host'];
headers['Date'] = HEADERS['Date'];
run_test('BASIC TEST', headers, EXPECTED_BASIC_TEST_SIGNATURE);
}
// uses all headers
function all_headers_test() {
const headers = HEADERS;
run_test('ALL HEADERS TEST', headers, EXPECTED_ALL_HEADERS_TEST_SIGNATURE);
}
function run_test(name, headers, expected_signature) {
const signing_string = get_signing_string(METHOD, PATH, headers);
const signature_base64 = get_signature(signing_string, PRIVATE_KEY);
const signature_header_value = get_signature_header_value(KEY_ID, ALGORITHM, headers, signature_base64);
console.log();
console.log('===================================================');
console.log(name);
console.log();
console.log('signing string:\n' + signing_string);
console.log();
console.log('signature_base64:\n' + signature_base64);
console.log();
console.log('signature_header_value:\n' + signature_header_value);
console.log();
if (signature_base64 === expected_signature) {
console.log('SUCESS: signature is the same as expected');
} else {
console.log('ERROR: signature is different than expected');
}
}
///// run tests /////
basic_test();
all_headers_test();