@octopusdeploy/step-packages-public-feed-encryption
Version:
A package that facilitates the generation of an encrypted signature for step package public feed. The encryption method follows the convention of [AWS Signature Version 4](https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html).
96 lines (90 loc) • 2.79 kB
text/typescript
import { createSignature, KeyValuePair } from "../encryption";
describe("HMAC encryption", () => {
test("signature should be created successfully", () => {
const expectedResult = "7892c95be58987aa446d08a2f15f4e476ae087773bd57dfd74b2cc55dfc1eca7";
const payloads: KeyValuePair[] = [
{
key: "package",
value: "e45b9ac1-640a-4337-8a14-6a3219b66a28",
},
{
key: "manifest",
value: JSON.stringify({
id: "hello-world",
version: "1.0.0",
type: "step-package",
metadata: {},
}),
},
];
const secretKey = "cbb5a5f1-6197-496b-a0e3-491c08bc9e70";
const algorithm = "sha256";
const requestMethod = "POST";
const requestTimestamp = "2022-01-25T04:39:44.586Z";
const requestHeader = { "x-functions-key": secretKey, "content-length": "999", timestamp: requestTimestamp };
const signature = createSignature(
algorithm,
secretKey,
requestMethod,
Object.entries(requestHeader).map(([key, value]) => ({ key, value })),
payloads,
requestTimestamp
);
expect(signature).toBe(expectedResult);
});
test("same signature is generated regardless of content orders", () => {
const secretKey = "cbb5a5f1-6197-496b-a0e3-491c08bc9e70";
const algorithm = "sha256";
const requestMethod = "POST";
const requestTimestamp = "2022-01-25T04:39:44.586Z";
let payloads: KeyValuePair[] = [
{
key: "package",
value: "e45b9ac1-640a-4337-8a14-6a3219b66a28",
},
{
key: "manifest",
value: JSON.stringify({
id: "hello-world",
version: "1.0.0",
type: "step-package",
metadata: {},
}),
},
];
let requestHeader = { "x-functions-key": secretKey, "content-length": "999", timestamp: requestTimestamp };
const signature1 = createSignature(
algorithm,
secretKey,
requestMethod,
Object.entries(requestHeader).map(([key, value]) => ({ key, value })),
payloads,
requestTimestamp
);
payloads = [
{
key: "manifest",
value: JSON.stringify({
id: "hello-world",
version: "1.0.0",
type: "step-package",
metadata: {},
}),
},
{
key: "package",
value: "e45b9ac1-640a-4337-8a14-6a3219b66a28",
},
];
requestHeader = { timestamp: requestTimestamp, "content-length": "999", "x-functions-key": secretKey };
const signature2 = createSignature(
algorithm,
secretKey,
requestMethod,
Object.entries(requestHeader).map(([key, value]) => ({ key, value })),
payloads,
requestTimestamp
);
expect(signature1).toBe(signature2);
});
});