Skip to content

Commit

Permalink
Change computeSignatureData() method to public to support API Explorer.
Browse files Browse the repository at this point in the history
  • Loading branch information
msqr committed Apr 29, 2024
1 parent 3dd3309 commit 3d0c6be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/main/net/authV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,16 +723,18 @@ class AuthorizationV2Builder {
/**
* Compute the data to be signed by the signing key.
*
* The signature data takes this form:
*
* ```
* SNWS2-HMAC-SHA256
* 20170301T120000Z
* Hex(SHA256(canonicalRequestData))
* ```
*
* @param canonicalRequestData - the request data, returned from {@link Net.AuthorizationV2Builder#buildCanonicalRequestData}
* @returns the data to sign
*/
#computeSignatureData(canonicalRequestData: string): string {
/*- signature data is like:
SNWS2-HMAC-SHA256\n
20170301T120000Z\n
Hex(SHA256(canonicalRequestData))
*/
computeSignatureData(canonicalRequestData: string): string {
return (
"SNWS2-HMAC-SHA256\n" +
iso8601Date(this.#requestDate, true) +
Expand All @@ -755,7 +757,7 @@ class AuthorizationV2Builder {
const sortedHeaderNames = this.canonicalHeaderNames();
const canonicalReq =
this.#computeCanonicalRequestData(sortedHeaderNames);
const signatureData = this.#computeSignatureData(canonicalReq);
const signatureData = this.computeSignatureData(canonicalReq);
const signature = Hex.stringify(HmacSHA256(signatureData, signingKey));
const result =
"SNWS2 Credential=" +
Expand Down
7 changes: 7 additions & 0 deletions src/test/net/authV2Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from "ava";

import Base64 from "crypto-js/enc-base64.js";
import Hex from "crypto-js/enc-hex.js";
import SHA256 from "crypto-js/sha256.js";
import Environment from "../../main/net/environment.js";
import {
HttpMethod,
Expand Down Expand Up @@ -828,6 +829,12 @@ test("simplePostWithComputedDigest", (t) => {
reqBodySha256Hex
);

const sigData = builder.computeSignatureData(canonicalRequestData);
t.is(sigData, "SNWS2-HMAC-SHA256\n" +
"20170425T143000Z\n" +
Hex.stringify(SHA256(canonicalRequestData))
);

t.is(
builder.httpHeaders.firstValue(HttpHeaders.DIGEST),
"sha-256=" + reqBodySha256Base64
Expand Down

0 comments on commit 3d0c6be

Please sign in to comment.