Skip to content

Commit

Permalink
Require a freshly authenticated JWT when creating an api_key
Browse files Browse the repository at this point in the history
Change-type: major
  • Loading branch information
thgreasi committed May 30, 2024
1 parent b6988c2 commit fab43ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/features/auth/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './create-application-actor.js';
import './create-device-actor.js';
import './create-user-actor.js';
import './fetch-api-key.js';
import './restrict-api-key-creation.js';
import './restrict-user-deletion.js';
import './update-jwt-secret.js';
import './validate-username-email.js';
15 changes: 15 additions & 0 deletions src/features/auth/hooks/restrict-api-key-creation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { hooks, errors } from '@balena/pinejs';

import { getUser } from '../../../infra/auth/auth.js';
import { checkSudoValidity } from '../../../infra/auth/jwt.js';

const { UnauthorizedError } = errors;

hooks.addPureHook('POST', 'resin', 'api_key', {
PRERUN: async ({ req, tx }) => {
const user = await getUser(req, tx);
if (!(await checkSudoValidity(user))) {
throw new UnauthorizedError('Fresh authentication token required');
}
},
});

0 comments on commit fab43ac

Please sign in to comment.