1 |
|
2 |
|
3 | import { hmac } from "./hmac";
|
4 | import { ResourceType, Constants } from "../common";
|
5 | export 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 | }
|
18 | async 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 |
|
\ | No newline at end of file |