acme validation.

This commit is contained in:
Simon Larsen
2022-12-05 11:35:48 +05:30
parent 745ce954e2
commit 4ba2be8622
4 changed files with 64 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ enum ExceptionCode {
NotAuthorizedException = 403,
NotAuthenticatedxception = 401,
PaymentRequiredException = 402,
NotFoundException = 404,
}
export default ExceptionCode;

View File

@@ -0,0 +1,8 @@
import Exception from './Exception';
import ExceptionCode from './ExceptionCode';
export default class NotFoundException extends Exception {
public constructor(message: string) {
super(ExceptionCode.NotFoundException, message);
}
}

View File

@@ -243,4 +243,24 @@ export default class Response {
oneUptimeResponse.status(200).send(item);
this.logResponse(req, res, item as JSONObject);
}
public static sendTextResponse(
req: ExpressRequest,
res: ExpressResponse,
text: string
): void {
const oneUptimeRequest: OneUptimeRequest = req as OneUptimeRequest;
const oneUptimeResponse: OneUptimeResponse = res as OneUptimeResponse;
oneUptimeResponse.set(
'ExpressRequest-Id',
oneUptimeRequest.id.toString()
);
oneUptimeResponse.set('Pod-Id', process.env['POD_NAME']);
oneUptimeResponse.logBody = { text: text as string };
oneUptimeResponse.status(200).send(text);
this.logResponse(req, res, { text: text as string });
}
}

View File

@@ -7,6 +7,11 @@ import Express, {
ExpressStatic,
} from 'CommonServer/Utils/Express';
import logger from 'CommonServer/Utils/Logger';
import { PostgresAppInstance } from 'CommonServer/Infrastructure/PostgresDatabase';
import GreenlockChallengeService from "CommonServer/Services/GreenlockChallengeService";
import GreenlockChallenge from 'Model/Models/GreenlockChallenge';
import Response from 'CommonServer/Utils/Response';
import NotFoundException from 'Common/Types/Exception/NotFoundException';
export const APP_NAME: string = 'status-page';
@@ -21,6 +26,30 @@ app.use(
ExpressStatic(path.join(__dirname, 'dist'))
);
// ACME Challenge Validation.
app.use(['/.well-known/acme-challenge/:token'], async (
req: ExpressRequest,
res: ExpressResponse
) => {
const challenge : GreenlockChallenge | null = await GreenlockChallengeService.findOneBy({
query: {
key: req.params['token'] as string
},
select: {
challenge: true,
},
props: {
isRoot: true,
}
})
if (!challenge) {
return Response.sendErrorResponse(req, res, new NotFoundException("Challenge not found"));
}
return Response.sendTextResponse(req, res, challenge.challenge as string);
});
app.get('/*', (_req: ExpressRequest, res: ExpressResponse) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
@@ -29,6 +58,12 @@ const init: Function = async (): Promise<void> => {
try {
// init the app
await App(APP_NAME);
// connect to the database.
await PostgresAppInstance.connect(
PostgresAppInstance.getDatasourceOptions()
);
} catch (err) {
logger.error('App Init Failed:');
logger.error(err);