Skip to content

Commit 0d573b8

Browse files
committed
feat(example): add example to show usage
1 parent 48fd26c commit 0d573b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1380
-0
lines changed

example/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

example/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

example/.prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

example/.vscode/launch.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/app.js"
12+
},
13+
14+
{
15+
"type": "node",
16+
"name": "vscode-jest-tests",
17+
"request": "launch",
18+
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
19+
"args": ["--runInBand"],
20+
"cwd": "${workspaceFolder}",
21+
"console": "integratedTerminal",
22+
"internalConsoleOptions": "neverOpen"
23+
},
24+
25+
{
26+
"type": "node",
27+
"request": "launch",
28+
"name": "Launch Program",
29+
"program": "${workspaceFolder}/src/main.ts",
30+
"preLaunchTask": "tsc: build - tsconfig.json",
31+
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
32+
},
33+
{
34+
"name": "TS File",
35+
"type": "node",
36+
"request": "launch",
37+
"runtimeArgs": ["-r", "ts-node/register"],
38+
"args": ["${workspaceFolder}/src/main.ts"]
39+
}
40+
]
41+
}

example/.vscode/tasks.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
// 有关 tasks.json 格式的文档,请参见
3+
// https://go.microsoft.com/fwlink/?LinkId=733558
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"type": "typescript",
8+
"tsconfig": "tsconfig.build.json",
9+
"problemMatcher": ["$tsc"]
10+
}
11+
]
12+
}

example/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Description
2+
3+
A demo shows how to use nest-authz to implement role-based access control.
4+
5+
## Installation
6+
7+
```bash
8+
$ npm install
9+
```
10+
11+
## Running the app
12+
13+
```bash
14+
# development
15+
$ npm run start
16+
17+
# watch mode
18+
$ npm run start:dev
19+
20+
# production mode
21+
$ npm run start:prod
22+
```
23+
24+
Open [http://localhost:3000/api](http://localhost:3000/api) to see api.
25+
The api uses Bear auth schema, so if you want to access protected api, you should click the `Authorize` button and set `Bearer yourjwttoken` after login.
26+
27+
## Test
28+
29+
```bash
30+
# unit tests
31+
$ npm run test
32+
```
33+
34+
## License
35+
36+
[MIT licensed](LICENSE).

example/model.conf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub, obj, act
6+
7+
[role_definition]
8+
g = _, _
9+
g2 = _, _
10+
11+
[policy_effect]
12+
e = some(where (p.eft == allow))
13+
14+
[matchers]
15+
m = g(r.sub, p.sub) && g2(r.obj, p.obj) && r.act == p.act || r.sub == "root"

example/nest-cli.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"language": "ts",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src"
5+
}

example/nodemon-debug.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"watch": ["src"],
3+
"ext": "ts",
4+
"ignore": ["src/**/*.spec.ts"],
5+
"exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts"
6+
}

example/nodemon.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"watch": ["src"],
3+
"ext": "ts",
4+
"ignore": ["src/**/*.spec.ts"],
5+
"exec": "ts-node -r tsconfig-paths/register src/main.ts"
6+
}

example/package.json

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"name": "example",
3+
"version": "0.0.1",
4+
"description": "",
5+
"author": "",
6+
"license": "MIT",
7+
"scripts": {
8+
"build": "tsc -p tsconfig.build.json",
9+
"format": "prettier --write \"src/**/*.ts\"",
10+
"start": "ts-node -r tsconfig-paths/register src/main.ts",
11+
"start:dev": "nodemon",
12+
"start:debug": "nodemon --config nodemon-debug.json",
13+
"prestart:prod": "rimraf dist && npm run build",
14+
"start:prod": "node dist/main.js",
15+
"lint": "tslint -p tsconfig.json -c tslint.json",
16+
"test": "jest",
17+
"test:watch": "jest --watch",
18+
"test:cov": "jest --coverage",
19+
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20+
"test:e2e": "jest --config ./test/jest-e2e.json"
21+
},
22+
"dependencies": {
23+
"@nestjs/common": "^6.0.0",
24+
"@nestjs/core": "^6.0.0",
25+
"@nestjs/jwt": "^6.0.0",
26+
"@nestjs/passport": "^6.0.0",
27+
"@nestjs/platform-express": "^6.0.0",
28+
"@nestjs/swagger": "^3.0.2",
29+
"class-transformer": "^0.2.0",
30+
"class-validator": "^0.9.1",
31+
"dotenv": "^7.0.0",
32+
"passport": "^0.4.0",
33+
"passport-jwt": "^4.0.0",
34+
"reflect-metadata": "^0.1.12",
35+
"rimraf": "^2.6.2",
36+
"rxjs": "^6.3.3",
37+
"swagger-ui-express": "^4.0.2",
38+
"uuid": "^3.3.2"
39+
},
40+
"devDependencies": {
41+
"@nestjs/testing": "^6.0.0",
42+
"@types/express": "^4.16.0",
43+
"@types/jest": "^23.3.13",
44+
"@types/node": "^10.12.18",
45+
"@types/passport-jwt": "^3.0.1",
46+
"@types/supertest": "^2.0.7",
47+
"@types/uuid": "^3.4.4",
48+
"casbin": "^2.0.1",
49+
"jest": "^23.6.0",
50+
"nodemon": "^1.18.9",
51+
"prettier": "^1.15.3",
52+
"supertest": "^3.4.1",
53+
"ts-jest": "^23.10.5",
54+
"ts-node": "^7.0.1",
55+
"tsconfig-paths": "^3.7.0",
56+
"tslint": "5.12.1",
57+
"typescript": "^3.2.4"
58+
},
59+
"jest": {
60+
"moduleFileExtensions": [
61+
"js",
62+
"json",
63+
"ts"
64+
],
65+
"roots": [
66+
"./test",
67+
"./src"
68+
],
69+
"testRegex": ".spec.ts$",
70+
"transform": {
71+
"^.+\\.(t|j)s$": "ts-jest"
72+
},
73+
"coverageDirectory": "../coverage",
74+
"testEnvironment": "node"
75+
}
76+
}

example/policy.csv

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
p, superuser, user, read:any
2+
p, manager, user_roles, read:any
3+
p, guest, user, read:own
4+
5+
g, alice, superuser
6+
g, bob, guest
7+
g, tom, manager
8+
9+
g, users_list, user
10+
g, user_roles, user
11+
g, user_permissions, user
12+
g, roles_list, role
13+
g, role_permissions, role

example/src/app.module.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Module, ExecutionContext } from '@nestjs/common';
2+
import { AppController } from './controllers/app.controller';
3+
import { UserController } from './controllers/user.controller';
4+
import { RoleController } from './controllers/role.controller';
5+
import { AuthController } from './controllers/auth.controller';
6+
import { UserPermissionController } from './controllers/user-permission.controller';
7+
import { UserRoleController } from './controllers/user-role.controller';
8+
9+
import { AuthZModule } from '../../src';
10+
import { PassportModule } from '@nestjs/passport';
11+
import { JwtModule } from '@nestjs/jwt';
12+
13+
import { AuthService, UserService, RoleService, JwtStrategy } from './services';
14+
15+
@Module({
16+
imports: [
17+
PassportModule.register({
18+
defaultStrategy: 'jwt',
19+
}),
20+
JwtModule.register({
21+
secretOrPrivateKey: 'secretKey',
22+
}),
23+
AuthZModule.register({
24+
model: 'model.conf',
25+
policy: 'policy.csv',
26+
usernameFromContext: (ctx: ExecutionContext) => {
27+
const request = ctx.switchToHttp().getRequest();
28+
return request.user && request.user.username;
29+
},
30+
}),
31+
],
32+
controllers: [
33+
AppController,
34+
AuthController,
35+
UserController,
36+
RoleController,
37+
UserRoleController,
38+
UserPermissionController,
39+
],
40+
providers: [AuthService, UserService, JwtStrategy, RoleService],
41+
})
42+
export class AppModule {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
3+
import { Resources } from '../resources';
4+
5+
@Controller()
6+
export class AppController {
7+
@Get()
8+
root() {
9+
return 'Hello World!';
10+
}
11+
12+
@Get('/resources')
13+
resources() {
14+
return Resources;
15+
}
16+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Controller, Post, Body } from '@nestjs/common';
2+
import { AuthService } from '../services/auth.service';
3+
4+
import { ApiUseTags, ApiOperation } from '@nestjs/swagger';
5+
import { LoginInput } from '../dto/login.input';
6+
import { RegisterInput } from 'src/dto/register.input';
7+
import { User } from '../interfaces';
8+
import uuid = require('uuid');
9+
10+
@ApiUseTags('Auth')
11+
@Controller('auth')
12+
export class AuthController {
13+
constructor(private readonly authSrv: AuthService) {}
14+
15+
@ApiOperation({
16+
title: 'User login',
17+
})
18+
@Post('login')
19+
async login(@Body() credentials: LoginInput) {
20+
return this.authSrv.login(credentials.username, credentials.password);
21+
}
22+
23+
@ApiOperation({
24+
title: 'User register',
25+
})
26+
@Post('register')
27+
async register(@Body() userDto: RegisterInput) {
28+
const data: User = Object.assign({ id: uuid.v1() }, userDto);
29+
return this.authSrv.register(data);
30+
}
31+
}

0 commit comments

Comments
 (0)