Skip to content

Commit

Permalink
Fix 'setExpiration' to return seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Jul 26, 2020
1 parent eb970be commit 2917bdd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ If the JWT is invalid, the promise resolves to
#### setExpiration(exp: number | Date): number

Additionally there is the helper function `setExpiration` which simplifies
setting an expiration date.
setting an expiration date. It takes either an `Date` object or a number (in seconds) as argument.

```javascript
// A specific date:
setExpiration(new Date("2022-07-01"))
setExpiration(new Date("2025-07-01"))
// One hour from now:
setExpiration(new Date().getTime() + 60 * 60 * 1000)
setExpiration(60 * 60)
```

## Example
Expand All @@ -104,7 +104,7 @@ import { makeJwt, setExpiration, Jose, Payload } from "https://deno.land/x/djwt/
const key = "your-secret";
const payload: Payload = {
iss: "joe",
exp: setExpiration(new Date().getTime() + 60000),
exp: setExpiration(60),
};
const header: Jose = {
alg: "HS256",
Expand Down
2 changes: 1 addition & 1 deletion create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface Jose {
// returns the number of seconds since January 1, 1970, 00:00:00 UTC
function setExpiration(exp: number | Date): number {
return Math.round(
(exp instanceof Date ? exp : new Date(exp)).getTime() / 1000
(exp instanceof Date ? exp.getTime() : Date.now() + exp * 1000) / 1000
);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { validateJwt } from "../validate.ts";
const payload = {
iss: "joe",
jti: "123456789abc",
exp: setExpiration(new Date().getTime() + 20000),
exp: setExpiration(new Date("July 26, 2040 22:43:00")),
};
const header = {
alg: "HS256" as const,
Expand Down
2 changes: 1 addition & 1 deletion examples/readme_example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { makeJwt, setExpiration, Jose, Payload } from "../create.ts";
const key = "your-secret";
const payload: Payload = {
iss: "joe",
exp: setExpiration(new Date().getTime() + 60000),
exp: setExpiration(60),
};
const header: Jose = {
alg: "HS256",
Expand Down
21 changes: 12 additions & 9 deletions tests/djwt_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@ Deno.test("makeSetAndCheckExpirationTest", function (): void {
// A specific date:
const t1 = setExpiration(new Date("2020-01-01"));
const t2 = setExpiration(new Date("2099-01-01"));
// Ten seconds from now:
const t3 = setExpiration(10);
// One hour from now:
const t3 = setExpiration(new Date().getTime() + 60 * 60 * 1000);
const t4 = setExpiration(60 * 60);
// 1 second from now:
const t4 = setExpiration(new Date().getTime() + 1000);
const t5 = setExpiration(1);
// 1 second earlier:
const t5 = setExpiration(new Date().getTime() - 1000);
const t6 = setExpiration(-1);
assertEquals(isExpired(t1), true);
assertEquals(isExpired(t2), false);
assertEquals(isExpired(t3), false);
assertEquals(10, t3 - Math.round(Date.now() / 1000));
assertEquals(isExpired(t4), false);
assertEquals(isExpired(t5), true);
assertEquals(isExpired(t5), false);
assertEquals(isExpired(t6), true);
// add leeway:
assertEquals(isExpired(t5, 1500), false);
assertEquals(isExpired(t6, 1500), false);
});

Deno.test("makeDataConversionTest", function (): void {
Expand Down Expand Up @@ -206,7 +209,7 @@ Deno.test("testExpiredJwt", async function (): Promise<void> {
const payload = {
iss: "joe",
jti: "123456789abc",
exp: setExpiration(new Date().getTime() - 20000),
exp: setExpiration(-20000),
};
const header = {
alg: "HS256" as const,
Expand All @@ -226,7 +229,7 @@ Deno.test("makeCheckHeaderCritTest", async function (): Promise<void> {
const payload = {
iss: "joe",
jti: "123456789abc",
exp: setExpiration(new Date().getTime() + 1),
exp: setExpiration(1),
};
const header = {
alg: "HS256" as const,
Expand All @@ -250,7 +253,7 @@ Deno.test("makeHeaderCritTest", async function (): Promise<void> {
const payload = {
iss: "joe",
jti: "123456789abc",
exp: setExpiration(new Date().getTime() + 1),
exp: setExpiration(1),
};
const header = {
alg: "HS256" as const,
Expand Down

0 comments on commit 2917bdd

Please sign in to comment.