Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update dependencies #11

Merged
merged 2 commits into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# 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
- AWS_SESSION_TOKEN
- 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!");
Expand All @@ -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", {
Expand All @@ -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.
4 changes: 2 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
48 changes: 21 additions & 27 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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`.
Expand All @@ -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<Request> {
public async sign(service: string, request: Request): Promise<Request> {
const date = new Date();
const amzdate = toAmz(date);
const datestamp = toDateStamp(date);
Expand Down Expand Up @@ -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 => {
Expand Down
36 changes: 17 additions & 19 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down