Try to make challenge works

This commit is contained in:
Fang-Pen Lin
2025-10-31 14:21:48 -07:00
parent 9e8f3843a6
commit 737bbd3e08
4 changed files with 21 additions and 13 deletions

View File

@@ -56,6 +56,19 @@ export const pkiAcmeChallengeDALFactory = (db: TDbClient) => {
}
};
const markAsInvalidCascadeById = async (id: string, tx?: Knex): Promise<TPkiAcmeChallenges> => {
try {
const [challenge] = (await (tx || db)(TableName.PkiAcmeChallenge)
.where({ id })
.update({ status: AcmeChallengeStatus.Valid, validatedAt: new Date() })
.returning("*")) as [TPkiAcmeChallenges];
// TODO:
return challenge;
} catch (error) {
throw new DatabaseError({ error, name: "Update certificate profile" });
}
};
const findByAccountAuthAndChallengeId = async (accountId: string, authId: string, challengeId: string, tx?: Knex) => {
try {
const challenge = await (tx || db)(TableName.PkiAcmeChallenge)
@@ -126,6 +139,7 @@ export const pkiAcmeChallengeDALFactory = (db: TDbClient) => {
return {
...pkiAcmeChallengeOrm,
markAsValidCascadeById,
markAsInvalidCascadeById,
findByAccountAuthAndChallengeId,
findByIdForChallengeValidation
};

View File

@@ -8,6 +8,7 @@ import { TPkiAcmeChallengeDALFactory } from "./pki-acme-challenge-dal";
import { AcmeIncorrectResponseError } from "./pki-acme-errors";
import { AcmeAuthStatus, AcmeChallengeStatus, AcmeChallengeType } from "./pki-acme-schemas";
import { TPkiAcmeChallengeServiceFactory } from "./pki-acme-types";
import { TPkiAcmeChallenges } from "@app/db/schemas";
type TPkiAcmeChallengeServiceFactoryDep = {
acmeAuthDAL: Pick<TPkiAcmeAuthDALFactory, "updateById">;
@@ -23,8 +24,8 @@ export const pkiAcmeChallengeServiceFactory = ({
}: TPkiAcmeChallengeServiceFactoryDep): TPkiAcmeChallengeServiceFactory => {
const appCfg = getConfig();
const validateChallengeResponse = async (challengeId: string, tx?: Knex): Promise<void> => {
return await acmeChallengeDAL.transaction(async (tx: Knex) => {
const validateChallengeResponse = async (challengeId: string): Promise<void> => {
return await acmeChallengeDAL.transaction(async (tx) => {
logger.info({ challengeId }, "Validating ACME challenge response");
const challenge = await acmeChallengeDAL.findByIdForChallengeValidation(challengeId, tx);
if (!challenge) {
@@ -72,9 +73,7 @@ export const pkiAcmeChallengeServiceFactory = ({
} catch (error) {
logger.error(error, "Error validating ACME challenge response");
// TODO: we should retry the challenge validation a few times, but let's keep it simple for now
await acmeChallengeDAL.updateById(challengeId, { status: AcmeChallengeStatus.Invalid }, tx);
await acmeAuthDAL.updateById(challenge.auth.account.id, { status: AcmeAuthStatus.Invalid }, tx);
// TODO: trigger a check for order status as well
await acmeChallengeDAL.markAsValidCascadeById(challengeId, tx);
throw error;
}
});

View File

@@ -611,14 +611,8 @@ export const pkiAcmeServiceFactory = ({
if (!result) {
throw new NotFoundError({ message: "ACME challenge not found" });
}
const challenge = await acmeChallengeDAL.transaction(async (tx) => {
await acmeChallengeService.validateChallengeResponse(challengeId, tx);
return {
...challenge,
...updatedChallenge
};
});
// TODO: Implement ACME challenge response
await acmeChallengeService.validateChallengeResponse(challengeId);
const challenge = (await acmeChallengeDAL.findByIdForChallengeValidation(challengeId))!;
return {
status: 200,
body: {

View File

@@ -1,5 +1,6 @@
import { z } from "zod";
import { TPkiAcmeChallenges } from "@app/db/schemas";
import { JWSHeaderParameters } from "jose";
import {
AcmeOrderResourceSchema,