-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Feat/permission decorator): Add permissions for specific GraphQL…
… fields (#36) * Feat: Implement graphql query guard - Can add inaccessible field from query to block attack request * Feat: Remove has count type * Feat: Add check permission * Chore: Remove test code * Chore: Move md explanation * Chore: Change name of class * Docs: Readme.md * Docs: Add note about permission guard * Fix: typo
- Loading branch information
Showing
7 changed files
with
113 additions
and
17 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
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,20 @@ | ||
import { SetMetadata, UseGuards, applyDecorators } from '@nestjs/common'; | ||
|
||
import { FindOptionsSelect } from 'typeorm'; | ||
|
||
import { GraphqlQueryPermissionGuard } from '../guards/graphql-query-permission.guard'; | ||
|
||
export type ClassConstructor<T = unknown> = new (...args: unknown[]) => T; | ||
|
||
export const PERMISSION = Symbol('PERMISSION'); | ||
export const INSTANCE = Symbol('INSTANCE'); | ||
|
||
export const UseQueryPermissionGuard = <T extends ClassConstructor>( | ||
instance: T, | ||
permission: FindOptionsSelect<InstanceType<T>>, | ||
) => | ||
applyDecorators( | ||
SetMetadata(INSTANCE, instance), | ||
SetMetadata(PERMISSION, permission), | ||
UseGuards(GraphqlQueryPermissionGuard<T>), | ||
); |
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,57 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
|
||
import { DataSource, FindOptionsSelect } from 'typeorm'; | ||
|
||
import { | ||
getCurrentGraphQLQuery, | ||
getOptionFromGqlQuery, | ||
} from '../decorators/option.decorator'; | ||
import { | ||
ClassConstructor, | ||
INSTANCE, | ||
PERMISSION, | ||
} from '../decorators/query-guard.decorator'; | ||
import { GetInfoFromQueryProps } from '../graphql/utils/types'; | ||
|
||
const checkPermission = <T extends ClassConstructor>( | ||
permission: FindOptionsSelect<InstanceType<T>>, | ||
select: FindOptionsSelect<InstanceType<T>>, | ||
): boolean => { | ||
return Object.entries(permission) | ||
.filter((v) => !!v[1]) | ||
.every(([key, value]) => { | ||
if (typeof value === 'boolean') { | ||
return select[key] ? false : true; | ||
} | ||
|
||
return checkPermission(value, select[key]); | ||
}); | ||
}; | ||
|
||
@Injectable() | ||
export class GraphqlQueryPermissionGuard<T extends ClassConstructor> { | ||
constructor( | ||
private reflector: Reflector, | ||
private readonly dataSource: DataSource, | ||
) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const permission = this.reflector.get<FindOptionsSelect<InstanceType<T>>>( | ||
PERMISSION, | ||
context.getHandler(), | ||
); | ||
|
||
const entity = this.reflector.get<T>(INSTANCE, context.getHandler()); | ||
const repository = this.dataSource.getRepository<T>(entity); | ||
|
||
const ctx = GqlExecutionContext.create(context); | ||
const query = getCurrentGraphQLQuery(ctx); | ||
|
||
const { select }: GetInfoFromQueryProps<InstanceType<T>> = | ||
getOptionFromGqlQuery.call(repository, query); | ||
|
||
return checkPermission<T>(permission, select); | ||
} | ||
} |
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