diff --git a/README.md b/README.md index 11a8c17..c330f92 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,14 @@ # deno_aws_sign_v4 ![ci](https://github.com/lucacasonato/deno_aws_sign_v4/workflows/ci/badge.svg) -[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/aws_sign_v4@0.1.5/mod.ts) +[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/aws_sign_v4@1.0.0/mod.ts) Generates AWS Signature V4 for AWS low-level REST APIs. ## Example -The below example will generate signed headers based on the region and credentials in following ENV variables: +The below example will generate signed headers based on the region and +credentials in following ENV variables: - AWS_ACCESS_KEY_ID - AWS_SECRET_ACCESS_KEY @@ -15,7 +16,7 @@ The below example will generate signed headers based on the region and credentia - AWS_REGION ```typescript -import { AWSSignerV4 } from "https://deno.land/x/aws_sign_v4@0.1.5/mod.ts"; +import { AWSSignerV4 } from "https://deno.land/x/aws_sign_v4@1.0.0/mod.ts"; const signer = new AWSSignerV4(); const body = new TextEncoder().encode("Hello World!"); @@ -29,7 +30,8 @@ const req = await signer.sign("s3", request); const response = await fetch(req); ``` -You can also explicitly specify credentials and a region when constructing a new `AWSSignerV4`: +You can also explicitly specify credentials and a region when constructing a new +`AWSSignerV4`: ```typescript const signer = new AWSSignerV4("us-east-1", { @@ -48,4 +50,6 @@ const signer = new AWSSignerV4("us-east-1", { The module is licenced under GPL-3.0. For more see the LICENCE file. -This module is forked from @silver-xu's work in [https://github.com/silver-xu/deno-aws-sign-v4]. Many thanks to them. This fork has some large feature improvements and bug fixes, and has tests. +This module is forked from @silver-xu's work in +[https://github.com/silver-xu/deno-aws-sign-v4]. Many thanks to them. This fork +has some large feature improvements and bug fixes, and has tests. diff --git a/deps.ts b/deps.ts index 95f7ee7..7ddd5a3 100644 --- a/deps.ts +++ b/deps.ts @@ -1,6 +1,6 @@ -export { hmac } from "https://denopkg.com/chiefbiiko/hmac@v1.0.2/mod.ts"; +export { hmac } from "https://deno.land/x/hmac@v2.0.1/mod.ts"; -import { createHash } from "https://deno.land/std@0.79.0/hash/mod.ts"; +import { createHash } from "https://deno.land/std@0.84.0/hash/mod.ts"; export function sha256Hex(data: string | Uint8Array): string { const hasher = createHash("sha256"); hasher.update(data); diff --git a/mod.ts b/mod.ts index d8353d0..40c8b14 100644 --- a/mod.ts +++ b/mod.ts @@ -25,10 +25,10 @@ export interface Credentials { * credentials for this API using the options in the * constructor, or let them be aquired automatically * through environment variables. - * + * * Example usage: - * - * ```ts + * + * ```ts * const signer = new AWSSignerV4(); * const body = new TextEncoder().encode("Hello World!") * const request = new Request("https://test-bucket.s3.amazonaws.com/test", { @@ -47,7 +47,7 @@ export class AWSSignerV4 implements Signer { /** * If no region or credentials are specified, they will * automatically be aquired from environment variables. - * + * * Region is aquired from `AWS_REGION`. The credentials * are acquired from `AWS_ACCESS_KEY_ID`, * `AWS_SECRET_ACCESS_KEY` and `AWS_SESSION_TOKEN`. @@ -60,18 +60,15 @@ export class AWSSignerV4 implements Signer { /** * Use this to create the signed headers required to * make a call to an AWS API. - * - * @param service This is the AWS service, e.g. `s3` for s3, `dynamodb` for DynamoDB + * + * @param service This is the AWS service, e.g. `s3` for s3, `dynamodb` for DynamoDB * @param url The URL for the request to sign. * @param request The request method of the request to sign. * @param headers Other headers to include while signing. * @param body The body for PUT/POST methods. * @returns {RequestHeaders} - the signed request headers */ - public async sign( - service: string, - request: Request, - ): Promise { + public async sign(service: string, request: Request): Promise { const date = new Date(); const amzdate = toAmz(date); const datestamp = toDateStamp(date); @@ -127,23 +124,20 @@ export class AWSSignerV4 implements Signer { headers.set("Authorization", authHeader); - return new Request( - request.url, - { - headers, - method: request.method, - body, - cache: request.cache, - credentials: request.credentials, - integrity: request.integrity, - keepalive: request.keepalive, - mode: request.mode, - redirect: request.redirect, - referrer: request.referrer, - referrerPolicy: request.referrerPolicy, - signal: request.signal, - }, - ); + return new Request(request.url, { + headers, + method: request.method, + body, + cache: request.cache, + credentials: request.credentials, + integrity: request.integrity, + keepalive: request.keepalive, + mode: request.mode, + redirect: request.redirect, + referrer: request.referrer, + referrerPolicy: request.referrerPolicy, + signal: request.signal, + }); } #getDefaultCredentials = (): Credentials => { diff --git a/mod_test.ts b/mod_test.ts index 0b6552d..ff4a0eb 100644 --- a/mod_test.ts +++ b/mod_test.ts @@ -2,7 +2,7 @@ import { AWSSignerV4 } from "./mod.ts"; import { assertEquals, assertStringIncludes, -} from "https://deno.land/std@0.79.0/testing/asserts.ts"; +} from "https://deno.land/std@0.84.0/testing/asserts.ts"; Deno.test("construct from env vars", async () => { Deno.env.set("AWS_ACCESS_KEY_ID", "examplekey"); @@ -13,18 +13,17 @@ Deno.test("construct from env vars", async () => { const signer = new AWSSignerV4(); const req = await signer.sign( "dynamodb", - new Request( - "https://test.dynamodb.us-east-1.amazonaws.com", - { - method: "GET", - headers: { "x-hello": "world" }, - body: "A dynamodb request!", - }, - ), + new Request("https://test.dynamodb.us-east-1.amazonaws.com", { + method: "GET", + headers: { "x-hello": "world" }, + body: "A dynamodb request!", + }), ); const now = new Date(); const today = `${now.getFullYear()}${ - (now.getMonth() + 1).toString().padStart(2, "0") + (now.getMonth() + 1) + .toString() + .padStart(2, "0") }${now.getDate().toString().padStart(2, "0")}`; assertStringIncludes(req.headers.get("x-amz-date")!, `${today}T`); assertEquals(req.headers.get("x-amz-security-token"), "sessiontoken"); @@ -47,18 +46,17 @@ Deno.test("construct manually", async () => { }); const req = await signer.sign( "dynamodb", - new Request( - "https://test.dynamodb.us-east-1.amazonaws.com", - { - method: "GET", - headers: { "x-hello": "world" }, - body: "A dynamodb request!", - }, - ), + new Request("https://test.dynamodb.us-east-1.amazonaws.com", { + method: "GET", + headers: { "x-hello": "world" }, + body: "A dynamodb request!", + }), ); const now = new Date(); const today = `${now.getFullYear()}${ - (now.getMonth() + 1).toString().padStart(2, "0") + (now.getMonth() + 1) + .toString() + .padStart(2, "0") }${now.getDate().toString().padStart(2, "0")}`; assertStringIncludes(req.headers.get("x-amz-date")!, `${today}T`); assertEquals(req.headers.get("x-amz-security-token"), "session_token");