Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit a125398

Browse files
update jwt api
1 parent 9465fcc commit a125398

13 files changed

+544
-516
lines changed

docs/pages/reference/jwt/JWTHeader.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: "JWTHeader"
3+
---
4+
5+
# `JWTHeader`
6+
7+
Represents a JWT header.
8+
9+
## Definition
10+
11+
```ts
12+
//$ JWTAlgorithm=/reference/jwt/JWTAlgorithm
13+
interface JWT {
14+
typ: "JWT";
15+
alg: $$JWTAlgorithm;
16+
[header: string]: any;
17+
}
18+
```
19+
20+
### Properties
21+
22+
- `typ`
23+
- `alg`
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: "JWTPayload"
3+
---
4+
5+
# `JWTPayload`
6+
7+
Represents a JWT payload.
8+
9+
## Definition
10+
11+
```ts
12+
interface JWTPayload {
13+
exp?: number;
14+
iss?: string;
15+
aud?: string[] | string;
16+
jti?: string;
17+
nbf?: number;
18+
sub?: string;
19+
iat?: number;
20+
[claim: string]: any;
21+
}
22+
```
23+
24+
### Properties
25+
26+
- `exp`
27+
- `iss`
28+
- `aud`
29+
- `jti`
30+
- `nbf`
31+
- `sub`
32+
- `iat`

docs/pages/reference/jwt/createJWT.md

+22-40
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,48 @@ title: "createJWT()"
44

55
# `createJWT()`
66

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

99
## Definition
1010

1111
```ts
12-
//$ JWTAlgorithm=/reference/jwt/JWTAlgorithm
13-
//$ TimeSpan=/reference/main/TimeSpan
14-
function createJWT(
15-
algorithm: $$JWTAlgorithm,
16-
key: Uint8Array,
17-
payloadClaims: Record<any, any>,
18-
options?: {
19-
headers?: Record<any, any>;
20-
expiresIn?: $$TimeSpan;
21-
issuer?: string;
22-
subject?: string;
23-
audiences?: string[];
24-
notBefore?: Date;
25-
includeIssuedTimestamp?: boolean;
26-
jwtId?: string;
27-
}
28-
): Promise<string>;
12+
//$ JWTHeader=/reference/jwt/JWTHeader
13+
//$ JWTPayload=/reference/jwt/JWTPayload
14+
function createJWT(key: Uint8Array, header: $$JWTHeader, payload: $$JWTPayload): Promise<string>;
2915
```
3016

3117
### Parameters
3218

33-
- `algorithm`
3419
- `key`: Secret key for HMAC, and private key for ECDSA and RSA
35-
- `payloadClaims`
36-
- `options`:
37-
- `headers`: Custom headers
38-
- `expiresIn`: How long the JWT is valid for (for `exp` claim)
39-
- `issuer`: `iss` claim
40-
- `subject`: `sub` claim
41-
- `audiences`: `aud` claims
42-
- `notBefore`: `nbf` claim
43-
- `includeIssuedTimestamp` (default: `false`): Set to `true` to include `iat` claim
44-
- `jwtId`: `jti` claim
20+
- `header`
21+
- `payload`
4522

4623
## Example
4724

4825
```ts
49-
import { HMAC } from "oslo/crypto";
50-
import { createJWT, validateJWT, parseJWT } from "oslo/jwt";
51-
import { TimeSpan } from "oslo";
26+
//$ HMAC=/reference/crypto/HMAC
27+
//$ createJWTHeader=/reference/jwt/createJWTHeader
28+
//$ createJWTPayload=/reference/jwt/createJWTHeader
29+
import { $$HMAC } from "oslo/crypto";
30+
import { createJWT, $$createJWTHeader, $$createJWTPayload } from "oslo/jwt";
31+
import { $$TimeSpan } from "oslo";
5232

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

55-
const payload = {
56-
message: "hello, world"
57-
};
35+
const header = createJWTHeader("HS256");
5836

59-
const jwt = await createJWT("HS256", secret, payload, {
60-
headers: {
61-
kid
62-
},
37+
const basePayload = createJWTPayload({
6338
expiresIn: new TimeSpan(30, "d"),
6439
issuer,
6540
subject,
6641
audiences,
6742
includeIssuedTimestamp: true
6843
});
44+
45+
const payload = {
46+
message: "hello, world",
47+
...basePayload
48+
};
49+
50+
const jwt = await createJWT(key, header, payload);
6951
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: "createJWTPayload()"
3+
---
4+
5+
# `createJWTPayload()`
6+
7+
Creates a new JWT payload with registered claims.
8+
9+
## Definition
10+
11+
```ts
12+
//$ TimeSpan=/reference/main/TimeSpan
13+
//$ JWTHeader=/reference/jwt/JWTHeader
14+
function createJWTPayload(options?: {
15+
expiresIn?: $$TimeSpan;
16+
issuer?: string;
17+
subject?: string;
18+
audiences?: string[];
19+
notBefore?: Date;
20+
includeIssuedTimestamp?: boolean;
21+
jwtId?: string;
22+
}): $$JWTHeader;
23+
```
24+
25+
### Parameters
26+
27+
- `options`:
28+
- `headers`: Custom headers
29+
- `expiresIn`: How long the JWT is valid for (for `exp` claim)
30+
- `issuer`: `iss` claim
31+
- `subject`: `sub` claim
32+
- `audiences`: `aud` claims
33+
- `notBefore`: `nbf` claim
34+
- `includeIssuedTimestamp` (default: `false`): Set to `true` to include `iat` claim
35+
- `jwtId`: `jti` claim
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: "createJWTHeader()"
3+
---
4+
5+
# `createJWTHeader()`
6+
7+
Creates a new JWT header.
8+
9+
## Definition
10+
11+
```ts
12+
//$ JWTAlgorithm=/reference/jwt/JWTAlgorithm
13+
//$ JWTHeader=/reference/jwt/JWTHeader
14+
function createJWTHeader(algorithm: $$JWTAlgorithm): $$JWTHeader;
15+
```
16+
17+
### Parameters
18+
19+
- `algorithm`

docs/pages/reference/jwt/index.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ Provides utilities for working with JWTs. Supports the following algorithms:
88

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

1414
## Functions
1515

1616
- [`createJWT()`](/reference/jwt/createJWT)
17+
- [`createJWTHeader()`](/reference/jwt/createJWTHeader)
18+
- [`createJWTPayload()`](/reference/jwt/createJWTPayload)
1719
- [`parseJWT()`](/reference/jwt/parseJWT)
1820
- [`validateJWT()`](/reference/jwt/validateJWT)
1921

2022
## Interfaces
2123

2224
- [`JWT`](/reference/jwt/JWT)
25+
- [`JWTHeader`](/reference/jwt/JWTHeader)
26+
- [`JWTPayload`](/reference/jwt/JWTPayload)
2327

2428
## Types
2529

docs/pages/reference/main/addToDate.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Creates a new `Date` by adding the provided time-span to the one provided. Suppo
1010

1111
```ts
1212
//$ TimeSpan=/reference/main/TimeSpan
13-
function createDate(date: Date, timeSpan: $$TimeSpan): Date;
13+
function addToDate(date: Date, timeSpan: $$TimeSpan): Date;
1414
```
1515

1616
### Parameters

docs/pages/reference/main/isWithinExpirationDate.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ function isWithinExpirationDate(expirationDate: Date): boolean;
2020
## Example
2121

2222
```ts
23-
import { createDate, TimeSpan, isWithinExpirationDate } from "oslo";
24-
25-
const tomorrow = createDate(new TimeSpan(1, "d"));
26-
const yesterday = createDate(new TimeSpan(-1, "d"));
23+
import { isWithinExpirationDate } from "oslo";
2724

2825
isWithinExpirationDate(tomorrow); // true
2926
isWithinExpirationDate(yesterday); // false

docs/pages/reference/oauth2/OAuth2TokenRevocationClient/revokeAccessToken.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function revokeAccessToken(
3030
```ts
3131
//$ OAuth2RequestError=/reference/oauth2/OAuth2RequestError
3232
//$ OAuth2TokenRevocationRetryError=/reference/oauth2/OAuth2TokenRevocationRetryError
33-
import { $OAuth2RequestError, $OAuth2TokenRevocationRetryError } from "oslo/oauth2";
33+
import { $$OAuth2RequestError, $$OAuth2TokenRevocationRetryError } from "oslo/oauth2";
3434

3535
try {
3636
const url = oauth2Client.revokeAccessToken(accessToken, {

docs/pages/reference/oauth2/OAuth2TokenRevocationClient/revokeRefreshToken.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function revokeRefreshToken(
3030
```ts
3131
//$ OAuth2RequestError=/reference/oauth2/OAuth2RequestError
3232
//$ OAuth2TokenRevocationRetryError=/reference/oauth2/OAuth2TokenRevocationRetryError
33-
import { $OAuth2RequestError, $OAuth2TokenRevocationRetryError } from "oslo/oauth2";
33+
import { $$OAuth2RequestError, $$OAuth2TokenRevocationRetryError } from "oslo/oauth2";
3434

3535
try {
3636
const url = oauth2Client.revokeRefreshToken(refreshToken, {

0 commit comments

Comments
 (0)