Skip to content

Commit bc8cb9b

Browse files
authored
fix: default route permission (#20113)
1 parent a675922 commit bc8cb9b

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

server/src/services/auth.service.spec.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -459,18 +459,34 @@ describe(AuthService.name, () => {
459459

460460
mocks.apiKey.getKey.mockResolvedValue({ ...authApiKey, user: authUser });
461461

462-
await expect(
463-
sut.authenticate({
464-
headers: { 'x-api-key': 'auth_token' },
465-
queryParams: {},
466-
metadata: { adminRoute: false, sharedLinkRoute: false, uri: 'test', permission: Permission.AssetRead },
467-
}),
468-
).rejects.toBeInstanceOf(ForbiddenException);
462+
const result = sut.authenticate({
463+
headers: { 'x-api-key': 'auth_token' },
464+
queryParams: {},
465+
metadata: { adminRoute: false, sharedLinkRoute: false, uri: 'test', permission: Permission.AssetRead },
466+
});
467+
468+
await expect(result).rejects.toBeInstanceOf(ForbiddenException);
469+
await expect(result).rejects.toThrow('Missing required permission: asset.read');
470+
});
471+
472+
it('should default to requiring the all permission when omitted', async () => {
473+
const authUser = factory.authUser();
474+
const authApiKey = factory.authApiKey({ permissions: [Permission.AssetRead] });
475+
476+
mocks.apiKey.getKey.mockResolvedValue({ ...authApiKey, user: authUser });
477+
478+
const result = sut.authenticate({
479+
headers: { 'x-api-key': 'auth_token' },
480+
queryParams: {},
481+
metadata: { adminRoute: false, sharedLinkRoute: false, uri: 'test' },
482+
});
483+
await expect(result).rejects.toBeInstanceOf(ForbiddenException);
484+
await expect(result).rejects.toThrow('Missing required permission: all');
469485
});
470486

471487
it('should return an auth dto', async () => {
472488
const authUser = factory.authUser();
473-
const authApiKey = factory.authApiKey({ permissions: [] });
489+
const authApiKey = factory.authApiKey({ permissions: [Permission.All] });
474490

475491
mocks.apiKey.getKey.mockResolvedValue({ ...authApiKey, user: authUser });
476492

server/src/services/auth.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ export class AuthService extends BaseService {
174174

175175
async authenticate({ headers, queryParams, metadata }: ValidateRequest): Promise<AuthDto> {
176176
const authDto = await this.validate({ headers, queryParams });
177-
const { adminRoute, sharedLinkRoute, permission, uri } = metadata;
177+
const { adminRoute, sharedLinkRoute, uri } = metadata;
178+
const requestedPermission = metadata.permission ?? Permission.All;
178179

179180
if (!authDto.user.isAdmin && adminRoute) {
180181
this.logger.warn(`Denied access to admin only route: ${uri}`);
@@ -186,8 +187,8 @@ export class AuthService extends BaseService {
186187
throw new ForbiddenException('Forbidden');
187188
}
188189

189-
if (authDto.apiKey && permission && !isGranted({ requested: [permission], current: authDto.apiKey.permissions })) {
190-
throw new ForbiddenException(`Missing required permission: ${permission}`);
190+
if (authDto.apiKey && !isGranted({ requested: [requestedPermission], current: authDto.apiKey.permissions })) {
191+
throw new ForbiddenException(`Missing required permission: ${requestedPermission}`);
191192
}
192193

193194
return authDto;

0 commit comments

Comments
 (0)