From 76f367b957cacbbae679e77eac86c593ac5cb514 Mon Sep 17 00:00:00 2001 From: Justin Spahr-Summers Date: Mon, 17 Feb 2025 12:17:00 +0000 Subject: [PATCH] WIP client registration handler for Express --- .../auth/handlers/clientRegistration.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/server/auth/handlers/clientRegistration.ts diff --git a/src/server/auth/handlers/clientRegistration.ts b/src/server/auth/handlers/clientRegistration.ts new file mode 100644 index 00000000..cdbe35aa --- /dev/null +++ b/src/server/auth/handlers/clientRegistration.ts @@ -0,0 +1,47 @@ +import { Request, Response } from "express"; +import { OAuthClientInformationFull, OAuthClientMetadataSchema, OAuthClientRegistrationError } from "../../../shared/auth.js"; +import crypto from 'node:crypto'; +import bodyParser from 'body-parser'; + +async function handler(requestBody: unknown): Promise { + let clientMetadata; + try { + clientMetadata = OAuthClientMetadataSchema.parse(requestBody); + } catch (error) { + return { error: "invalid_client_metadata", error_description: String(error) }; + } + + // Implement RFC 7591 dynamic client registration + const clientId = crypto.randomUUID(); + const clientSecret = clientMetadata.token_endpoint_auth_method !== 'none' + ? crypto.randomBytes(32).toString('hex') + : undefined; + const clientIdIssuedAt = Math.floor(Date.now() / 1000); + + const clientInfo: OAuthClientInformationFull = { + ...clientMetadata, + client_id: clientId, + client_secret: clientSecret, + client_id_issued_at: clientIdIssuedAt, + client_secret_expires_at: 0 // Set to 0 for non-expiring secret + }; + + // TODO: Store client information securely + + return clientInfo; +} + +export const clientRegistrationHandler = (req: Request, res: Response) => bodyParser.json()(req, res, (err) => { + if (err === undefined) { + handler(req.body).then((result) => { + if ("error" in result) { + res.status(400).json(result); + } else { + res.status(201).json(result); + } + }, (error) => { + console.error("Uncaught error in client registration handler:", error); + res.status(500).end("Internal Server Error"); + }); + } +}); \ No newline at end of file