Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added route to disable API Key #282

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/api/routers/participantsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { isApproverCheck } from '../middleware/approversMiddleware';
import {
addKeyPair,
createApiKey,
disableApiKey,
getApiKeysBySite,
getKeyPairsList,
getSharingList,
Expand Down Expand Up @@ -374,6 +375,45 @@ export function createParticipantsRouter() {
}
);

const apiKeyDeleteInputParser = z.object({
keyId: z.string(),
});
participantsRouter.delete(
'/:participantId/apiKey',
async (req: ParticipantRequest, res: Response) => {
const { participant } = req;
if (!participant?.siteId) {
return res.status(400).send('Site id is not set');
}

const { keyId } = apiKeyDeleteInputParser.parse(req.body);

const apiKey = await getApiKey(participant.siteId, keyId);
if (!apiKey) {
return res.status(404).send('KeyId was invalid');
}

const traceId = getTraceId(req);
const currentUser = await findUserByEmail(req.auth?.payload?.email as string);
const auditTrail = await insertManageApiKeyAuditTrail(
participant!,
currentUser!.id,
currentUser!.email,
AuditAction.Delete,
apiKey.name,
apiKey.roles.map((role) => role.roleName),
traceId,
apiKey.key_id
);

await disableApiKey(apiKey.contact);

await updateAuditTrailToProceed(auditTrail.id);

return res.sendStatus(200);
}
);

participantsRouter.get(
'/:participantId/apiRoles',
async (req: ParticipantRequest, res: Response) => {
Expand Down
8 changes: 8 additions & 0 deletions src/api/services/adminServiceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ export const updateApiKeyRoles = async (contact: string, apiRoles: string[]): Pr
});
};

export const disableApiKey = async (contact: string): Promise<void> => {
await adminServiceClient.post('/api/client/disable', null, {
params: {
contact,
},
});
};

export const getVisibleSiteList = async (): Promise<SiteDTO[]> => {
const siteList = await getSiteList();
return siteList.filter((x) => x.visible !== false);
Expand Down
Loading