Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
45 lines
1.8 KiB
Plaintext
45 lines
1.8 KiB
Plaintext
import * as z from 'zod/v4';
|
|
import { InvalidRequestError, InvalidClientError, ServerError, OAuthError } from '../errors.js';
|
|
const ClientAuthenticatedRequestSchema = z.object({
|
|
client_id: z.string(),
|
|
client_secret: z.string().optional()
|
|
});
|
|
export function authenticateClient({ clientsStore }) {
|
|
return async (req, res, next) => {
|
|
try {
|
|
const result = ClientAuthenticatedRequestSchema.safeParse(req.body);
|
|
if (!result.success) {
|
|
throw new InvalidRequestError(String(result.error));
|
|
}
|
|
const { client_id, client_secret } = result.data;
|
|
const client = await clientsStore.getClient(client_id);
|
|
if (!client) {
|
|
throw new InvalidClientError('Invalid client_id');
|
|
}
|
|
if (client.client_secret) {
|
|
if (!client_secret) {
|
|
throw new InvalidClientError('Client secret is required');
|
|
}
|
|
if (client.client_secret !== client_secret) {
|
|
throw new InvalidClientError('Invalid client_secret');
|
|
}
|
|
if (client.client_secret_expires_at && client.client_secret_expires_at < Math.floor(Date.now() / 1000)) {
|
|
throw new InvalidClientError('Client secret has expired');
|
|
}
|
|
}
|
|
req.client = client;
|
|
next();
|
|
}
|
|
catch (error) {
|
|
if (error instanceof OAuthError) {
|
|
const status = error instanceof ServerError ? 500 : 400;
|
|
res.status(status).json(error.toResponseObject());
|
|
}
|
|
else {
|
|
const serverError = new ServerError('Internal Server Error');
|
|
res.status(500).json(serverError.toResponseObject());
|
|
}
|
|
}
|
|
};
|
|
}
|
|
//# sourceMappingURL=clientAuth.js.map |