|
| 1 | +# Express |
| 2 | + |
| 3 | +# Integrate your custom backend |
| 4 | + |
| 5 | +This guide shows how to build a custom backend that generates JWT tokens for FastAuth transaction signing using Express.js. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- Node.js and npm/pnpm installed |
| 10 | +- RSA key pair for JWT signing |
| 11 | + |
| 12 | +## Step 1: Generate RSA Key Pair |
| 13 | + |
| 14 | +```bash |
| 15 | +# Generate private key |
| 16 | +openssl genrsa -out jwtRS256.key 2048 |
| 17 | + |
| 18 | +# Generate public key |
| 19 | +openssl rsa -in jwtRS256.key -pubout -out jwtRS256.pub |
| 20 | +``` |
| 21 | + |
| 22 | +## Step 2: Install Dependencies |
| 23 | + |
| 24 | +```bash |
| 25 | +npm install express jsonwebtoken |
| 26 | +npm install -D @types/express @types/jsonwebtoken @types/node typescript |
| 27 | +``` |
| 28 | + |
| 29 | +## Step 3: Create the Express Server |
| 30 | + |
| 31 | +```typescript |
| 32 | +import { Request, Response } from "express"; |
| 33 | +import express from "express"; |
| 34 | +import jwt from "jsonwebtoken"; |
| 35 | +import * as fs from "node:fs"; |
| 36 | +import { createPublicKey } from "crypto"; |
| 37 | + |
| 38 | +const app = express(); |
| 39 | +const PORT = 3000; |
| 40 | +const JWT_ISSUER = "https://fa-custom-backend.com"; |
| 41 | +const JWT_SUBJECT = "user+1@fa-custom-backend.com"; |
| 42 | +``` |
| 43 | + |
| 44 | +## Step 4: Create JWT Generation Endpoint |
| 45 | + |
| 46 | +```typescript |
| 47 | +app.get("/jwt", (_: Request, response: Response) => { |
| 48 | + // Your transaction payload (hex-encoded) |
| 49 | + const SIGNING_PAYLOAD = Buffer.from("Hello world").toString("hex"); |
| 50 | + |
| 51 | + const token = jwt.sign( |
| 52 | + { fatxn: SIGNING_PAYLOAD }, // FastAuth transaction claim |
| 53 | + { key: fs.readFileSync("./jwtRS256.key") }, |
| 54 | + { |
| 55 | + expiresIn: "1h", |
| 56 | + algorithm: "RS256", |
| 57 | + issuer: JWT_ISSUER, |
| 58 | + subject: JWT_SUBJECT, |
| 59 | + }, |
| 60 | + ); |
| 61 | + |
| 62 | + response.status(200).send(token); |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +## Step 5: Start Server and Extract Public Key |
| 67 | + |
| 68 | +```typescript |
| 69 | +app.listen(PORT, () => { |
| 70 | + console.log("Server running at PORT: ", PORT); |
| 71 | + console.log("Jwt issuer: ", JWT_ISSUER); |
| 72 | + |
| 73 | + // Extract public key components for contract configuration |
| 74 | + const publicKey = createPublicKey(fs.readFileSync("./jwtRS256.pub")); |
| 75 | + const jwk = publicKey.export({ format: "jwk" }); |
| 76 | + |
| 77 | + console.log("Jwt public key [n]: ", jwk.n); |
| 78 | + console.log("Jwt public key [e]: ", jwk.e); |
| 79 | +}).on("error", (error: Error) => { |
| 80 | + throw new Error(error.message); |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +## Step 6: Run the Server |
| 85 | + |
| 86 | +```bash |
| 87 | +# Compile TypeScript |
| 88 | +npx tsc index.ts |
| 89 | + |
| 90 | +# Run the server |
| 91 | +node index.js |
| 92 | +``` |
| 93 | + |
| 94 | +## Step 7: Test JWT Generation |
| 95 | + |
| 96 | +```bash |
| 97 | +# Get JWT token |
| 98 | +curl http://localhost:3000/jwt |
| 99 | +``` |
| 100 | + |
| 101 | +## Next Steps |
| 102 | + |
| 103 | +1. Customize the `SIGNING_PAYLOAD` with your actual transaction data |
| 104 | +2. Implement user authentication before JWT generation |
| 105 | +3. Deploy your [JwtRS256Guard contract](./jwt-rs256-guard) with your custom jwt verification logic |
| 106 | +4. Register your guard in the JwtGuardRouter contract |
| 107 | +5. Test your integration with the FastAuth contract |
| 108 | + |
| 109 | +## Code |
| 110 | + |
| 111 | +You can find the complete code in the [examples](https://github.com/Peersyst/fast-auth/tree/main/examples/custom-backend) folder. |
0 commit comments