-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First phase of authorization: simple rules
next step: - casl + prisma (or other supported orms) to avoid weird logic, and to filter the resource for the rows that are accessible to the current user - oauth0 service to verify that some fields match the user's data on user creation
- Loading branch information
1 parent
a96ccac
commit e0d1da1
Showing
13 changed files
with
186 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import { Role } from '@hkrecruitment/shared'; | ||
import { AppAbility, UserAuth } from '@hkrecruitment/shared'; | ||
|
||
export type AuthenticatedRequest = Request & { | ||
user: { | ||
sub: string; | ||
role: Role; | ||
}; | ||
user: UserAuth; | ||
ability: AppAbility; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,36 @@ | ||
import { CanActivate, ExecutionContext, Injectable, Logger } from '@nestjs/common'; | ||
import { abilityForUser } from '@hkrecruitment/shared'; | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { UsersService } from 'src/users/users.service'; | ||
import { CHECK_POLICIES_KEY, PolicyHandler } from './check-policies.decorator'; | ||
|
||
@Injectable() | ||
export class AuthorizationGuard implements CanActivate { | ||
private readonly logger = new Logger(AuthorizationGuard.name); | ||
|
||
constructor(private readonly usersService: UsersService) {} | ||
constructor( | ||
private reflector: Reflector, | ||
private readonly usersService: UsersService, | ||
) {} | ||
|
||
async canActivate( | ||
context: ExecutionContext, | ||
): Promise<boolean> { | ||
const role = await this.usersService.getRoleForOauthId(context.switchToHttp().getRequest().user.sub) ?? 'none'; | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const role = | ||
(await this.usersService.getRoleForOauthId( | ||
context.switchToHttp().getRequest().user.sub, | ||
)) ?? 'none'; | ||
context.switchToHttp().getRequest().user.role = role; | ||
this.logger.log(`user.sub: ${context.switchToHttp().getRequest().user.sub} -> ${role}}`); | ||
return true; | ||
this.logger.log( | ||
`user.sub: ${context.switchToHttp().getRequest().user.sub} -> ${role}}`, | ||
); | ||
const ability = abilityForUser(context.switchToHttp().getRequest().user); | ||
context.switchToHttp().getRequest().ability = ability; | ||
|
||
const handlers = this.reflector.get<PolicyHandler[]>(CHECK_POLICIES_KEY, context.getHandler()) ?? []; | ||
return handlers.every((handler) => handler(ability)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { AppAbility } from '@hkrecruitment/shared'; | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
export type PolicyHandler = (ability: AppAbility) => boolean; | ||
|
||
export const CHECK_POLICIES_KEY = 'check-policies'; | ||
export const CheckPolicies = (...args: PolicyHandler[]) => SetMetadata(CHECK_POLICIES_KEY, args); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { AbilityBuilder, AbilityClass, createMongoAbility, PureAbility, subject } from "@casl/ability"; | ||
import { applyAbilitesOnPerson, Person, Role } from "./person"; | ||
|
||
export interface UserAuth { | ||
sub: string; | ||
role: Role; | ||
} | ||
|
||
export enum Action { | ||
Manage = "manage", | ||
Create = "create", | ||
Read = "read", | ||
Update = "update", | ||
Delete = "delete", | ||
} | ||
type SubjectsTypes = Partial<Person>; | ||
type SubjectNames = 'Person'; | ||
export type Subjects = SubjectsTypes | SubjectNames; | ||
|
||
export type AppAbility = PureAbility<[Action, Subjects]>; | ||
export const AppAbility = PureAbility as AbilityClass<AppAbility>; | ||
|
||
export type ApplyAbilities = ( | ||
user: UserAuth, | ||
{ can, cannot }: AbilityBuilder<AppAbility> | ||
) => void; | ||
|
||
export const abilityForUser = (user: UserAuth): AppAbility => { | ||
const builder = new AbilityBuilder<AppAbility>(createMongoAbility); | ||
|
||
applyAbilitesOnPerson(user, builder); | ||
|
||
const { build } = builder; | ||
return build(); | ||
}; | ||
|
||
export const canContinue = (ability: AppAbility, Action: Action, subjectObj: SubjectsTypes, subjectName: SubjectNames): boolean => { | ||
const subj = subject(subjectName, subjectObj); | ||
return ability.can(Action, subj) && Object.keys(subject).every(field => ability.can(Action, subj, field)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./person"; | ||
export * from "./user-auth"; | ||
export * from "./abilities"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
e0d1da1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably using typeorm forever is enough. CASL author doesn't like typeorm, he likes MikroORM and said he would create an integration with it. However at the moment the only official integration is with Prisma (a framework that uses codegen, so it's a excessive for me).
https://casl.js.org/v6/en/advanced/ability-to-database-query could be the answer, hopefully it's possible to add rules to all typeorm queries using this helper functions