-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
authorization refactor for consistency
- Loading branch information
1 parent
f199d44
commit 0d31a5d
Showing
9 changed files
with
89 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Role } from '@prisma/client'; | ||
|
||
type RoleType = string[] | Role[] | undefined; | ||
|
||
export function authLogic( | ||
userRoles: RoleType, | ||
classRoles: RoleType, | ||
handlerRoles: RoleType | ||
): boolean { | ||
const _userRoles = userRoles ?? []; | ||
const _classRoles = classRoles ?? []; | ||
const _handlerRoles = handlerRoles ?? []; | ||
|
||
// Give super users unlimited access | ||
if (_userRoles.includes(Role.Super)) return true; | ||
|
||
if (_classRoles.length > 0) { | ||
if (!_userRoles.some(r => _classRoles.includes(r as Role))) return false; | ||
if (_handlerRoles.length > 0 && !_userRoles.some(r => _handlerRoles.includes(r))) return false; | ||
} else if (_handlerRoles.length > 0 && !_userRoles.some(r => _handlerRoles.includes(r))) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...pi/src/app/auth/gql/gql-user.decorator.ts → apps/api/src/app/auth/gql-user.decorator.ts
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { Role } from '@prisma/client'; | ||
|
||
import { authLogic } from './auth-logic'; | ||
import { ROLES_KEY } from './roles.decorator'; | ||
|
||
@Injectable() | ||
/** | ||
* Replicates RBAC rules for [ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-6.0). | ||
* **Super** users are granted unlimited access. | ||
*/ | ||
export class GqlGuard extends AuthGuard('jwt') { | ||
constructor(private readonly reflector: Reflector) { | ||
super(); | ||
} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
await super.canActivate(context); | ||
|
||
const ctx = GqlExecutionContext.create(context); | ||
const user = ctx.getContext().req.user; | ||
const classRoles = this.reflector.get<Role[]>(ROLES_KEY, ctx.getClass()); | ||
const handlerRoles = this.reflector.get<Role[]>(ROLES_KEY, ctx.getHandler()); | ||
|
||
return authLogic(user.roles, classRoles, handlerRoles); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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,24 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { Role } from '@prisma/client'; | ||
|
||
import { authLogic } from './auth-logic'; | ||
import { ROLES_KEY } from './roles.decorator'; | ||
|
||
@Injectable() | ||
export class HttpGuard extends AuthGuard('jwt') { | ||
constructor(private readonly reflector: Reflector) { | ||
super(); | ||
} | ||
|
||
async canActivate(ctx: ExecutionContext) { | ||
await super.canActivate(ctx); | ||
|
||
const { user } = ctx.switchToHttp().getRequest(); | ||
const classRoles = this.reflector.get<Role[]>(ROLES_KEY, ctx.getClass()); | ||
const handlerRoles = this.reflector.get<Role[]>(ROLES_KEY, ctx.getHandler()); | ||
|
||
return authLogic(user.roles, classRoles, handlerRoles); | ||
} | ||
} |
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,10 @@ | ||
export { AuthGuard } from '@nestjs/passport'; | ||
export * from './auth.module'; | ||
export * from './gql'; | ||
export { Role } from '@prisma/client'; | ||
export { RequestUser } from './request-user'; | ||
export * from './roles.decorator'; | ||
export * from './auth.module'; | ||
export * from './auth.service'; | ||
export * from './gql-user.decorator'; | ||
export * from './gql.guard'; | ||
export * from './http-user.decorator'; | ||
export { Role } from '@prisma/client'; | ||
export * from './http.guard'; | ||
export * from './roles.decorator'; |
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,4 +1,5 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
import { Role } from '@prisma/client'; | ||
|
||
export const Roles = (...roles: Array<Role>) => SetMetadata('roles', roles); | ||
export const ROLES_KEY = 'roles'; | ||
export const Roles = (...roles: Array<Role>) => SetMetadata(ROLES_KEY, roles); |