my monorepo for atproto based applications
1import express from "express";
2import {
3 verifyJwt,
4 AuthRequiredError,
5 parseReqNsid,
6} from "@atproto/xrpc-server";
7import { DidResolver } from "@atproto/identity";
8
9export const validateAuth = async (
10 req: express.Request,
11 serviceDid: string,
12 didResolver: DidResolver,
13): Promise<string> => {
14 const { authorization = "" } = req.headers;
15 if (!authorization.startsWith("Bearer ")) {
16 throw new AuthRequiredError();
17 }
18 const jwt = authorization.replace("Bearer ", "").trim();
19 const nsid = parseReqNsid(req);
20 const parsed = await verifyJwt(jwt, serviceDid, nsid, async (did: string) => {
21 return didResolver.resolveAtprotoKey(did);
22 });
23 return parsed.iss;
24};