|
| 1 | +--- |
| 2 | +title: include file |
| 3 | +description: JavaScript websocket callback security |
| 4 | +services: azure-communication-services |
| 5 | +author: Kunaal Punjabi |
| 6 | +ms.service: azure-communication-services |
| 7 | +ms.subservice: azure-communication-services |
| 8 | +ms.date: 05/06/2025 |
| 9 | +ms.topic: include |
| 10 | +ms.topic: include file |
| 11 | +ms.author: kpunjabi |
| 12 | +--- |
| 13 | + |
| 14 | +## Websocket code sample |
| 15 | + |
| 16 | +This sample code demonstrates how to configure OIDC client to validate webhook payload using JWT |
| 17 | + |
| 18 | +```JavaScript |
| 19 | +import express from "express"; |
| 20 | +import { JwksClient } from "jwks-rsa"; |
| 21 | +import { verify } from "jsonwebtoken"; |
| 22 | + |
| 23 | +const app = express(); |
| 24 | +const port = 3000; |
| 25 | +const audience = "ACS resource ID"; |
| 26 | +const issuer = "https://acscallautomation.communication.azure.com"; |
| 27 | + |
| 28 | +app.use(express.json()); |
| 29 | + |
| 30 | +app.post("/api/callback", (req, res) => { |
| 31 | + const token = req?.headers?.authorization?.split(" ")[1] || ""; |
| 32 | + |
| 33 | + if (!token) { |
| 34 | + res.sendStatus(401); |
| 35 | + |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + try { |
| 40 | + verify( |
| 41 | + token, |
| 42 | + (header, callback) => { |
| 43 | + const client = new JwksClient({ |
| 44 | + jwksUri: "https://acscallautomation.communication.azure.com/calling/keys", |
| 45 | + }); |
| 46 | + |
| 47 | + client.getSigningKey(header.kid, (err, key) => { |
| 48 | + const signingKey = key?.publicKey || key?.rsaPublicKey; |
| 49 | + |
| 50 | + callback(err, signingKey); |
| 51 | + }); |
| 52 | + }, |
| 53 | + { |
| 54 | + audience, |
| 55 | + issuer, |
| 56 | + algorithms: ["RS256"], |
| 57 | + }); |
| 58 | + // Your implementation on the callback event |
| 59 | + res.sendStatus(200); |
| 60 | + } catch (error) { |
| 61 | + res.sendStatus(401); |
| 62 | + } |
| 63 | +}); |
| 64 | + |
| 65 | +app.listen(port, () => { |
| 66 | + console.log(`Server running on port ${port}`); |
| 67 | +}); |
| 68 | +``` |
0 commit comments