Skip to content

Commit

Permalink
update jwt api
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper committed Apr 26, 2024
1 parent 9465fcc commit a125398
Show file tree
Hide file tree
Showing 13 changed files with 544 additions and 516 deletions.
23 changes: 23 additions & 0 deletions docs/pages/reference/jwt/JWTHeader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: "JWTHeader"
---

# `JWTHeader`

Represents a JWT header.

## Definition

```ts
//$ JWTAlgorithm=/reference/jwt/JWTAlgorithm
interface JWT {
typ: "JWT";
alg: $$JWTAlgorithm;
[header: string]: any;
}
```

### Properties

- `typ`
- `alg`
32 changes: 32 additions & 0 deletions docs/pages/reference/jwt/JWTPayload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: "JWTPayload"
---

# `JWTPayload`

Represents a JWT payload.

## Definition

```ts
interface JWTPayload {
exp?: number;
iss?: string;
aud?: string[] | string;
jti?: string;
nbf?: number;
sub?: string;
iat?: number;
[claim: string]: any;
}
```

### Properties

- `exp`
- `iss`
- `aud`
- `jti`
- `nbf`
- `sub`
- `iat`
62 changes: 22 additions & 40 deletions docs/pages/reference/jwt/createJWT.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,48 @@ title: "createJWT()"

# `createJWT()`

Creates a new JWT. Claims are not included by default and must by defined with `options`.
Creates a new JWT. The algorithm is based on the header.

## Definition

```ts
//$ JWTAlgorithm=/reference/jwt/JWTAlgorithm
//$ TimeSpan=/reference/main/TimeSpan
function createJWT(
algorithm: $$JWTAlgorithm,
key: Uint8Array,
payloadClaims: Record<any, any>,
options?: {
headers?: Record<any, any>;
expiresIn?: $$TimeSpan;
issuer?: string;
subject?: string;
audiences?: string[];
notBefore?: Date;
includeIssuedTimestamp?: boolean;
jwtId?: string;
}
): Promise<string>;
//$ JWTHeader=/reference/jwt/JWTHeader
//$ JWTPayload=/reference/jwt/JWTPayload
function createJWT(key: Uint8Array, header: $$JWTHeader, payload: $$JWTPayload): Promise<string>;
```

### Parameters

- `algorithm`
- `key`: Secret key for HMAC, and private key for ECDSA and RSA
- `payloadClaims`
- `options`:
- `headers`: Custom headers
- `expiresIn`: How long the JWT is valid for (for `exp` claim)
- `issuer`: `iss` claim
- `subject`: `sub` claim
- `audiences`: `aud` claims
- `notBefore`: `nbf` claim
- `includeIssuedTimestamp` (default: `false`): Set to `true` to include `iat` claim
- `jwtId`: `jti` claim
- `header`
- `payload`

## Example

```ts
import { HMAC } from "oslo/crypto";
import { createJWT, validateJWT, parseJWT } from "oslo/jwt";
import { TimeSpan } from "oslo";
//$ HMAC=/reference/crypto/HMAC
//$ createJWTHeader=/reference/jwt/createJWTHeader
//$ createJWTPayload=/reference/jwt/createJWTHeader
import { $$HMAC } from "oslo/crypto";
import { createJWT, $$createJWTHeader, $$createJWTPayload } from "oslo/jwt";
import { $$TimeSpan } from "oslo";

const secret = await new HMAC("SHA-256").generateKey();
const key = await new HMAC("SHA-256").generateKey();

const payload = {
message: "hello, world"
};
const header = createJWTHeader("HS256");

const jwt = await createJWT("HS256", secret, payload, {
headers: {
kid
},
const basePayload = createJWTPayload({
expiresIn: new TimeSpan(30, "d"),
issuer,
subject,
audiences,
includeIssuedTimestamp: true
});

const payload = {
message: "hello, world",
...basePayload
};

const jwt = await createJWT(key, header, payload);
```
35 changes: 35 additions & 0 deletions docs/pages/reference/jwt/createJWTPayload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: "createJWTPayload()"
---

# `createJWTPayload()`

Creates a new JWT payload with registered claims.

## Definition

```ts
//$ TimeSpan=/reference/main/TimeSpan
//$ JWTHeader=/reference/jwt/JWTHeader
function createJWTPayload(options?: {
expiresIn?: $$TimeSpan;
issuer?: string;
subject?: string;
audiences?: string[];
notBefore?: Date;
includeIssuedTimestamp?: boolean;
jwtId?: string;
}): $$JWTHeader;
```

### Parameters

- `options`:
- `headers`: Custom headers
- `expiresIn`: How long the JWT is valid for (for `exp` claim)
- `issuer`: `iss` claim
- `subject`: `sub` claim
- `audiences`: `aud` claims
- `notBefore`: `nbf` claim
- `includeIssuedTimestamp` (default: `false`): Set to `true` to include `iat` claim
- `jwtId`: `jti` claim
19 changes: 19 additions & 0 deletions docs/pages/reference/jwt/createJWTheader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: "createJWTHeader()"
---

# `createJWTHeader()`

Creates a new JWT header.

## Definition

```ts
//$ JWTAlgorithm=/reference/jwt/JWTAlgorithm
//$ JWTHeader=/reference/jwt/JWTHeader
function createJWTHeader(algorithm: $$JWTAlgorithm): $$JWTHeader;
```

### Parameters

- `algorithm`
6 changes: 5 additions & 1 deletion docs/pages/reference/jwt/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ Provides utilities for working with JWTs. Supports the following algorithms:

- HMAC: `HS256`, `HS384`, `HS512`
- ECDSA: `ES256`, `ES384`, `ES512`
- RSASSA-PKCS1-v1_5: `RS256`, `RS384`, `RS512`
- RSASSA-PKCS1-v1.5: `RS256`, `RS384`, `RS512`
- RSASSA-PSS: `PS256`, `PS384`, `PS512`

## Functions

- [`createJWT()`](/reference/jwt/createJWT)
- [`createJWTHeader()`](/reference/jwt/createJWTHeader)
- [`createJWTPayload()`](/reference/jwt/createJWTPayload)
- [`parseJWT()`](/reference/jwt/parseJWT)
- [`validateJWT()`](/reference/jwt/validateJWT)

## Interfaces

- [`JWT`](/reference/jwt/JWT)
- [`JWTHeader`](/reference/jwt/JWTHeader)
- [`JWTPayload`](/reference/jwt/JWTPayload)

## Types

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/reference/main/addToDate.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Creates a new `Date` by adding the provided time-span to the one provided. Suppo

```ts
//$ TimeSpan=/reference/main/TimeSpan
function createDate(date: Date, timeSpan: $$TimeSpan): Date;
function addToDate(date: Date, timeSpan: $$TimeSpan): Date;
```

### Parameters
Expand Down
5 changes: 1 addition & 4 deletions docs/pages/reference/main/isWithinExpirationDate.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ function isWithinExpirationDate(expirationDate: Date): boolean;
## Example

```ts
import { createDate, TimeSpan, isWithinExpirationDate } from "oslo";

const tomorrow = createDate(new TimeSpan(1, "d"));
const yesterday = createDate(new TimeSpan(-1, "d"));
import { isWithinExpirationDate } from "oslo";

isWithinExpirationDate(tomorrow); // true
isWithinExpirationDate(yesterday); // false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function revokeAccessToken(
```ts
//$ OAuth2RequestError=/reference/oauth2/OAuth2RequestError
//$ OAuth2TokenRevocationRetryError=/reference/oauth2/OAuth2TokenRevocationRetryError
import { $OAuth2RequestError, $OAuth2TokenRevocationRetryError } from "oslo/oauth2";
import { $$OAuth2RequestError, $$OAuth2TokenRevocationRetryError } from "oslo/oauth2";

try {
const url = oauth2Client.revokeAccessToken(accessToken, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function revokeRefreshToken(
```ts
//$ OAuth2RequestError=/reference/oauth2/OAuth2RequestError
//$ OAuth2TokenRevocationRetryError=/reference/oauth2/OAuth2TokenRevocationRetryError
import { $OAuth2RequestError, $OAuth2TokenRevocationRetryError } from "oslo/oauth2";
import { $$OAuth2RequestError, $$OAuth2TokenRevocationRetryError } from "oslo/oauth2";

try {
const url = oauth2Client.revokeRefreshToken(refreshToken, {
Expand Down
Loading

0 comments on commit a125398

Please sign in to comment.