UNPKG

1.28 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3import { hmac } from "./hmac";
4import { ResourceType, Constants } from "../common";
5export async function generateHeaders(masterKey, method, resourceType = ResourceType.none, resourceId = "", date = new Date()) {
6 if (masterKey.startsWith("type=sas&")) {
7 return {
8 [Constants.HttpHeaders.Authorization]: encodeURIComponent(masterKey),
9 [Constants.HttpHeaders.XDate]: date.toUTCString(),
10 };
11 }
12 const sig = await signature(masterKey, method, resourceType, resourceId, date);
13 return {
14 [Constants.HttpHeaders.Authorization]: sig,
15 [Constants.HttpHeaders.XDate]: date.toUTCString(),
16 };
17}
18async function signature(masterKey, method, resourceType, resourceId = "", date = new Date()) {
19 const type = "master";
20 const version = "1.0";
21 const text = method.toLowerCase() +
22 "\n" +
23 resourceType.toLowerCase() +
24 "\n" +
25 resourceId +
26 "\n" +
27 date.toUTCString().toLowerCase() +
28 "\n" +
29 "" +
30 "\n";
31 const signed = await hmac(masterKey, text);
32 return encodeURIComponent("type=" + type + "&ver=" + version + "&sig=" + signed);
33}
34//# sourceMappingURL=headers.js.map
\No newline at end of file