Skip to content

Commit

Permalink
fix: assign all abilities to user in test mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Feb 28, 2024
1 parent 028215b commit 10e7620
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
29 changes: 20 additions & 9 deletions packages/server-core/src/http/middleware/authup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { parseAuthorizationHeader } from 'hapic';
import { coreHandler, getRequestHeader } from 'routup';
import { useRequestCookie } from '@routup/basic/cookie';
import type { Request, Router } from 'routup';
import { useEnv } from '../../config';
import { EnvironmentName, useEnv } from '../../config';
import {
hasAuthupClient, hasRedisClient, hasVaultClient, useAuthupClient, useRedisClient,
} from '../../core';
Expand All @@ -31,6 +31,16 @@ TokenVerificationData,
'sub_name'
>;

function generateAbilities() : Ability[] {
return Object.values(PermissionID).map((name) => ({
name,
condition: null,
inverse: false,
power: 999,
target: null,
} satisfies Ability));
}

function createFakeTokenVerificationData() : TokenVerificationDataMinimal {
return {
realm_id: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee',
Expand All @@ -40,21 +50,22 @@ function createFakeTokenVerificationData() : TokenVerificationDataMinimal {
sub: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee',
sub_name: 'admin',

permissions: Object.values(PermissionID).map((name) => ({
name,
condition: null,
inverse: false,
power: 999,
target: null,
} satisfies Ability)),
permissions: generateAbilities(),
};
}

function applyTokenVerificationData(
req: Request,
data: TokenVerificationDataMinimal,
) {
const ability = new AbilityManager(data.permissions);
let abilities : Ability[];
if (useEnv('env') === EnvironmentName.TEST) {
abilities = generateAbilities();
} else {
abilities = data.permissions;
}

const ability = new AbilityManager(abilities);
setRequestEnv(req, 'ability', ability);

setRequestEnv(req, 'realmId', data.realm_id);
Expand Down
1 change: 0 additions & 1 deletion packages/server-core/src/http/middleware/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { useLogger } from '../../config';

export function registerErrorHandler(router: Router) {
router.use(errorHandler((error, req, res) => {
console.log(error.cause);
// catch and decorate some db errors :)
switch (error.code) {
case 'ER_DUP_ENTRY':
Expand Down

0 comments on commit 10e7620

Please sign in to comment.