Skip to content

Commit 8c6936f

Browse files
committed
Fix Custom JWT page
1 parent c765316 commit 8c6936f

5 files changed

Lines changed: 204 additions & 233 deletions

File tree

docs/authentication/advanced/id-token.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Web3AuthIdentity Token
2+
title: Web3Auth Identity Token
33
sidebar_label: Identity Token
44
image: "images/docs-meta-cards/documentation-card.png"
55
description: "Identity Token | Documentation - Web3Auth"

docs/authentication/custom-connections/custom-jwt.mdx

Lines changed: 202 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ description: "Custom JWT Login with Web3Auth | Documentation - Web3Auth"
55
image: "images/docs-meta-cards/documentation-card.png"
66
---
77

8+
import TabItem from "@theme/TabItem";
9+
import Tabs from "@theme/Tabs";
10+
811
import CustomConnectionOptions from "@site/static/images/dashboard/authentication-custom-connections.png";
912
import CustomJWTConnection from "@site/static/images/dashboard/custom-connection.png";
1013

@@ -29,10 +32,14 @@ Web3Auth Dashboard.
2932
To use this feature, developers must go to the `Custom Connections` tab in the
3033
[Web3Auth Dashboard](https://dashboard.web3auth.io).
3134

32-
<div style={{ flexBasis: "300px", flexGrow: "1", textAlign: "center" }}>
35+
<div style={{ display: "flex", margin: "20px 0" }}>
3336
<img
3437
src={CustomConnectionOptions}
35-
style={{ alignSelf: "center", maxWidth: "100%" }}
38+
style={{
39+
maxWidth: "600px",
40+
borderRadius: "8px",
41+
boxShadow: "0 2px 6px rgba(0, 0, 0, 0.1)",
42+
}}
3643
alt="Custom Connection Options"
3744
/>
3845
</div>
@@ -54,27 +61,209 @@ Follow these steps to create a AWS Cognito connection:
5461
1. Next, type aud as a field and `your-audience` as a value.
5562
1. Finally, click on the `Add Connection` button.
5663

57-
<div style={{ flexBasis: "300px", flexGrow: "1", textAlign: "left" }}>
64+
<div style={{ display: "flex", margin: "20px 0" }}>
5865
<img
5966
src={CustomJWTConnection}
60-
style={{ alignSelf: "center", maxWidth: "100%" }}
67+
style={{
68+
maxWidth: "600px",
69+
borderRadius: "8px",
70+
boxShadow: "0 2px 6px rgba(0, 0, 0, 0.1)",
71+
}}
6172
alt="Custom JWT Connection"
6273
/>
6374
</div>
6475

65-
## Generate JWT
76+
## Create JWT
77+
78+
To generate the JWT, developers may use a package of their choice. Web3Auth provides documentation
79+
and examples using both the `jsonwebtoken` and `jose` libraries.
80+
81+
### Generate Private Key
82+
83+
Developers can generate a private key using the `openssl` command-line tool. This private key will
84+
be used to sign the ID token.
85+
86+
<Tabs
87+
defaultValue="rsa"
88+
values={[
89+
{ label: "RSA256", value: "rsa" },
90+
{ label: "ECDSA", value: "ecdsa" },
91+
]}
92+
>
93+
<TabItem value="rsa">
94+
95+
Developers can run the following command in the terminal to generate a new `privateKey.pem` file
96+
containing the `RSA256` key details.
97+
98+
```bash
99+
openssl genrsa -out privateKey.pem 2048
100+
```
101+
102+
Once the private key is generated, developers can generate the public key which can be used to
103+
verify the JWT and [convert it to JWKS](#how-to-convert-pem-to-jwks).
104+
105+
```bash
106+
openssl rsa -in privateKey.pem -pubout -out publicKey.pem
107+
```
108+
109+
</TabItem>
110+
111+
<TabItem value="ecdsa">
112+
113+
Developers can run the following command in the terminal to generate a new `privateKey.pem` file
114+
containing the `ECDSA` key details.
66115

67-
To generate the JWT, you can choose package of your choice. We have documented few of the well known
68-
packages.
116+
```bash
117+
openssl ecparam -name secp256k1 -genkey -noout -out ec-secp256k1-privateKey.pem
118+
```
119+
120+
Once the private key is generated, developers can generate the public key which can be used to
121+
verify the JWT and [convert it to JWKS](#how-to-convert-pem-to-jwks).
69122

70-
- [Generate JWT using jsonwebtoken](/docs/authentication/custom-connections/custom/jsonwebtoken)
71-
- [Generate JWT using jose](/docs/authentication/custom-connections/custom/jose)
123+
```bash
124+
openssl ec -in ec-secp256k1-privateKey.pem -pubout -out ec-secp256k1-publicKey.pem
125+
```
72126

73-
:::warning Facing issue with JWT?
127+
</TabItem>
128+
</Tabs>
74129

75-
Check out [**this troubleshooting page to fix those.**](/troubleshooting/jwt-errors)
130+
### Install JWT Library
76131

77-
:::
132+
Developers can install a JWT library of their choice. Following are the documentation and examples
133+
using both the `jsonwebtoken` and `jose` libraries.
134+
135+
```bash npm2yarn
136+
npm i jsonwebtoken
137+
138+
npm i jose
139+
```
140+
141+
### Generate JWT
142+
143+
<Tabs
144+
defaultValue="jsonwebtoken"
145+
values={[
146+
{ label: "jsonwebtoken", value: "jsonwebtoken" },
147+
{ label: "jose", value: "jose" },
148+
]}
149+
>
150+
<TabItem value="jsonwebtoken">
151+
152+
Web3Auth provides documentation for using RSA256 and ECDSA—two of the most commonly used
153+
algorithms—for generating JWTs with the `jsonwebtoken` package. For a complete list of supported
154+
algorithms, developers can refer to the
155+
[jsonwebtoken documentation](https://www.npmjs.com/package/jsonwebtoken#algorithms-supported).
156+
157+
<Tabs
158+
defaultValue="rsa"
159+
values={[
160+
{ label: "RSA256", value: "rsa" },
161+
{ label: "ECDSA", value: "ecdsa" },
162+
]}
163+
>
164+
<TabItem value="rsa">
165+
166+
Developers can create an `index.js` file and insert the following code snippet to generate a JWT
167+
using the `RSA` algorithm.
168+
169+
```tsx
170+
import jwt from "jsonwebtoken";
171+
import fs from "fs";
172+
173+
var privateKey = fs.readFileSync("privateKey.pem");
174+
175+
var token = jwt.sign(
176+
{
177+
sub: "faj2720i2fdG7NsqznOKrthDvq43", // must be unique to each user
178+
name: "Mohammad Shahbaz Alam",
179+
email: "shahbaz@web3auth.io",
180+
aud: "urn:my-resource-server", // -> to be used in Custom Authentication as JWT Field
181+
iss: "https://my-authz-server", // -> to be used in Custom Authentication as JWT Field
182+
iat: Math.floor(Date.now() / 1000),
183+
exp: Math.floor(Date.now() / 1000) + 60 * 60,
184+
},
185+
privateKey,
186+
{ algorithm: "RS256", keyid: "1bb9605c36e69386830202b2d" }, // <-- Replace it with your kid. This has to be present in the JWKS endpoint.
187+
);
188+
189+
console.log(token);
190+
```
191+
192+
</TabItem>
193+
<TabItem value="ecdsa">
194+
195+
Developers can create an `index.js` file and insert the following code snippet to generate a JWT
196+
using the `ECDSA` algorithm.
197+
198+
```tsx
199+
import jwt from "jsonwebtoken";
200+
import fs from "fs";
201+
202+
var privateKey = fs.readFileSync("ec-secp256k1-privateKey.pem");
203+
204+
var token = jwt.sign(
205+
{
206+
sub: "faj2720i2fdG7NsqzncndijwnKrthDvq43",
207+
name: "Mohammad Shahbaz Alam",
208+
email: "shahbaz@web3auth.io",
209+
aud: "urn:my-resource-server", // -> to be used in Custom Authentication as JWT Field
210+
iss: "https://my-authz-server", // -> to be used in Custom Authentication as JWT Field
211+
iat: Math.floor(Date.now() / 1000),
212+
exp: Math.floor(Date.now() / 1000) + 60 * 60,
213+
},
214+
privateKey,
215+
{ algorithm: "ECDSA", keyid: "1bb9605c36e69386830202b2d" }, // <-- Replace it with your kid. This has to be present in the JWKS endpoint.
216+
);
217+
218+
console.log(token);
219+
```
220+
221+
</TabItem>
222+
</Tabs>
223+
224+
</TabItem>
225+
<TabItem value="jose">
226+
227+
Create an index.js file and paste the below code to generate the JWT using RSA algorithm.
228+
229+
```tsx
230+
import * as jose from "jose";
231+
import fs from "fs";
232+
var privateKey = fs.readFileSync("privateKey.pem");
233+
var publicKey = fs.readFileSync("publicKey.pem");
234+
235+
const jwt = await new jose.SignJWT({ "urn:example:claim": true })
236+
.setProtectedHeader({ alg: "RS256", kid: "1bb9605c36e69386830202b2d" }) // <-- Replace it with your kid. This has to be present in the JWKS endpoint.
237+
.setIssuedAt()
238+
.setIssuer("https://my-authz-server")
239+
.setAudience("urn:my-resource-server")
240+
.setExpirationTime("2h")
241+
.sign(privateKey);
242+
243+
console.log(jwt);
244+
245+
// Verifying the JWT using Remote JWK Set.
246+
// This is just to show how the Verify works, look above to set-up custom jwt verifier on the Web3Auth Dashboard.
247+
// Check the steps below to see how once can generate the JWKS
248+
const JWKS = jose.createRemoteJWKSet(new URL("https://my-authz-server/.well-known/jwks.json"));
249+
250+
const { payload, protectedHeader } = await jose.jwtVerify(jwt, JWKS, {
251+
issuer: "https://my-authz-server",
252+
audience: "urn:my-resource-server",
253+
});
254+
255+
console.log(protectedHeader);
256+
console.log(payload);
257+
```
258+
259+
</TabItem>
260+
</Tabs>
261+
262+
:::warning Facing issue with JWT?
263+
264+
Check out [**this troubleshooting page to fix those.**](/troubleshooting/jwt-errors)
265+
266+
:::
78267

79268
## Usage
80269

@@ -108,7 +297,7 @@ createRoot(document.getElementById("root")!).render(
108297
);
109298
```
110299

111-
Since, the `Firebase Connection` details are available from Dashboard, developers don't need to pass
300+
Since, the `Custom Connection` details are available from Dashboard, developers don't need to pass
112301
any additional parameters to the `Web3AuthProvider`.
113302

114303
> Follow our [Quickstart Guide](/quick-start) to setup the basic flow.

docs/authentication/custom-connections/custom/jose.mdx

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)