Skip to content

Commit c4f36b7

Browse files
committed
fix(ssh): add proper TypeScript types to SSH key route params
1 parent be7759a commit c4f36b7

1 file changed

Lines changed: 59 additions & 53 deletions

File tree

src/service/routes/users.ts

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,40 @@ router.get('/:id', async (req: Request<{ id: string }>, res: Response) => {
4747
});
4848

4949
// Get SSH key fingerprints for a user
50-
router.get('/:username/ssh-key-fingerprints', async (req: Request, res: Response) => {
51-
if (!req.user) {
52-
res.status(401).json({ error: 'Authentication required' });
53-
return;
54-
}
50+
router.get(
51+
'/:username/ssh-key-fingerprints',
52+
async (req: Request<{ username: string }>, res: Response) => {
53+
if (!req.user) {
54+
res.status(401).json({ error: 'Authentication required' });
55+
return;
56+
}
5557

56-
const { username, admin } = req.user as { username: string; admin: boolean };
57-
const targetUsername = req.params.username.toLowerCase();
58+
const { username, admin } = req.user as { username: string; admin: boolean };
59+
const targetUsername = req.params.username.toLowerCase();
5860

59-
// Only allow users to view their own keys, or admins to view any keys
60-
if (username !== targetUsername && !admin) {
61-
res.status(403).json({ error: 'Not authorized to view keys for this user' });
62-
return;
63-
}
61+
// Only allow users to view their own keys, or admins to view any keys
62+
if (username !== targetUsername && !admin) {
63+
res.status(403).json({ error: 'Not authorized to view keys for this user' });
64+
return;
65+
}
6466

65-
try {
66-
const publicKeys = await db.getPublicKeys(targetUsername);
67-
const keyFingerprints = publicKeys.map((keyRecord) => ({
68-
fingerprint: keyRecord.fingerprint,
69-
name: keyRecord.name,
70-
addedAt: keyRecord.addedAt,
71-
}));
72-
res.json(keyFingerprints);
73-
} catch (error) {
74-
console.error('Error retrieving SSH keys:', error);
75-
res.status(500).json({ error: 'Failed to retrieve SSH keys' });
76-
}
77-
});
67+
try {
68+
const publicKeys = await db.getPublicKeys(targetUsername);
69+
const keyFingerprints = publicKeys.map((keyRecord) => ({
70+
fingerprint: keyRecord.fingerprint,
71+
name: keyRecord.name,
72+
addedAt: keyRecord.addedAt,
73+
}));
74+
res.json(keyFingerprints);
75+
} catch (error) {
76+
console.error('Error retrieving SSH keys:', error);
77+
res.status(500).json({ error: 'Failed to retrieve SSH keys' });
78+
}
79+
},
80+
);
7881

7982
// Add SSH public key
80-
router.post('/:username/ssh-keys', async (req: Request, res: Response) => {
83+
router.post('/:username/ssh-keys', async (req: Request<{ username: string }>, res: Response) => {
8184
if (!req.user) {
8285
res.status(401).json({ error: 'Authentication required' });
8386
return;
@@ -137,36 +140,39 @@ router.post('/:username/ssh-keys', async (req: Request, res: Response) => {
137140
});
138141

139142
// Remove SSH public key by fingerprint
140-
router.delete('/:username/ssh-keys/:fingerprint', async (req: Request, res: Response) => {
141-
if (!req.user) {
142-
res.status(401).json({ error: 'Authentication required' });
143-
return;
144-
}
145-
146-
const { username, admin } = req.user as { username: string; admin: boolean };
147-
const targetUsername = req.params.username.toLowerCase();
148-
const fingerprint = req.params.fingerprint;
143+
router.delete(
144+
'/:username/ssh-keys/:fingerprint',
145+
async (req: Request<{ username: string; fingerprint: string }>, res: Response) => {
146+
if (!req.user) {
147+
res.status(401).json({ error: 'Authentication required' });
148+
return;
149+
}
149150

150-
// Only allow users to remove keys from their own account, or admins to remove from any account
151-
if (username !== targetUsername && !admin) {
152-
res.status(403).json({ error: 'Not authorized to remove keys for this user' });
153-
return;
154-
}
151+
const { username, admin } = req.user as { username: string; admin: boolean };
152+
const targetUsername = req.params.username.toLowerCase();
153+
const fingerprint = req.params.fingerprint;
155154

156-
console.log('Removing SSH key', { targetUsername, fingerprint });
157-
try {
158-
await db.removePublicKey(targetUsername, fingerprint);
159-
res.status(200).json({ message: 'SSH key removed successfully' });
160-
} catch (error: any) {
161-
console.error('Error removing SSH key:', error);
155+
// Only allow users to remove keys from their own account, or admins to remove from any account
156+
if (username !== targetUsername && !admin) {
157+
res.status(403).json({ error: 'Not authorized to remove keys for this user' });
158+
return;
159+
}
162160

163-
// Return specific error message
164-
if (error.message === 'User not found') {
165-
res.status(404).json({ error: 'User not found' });
166-
} else {
167-
res.status(500).json({ error: error.message || 'Failed to remove SSH key' });
161+
console.log('Removing SSH key', { targetUsername, fingerprint });
162+
try {
163+
await db.removePublicKey(targetUsername, fingerprint);
164+
res.status(200).json({ message: 'SSH key removed successfully' });
165+
} catch (error: any) {
166+
console.error('Error removing SSH key:', error);
167+
168+
// Return specific error message
169+
if (error.message === 'User not found') {
170+
res.status(404).json({ error: 'User not found' });
171+
} else {
172+
res.status(500).json({ error: error.message || 'Failed to remove SSH key' });
173+
}
168174
}
169-
}
170-
});
175+
},
176+
);
171177

172178
export default router;

0 commit comments

Comments
 (0)