mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-22 15:24:55 +00:00
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import BadDataException from "Common/Types/Exception/BadDataException";
|
|
import {
|
|
ExpressRequest,
|
|
ExpressResponse,
|
|
NextFunction,
|
|
} from "../Utils/Express";
|
|
import CopilotCodeRepository from "Common/Models/DatabaseModels/CopilotCodeRepository";
|
|
import CopilotCodeRepositoryService from "../Services/CopilotCodeRepositoryService";
|
|
import ObjectID from "Common/Types/ObjectID";
|
|
|
|
export default class CopilotCodeRepositoryAuthorization {
|
|
public static async isAuthorizedRepository(
|
|
req: ExpressRequest,
|
|
_res: ExpressResponse,
|
|
next: NextFunction,
|
|
): Promise<void> {
|
|
try {
|
|
const secretkey: string = req.params["secretkey"]!;
|
|
|
|
if (!secretkey) {
|
|
throw new BadDataException("Secret key is required");
|
|
}
|
|
|
|
const CopilotCodeRepository: CopilotCodeRepository | null =
|
|
await CopilotCodeRepositoryService.findOneBy({
|
|
query: {
|
|
secretToken: new ObjectID(secretkey),
|
|
},
|
|
select: {
|
|
_id: true,
|
|
},
|
|
props: {
|
|
isRoot: true,
|
|
},
|
|
});
|
|
|
|
if (!CopilotCodeRepository) {
|
|
throw new BadDataException(
|
|
"Code repository not found. Secret key is invalid.",
|
|
);
|
|
}
|
|
|
|
next();
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
}
|
|
}
|