-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from conceptadev/feature/access-control-passw…
…ord-enforcement feat: access control refactor for user password enforement
- Loading branch information
Showing
87 changed files
with
1,521 additions
and
1,374 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
58 changes: 58 additions & 0 deletions
58
packages/nestjs-access-control/src/access-control.context.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { AccessControl } from 'accesscontrol'; | ||
import { mock } from 'jest-mock-extended'; | ||
import { Controller } from '@nestjs/common'; | ||
import { ExecutionContext, HttpArgumentsHost } from '@nestjs/common/interfaces'; | ||
import { AccessControlContext } from './access-control.context'; | ||
import { AccessControlReadOne } from './decorators/access-control-read-one.decorator'; | ||
import { ActionEnum } from './enums/action.enum'; | ||
import { PossessionEnum } from './enums/possession.enum'; | ||
|
||
describe(AccessControlContext.name, () => { | ||
it('should return expected values', () => { | ||
@Controller() | ||
@AccessControlReadOne('a_resource_name') | ||
class TestController {} | ||
|
||
const rules = new AccessControl(); | ||
rules.grant('role1').readAny('a_resource_name'); | ||
|
||
const argsHost = mock<HttpArgumentsHost>(); | ||
argsHost.getRequest.mockReturnValue({ body: { b1: 'xyz' } }); | ||
|
||
const context = mock<ExecutionContext>(); | ||
context.getClass.mockReturnValue(TestController); | ||
context.switchToHttp.mockReturnValue(argsHost); | ||
|
||
const expectedAccessControlContext = new AccessControlContext({ | ||
request: { | ||
body: { b1: 'xyz' }, | ||
}, | ||
user: { id: 1234 }, | ||
query: { | ||
possession: PossessionEnum.OWN, | ||
resource: 'resource_create_own', | ||
action: ActionEnum.READ, | ||
}, | ||
accessControl: rules, | ||
executionContext: context, | ||
}); | ||
|
||
expect(expectedAccessControlContext.getRequest()).toEqual({ | ||
body: { b1: 'xyz' }, | ||
}); | ||
expect(expectedAccessControlContext.getRequest('body')).toEqual({ | ||
b1: 'xyz', | ||
}); | ||
expect(expectedAccessControlContext.getRequest('not-a-real-prop')).toEqual( | ||
undefined, | ||
); | ||
expect(expectedAccessControlContext.getUser()).toEqual({ id: 1234 }); | ||
expect(expectedAccessControlContext.getQuery()).toEqual({ | ||
possession: PossessionEnum.OWN, | ||
resource: 'resource_create_own', | ||
action: ActionEnum.READ, | ||
}); | ||
expect(expectedAccessControlContext.getAccessControl()).toEqual(rules); | ||
expect(expectedAccessControlContext.getExecutionContext()).toEqual(context); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/nestjs-access-control/src/access-control.context.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { AccessControl, IQueryInfo } from 'accesscontrol'; | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { AccessControlContextArgsInterface } from './interfaces/access-control-context-args.interface'; | ||
import { AccessControlContextInterface } from './interfaces/access-control-context.interface'; | ||
|
||
export class AccessControlContext implements AccessControlContextInterface { | ||
constructor(private readonly ctxArgs: AccessControlContextArgsInterface) {} | ||
|
||
protected hasProp<K extends string>( | ||
obj: unknown, | ||
key: K, | ||
): obj is Record<K, unknown> { | ||
return ( | ||
key !== null && obj !== null && typeof obj === 'object' && key in obj | ||
); | ||
} | ||
|
||
protected getProp(obj: unknown, prop: string) { | ||
return this.hasProp(obj, prop) ? obj[prop] : undefined; | ||
} | ||
|
||
getRequest(property?: string): unknown { | ||
return property?.length | ||
? this.getProp(this.ctxArgs.request, property) | ||
: this.ctxArgs.request; | ||
} | ||
|
||
getUser(): unknown { | ||
return this.ctxArgs.user; | ||
} | ||
|
||
getQuery(): IQueryInfo { | ||
return this.ctxArgs.query; | ||
} | ||
|
||
getAccessControl(): AccessControl { | ||
return this.ctxArgs.accessControl; | ||
} | ||
|
||
getExecutionContext(): ExecutionContext { | ||
return this.ctxArgs.executionContext; | ||
} | ||
} |
Oops, something went wrong.