Skip to content

Commit fc769d2

Browse files
committed
fix(api): incorrect user permissions extraction
1 parent 6a61fc4 commit fc769d2

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

modules/services/api/src/auth/decorators/authentication.decorator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { UserAuthentication, VidyaRequest } from '@vidya/api/auth/utils';
44
export const Authentication = createParamDecorator(
55
(data, ctx: ExecutionContext) => {
66
const request = ctx.switchToHttp().getRequest<VidyaRequest>();
7-
const accessToken = request.accessToken;
8-
return new UserAuthentication(accessToken);
7+
return new UserAuthentication(request.accessToken);
98
},
109
);

modules/services/api/src/auth/guards/authenticated-user.guard.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ export class AuthenticatedUserGuard implements CanActivate {
4848
throw new UnauthorizedException();
4949
}
5050

51-
// Attach the user id and permissions to the request object
52-
request.userId = accessToken.sub;
53-
request.accessToken = accessToken;
54-
request.userPermissions = accessToken.permissions
51+
// Create final access token object with user permissions
52+
// (if not already present in the token)
53+
const userPermissions = accessToken.permissions
5554
? accessToken.permissions
5655
: await this.usersService.getUserPermissions(accessToken.sub);
56+
request.accessToken = {
57+
...accessToken,
58+
permissions: userPermissions,
59+
};
5760
} catch {
5861
throw new UnauthorizedException();
5962
}

modules/services/api/src/auth/utils/request.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ import { Request } from 'express';
33

44
export type VidyaRequest = Request & {
55
accessToken: AccessToken;
6-
userPermissions: UserPermission[];
7-
userId: string;
86
};

modules/services/api/src/auth/utils/user-permissions.util.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,21 @@ export class UserAuthentication {
3232
}
3333

3434
export class AuthenticatedUserPermissions {
35-
constructor(private readonly _permissions: protocol.UserPermission[]) {}
35+
constructor(private readonly _userPermissions: protocol.UserPermission[]) {}
3636

37+
/**
38+
* Get list of scopes that user has permission for
39+
* @param requiredPermissions Permissions required to perform an action
40+
* @returns List of scopes that user has permission for
41+
*/
3742
public getScopes(
38-
permissions: domain.PermissionKey[],
43+
requiredPermissions: domain.PermissionKey[],
3944
): { schoolId: string }[] {
40-
return this._permissions
45+
return this._userPermissions
4146
.filter((p) =>
42-
permissions.every((permission) => p.p.includes(permission)),
47+
requiredPermissions.every(
48+
(permission) => p.p.includes(permission) || p.p.includes('*'),
49+
),
4350
)
4451
.map((p) => ({ schoolId: p.sid }));
4552
}

modules/services/database/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './main'

0 commit comments

Comments
 (0)