/
StorageSharedKeyCredential.ts
75 lines (68 loc) · 2.05 KB
/
StorageSharedKeyCredential.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { createHmac } from "crypto";
import { RequestPolicy, RequestPolicyOptions } from "@azure/core-http";
import { StorageSharedKeyCredentialPolicy } from "../policies/StorageSharedKeyCredentialPolicy";
import { Credential } from "./Credential";
/**
* ONLY AVAILABLE IN NODE.JS RUNTIME.
*
* StorageSharedKeyCredential for account key authorization of Azure Storage service.
*
* @export
* @class StorageSharedKeyCredential
* @extends {Credential}
*/
export class StorageSharedKeyCredential extends Credential {
/**
* Azure Storage account name; readonly.
*
* @type {string}
* @memberof StorageSharedKeyCredential
*/
public readonly accountName: string;
/**
* Azure Storage account key; readonly.
*
* @type {Buffer}
* @memberof StorageSharedKeyCredential
*/
private readonly accountKey: Buffer;
/**
* Creates an instance of StorageSharedKeyCredential.
* @param {string} accountName
* @param {string} accountKey
* @memberof StorageSharedKeyCredential
*/
constructor(accountName: string, accountKey: string) {
super();
this.accountName = accountName;
this.accountKey = Buffer.from(accountKey, "base64");
}
/**
* Creates a {@link StorageSharedKeyCredentialPolicy} object.
*
* @param {RequestPolicy} nextPolicy
* @param {RequestPolicyOptions} options
* @returns {StorageSharedKeyCredentialPolicy}
* @memberof StorageSharedKeyCredential
*/
public create(
nextPolicy: RequestPolicy,
options: RequestPolicyOptions
): StorageSharedKeyCredentialPolicy {
return new StorageSharedKeyCredentialPolicy(nextPolicy, options, this);
}
/**
* Generates a hash signature for an HTTP request or for a SAS.
*
* @param {string} stringToSign
* @returns {string}
* @memberof StorageSharedKeyCredential
*/
public computeHMACSHA256(stringToSign: string): string {
return createHmac("sha256", this.accountKey)
.update(stringToSign, "utf8")
.digest("base64");
}
}