nest-authz
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,5 +0,29 @@ | ||
# Change Log | ||
# Changelog | ||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. | ||
## [1.1.0](https://github.com/dreamdevil00/nest-authz/compare/v1.0.0...v1.1.0) (2020-01-03) | ||
### Features | ||
* add module options to allow dynamic configure enforcer ([#42](https://github.com/dreamdevil00/nest-authz/issues/42)) ([d03472f](https://github.com/dreamdevil00/nest-authz/commit/d03472f83a5d223e6b441c4a69c766c454395e73)) | ||
* **options:** add module options ([765df74](https://github.com/dreamdevil00/nest-authz/commit/765df745646fc25f4984808e513df16b8cf70893)) | ||
### Bug Fixes | ||
* **deps:** pin dependencies ([07f5668](https://github.com/dreamdevil00/nest-authz/commit/07f56681555d2c082ab669b89ec611fa841bbc82)) | ||
* **deps:** update dependency @nestjs/passport to v6.1.1 ([e48df14](https://github.com/dreamdevil00/nest-authz/commit/e48df141a6a8214d0d7509dc23363891ad1384a3)) | ||
* **deps:** update dependency @nestjs/passport to v6.1.1 ([#21](https://github.com/dreamdevil00/nest-authz/issues/21)) ([df4ee95](https://github.com/dreamdevil00/nest-authz/commit/df4ee952c2ca418bb9dccc067801bb6ca13e0692)) | ||
* **deps:** update dependency @nestjs/swagger to v4 ([f91ce80](https://github.com/dreamdevil00/nest-authz/commit/f91ce8063cd786f0054e9f0ff6c11fdf2a4ed866)) | ||
* **deps:** update dependency @nestjs/swagger to v4.1.2 ([05d18aa](https://github.com/dreamdevil00/nest-authz/commit/05d18aa4786e82adf92a165a269c2ac8ad90c102)) | ||
* **deps:** update dependency @nestjs/swagger to v4.1.2 ([#26](https://github.com/dreamdevil00/nest-authz/issues/26)) ([b16ab8d](https://github.com/dreamdevil00/nest-authz/commit/b16ab8d10753f1f0cf871e0f34a52ff2b02bba8d)) | ||
* **deps:** update dependency class-validator to v0.11.0 ([7fe2dac](https://github.com/dreamdevil00/nest-authz/commit/7fe2dacc43847708403da2733dcb563866ce2cfe)) | ||
* **deps:** update dependency class-validator to v0.11.0 ([#5](https://github.com/dreamdevil00/nest-authz/issues/5)) ([c91051c](https://github.com/dreamdevil00/nest-authz/commit/c91051cc324fb426959afcfce82905102ea2b81b)) | ||
* **deps:** update dependency dotenv to v8 ([d0fa313](https://github.com/dreamdevil00/nest-authz/commit/d0fa3137f8db1cf4ad4af734d91a4c594452789f)) | ||
* **deps:** update dependency dotenv to v8 ([#14](https://github.com/dreamdevil00/nest-authz/issues/14)) ([8b777b9](https://github.com/dreamdevil00/nest-authz/commit/8b777b9ca056d538671a922f386e7ce54fe3ea32)) | ||
* **deps:** update dependency passport to v0.4.1 ([fb57896](https://github.com/dreamdevil00/nest-authz/commit/fb57896338ef430d1dfb0429eeb07438cfb7fb5d)) | ||
* **deps:** update dependency passport to v0.4.1 ([#30](https://github.com/dreamdevil00/nest-authz/issues/30)) ([6239013](https://github.com/dreamdevil00/nest-authz/commit/623901321bdb6c65a5dd3d20104c68ce669389f1)) | ||
# [1.0.0](https://github.com/dreamdevil00/nest-authz/compare/v0.1.0...v1.0.0) (2019-04-27) | ||
@@ -6,0 +30,0 @@ |
@@ -9,15 +9,16 @@ "use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var AuthZModule_1; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var AuthZModule_1; | ||
const common_1 = require("@nestjs/common"); | ||
const casbin = require("casbin"); | ||
const authz_guard_1 = require("./authz.guard"); | ||
const authz_constants_1 = require("./authz.constants"); | ||
const casbin = require("casbin"); | ||
const services_1 = require("./services"); | ||
@@ -30,16 +31,23 @@ let AuthZModule = AuthZModule_1 = class AuthZModule { | ||
}; | ||
const enforcerProvider = { | ||
provide: authz_constants_1.AUTHZ_ENFORCER, | ||
useFactory: () => __awaiter(this, void 0, void 0, function* () { | ||
const isFile = typeof options.policy === 'string'; | ||
let policyOption; | ||
if (isFile) { | ||
policyOption = options.policy; | ||
} | ||
else { | ||
policyOption = yield options.policy; | ||
} | ||
return yield casbin.newEnforcer(options.model, policyOption); | ||
}) | ||
}; | ||
let enforcerProvider = options.enforcerProvider; | ||
const importsModule = options.imports || []; | ||
if (!enforcerProvider) { | ||
if (!options.model || !options.policy) { | ||
throw new Error('must provide either enforcerProvider or both model and policy'); | ||
} | ||
enforcerProvider = { | ||
provide: authz_constants_1.AUTHZ_ENFORCER, | ||
useFactory: () => __awaiter(this, void 0, void 0, function* () { | ||
const isFile = typeof options.policy === 'string'; | ||
let policyOption; | ||
if (isFile) { | ||
policyOption = options.policy; | ||
} | ||
else { | ||
policyOption = yield options.policy; | ||
} | ||
return casbin.newEnforcer(options.model, policyOption); | ||
}) | ||
}; | ||
} | ||
return { | ||
@@ -54,2 +62,3 @@ module: AuthZModule_1, | ||
], | ||
imports: importsModule, | ||
exports: [ | ||
@@ -56,0 +65,0 @@ moduleOptionsProvider, |
@@ -1,6 +0,8 @@ | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { ExecutionContext, Provider, DynamicModule, ForwardReference, Type } from '@nestjs/common'; | ||
export interface AuthZModuleOptions<T = any> { | ||
model: string; | ||
policy: string | Promise<T>; | ||
model?: string; | ||
policy?: string | Promise<T>; | ||
usernameFromContext: (context: ExecutionContext) => string; | ||
enforcerProvider?: Provider<any>; | ||
imports?: Array<Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference>; | ||
} |
@@ -15,6 +15,7 @@ "use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
@@ -21,0 +22,0 @@ }); |
{ | ||
"name": "nest-authz", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "基于 node-casbin 实现的 RBAC 权限控制模块。", | ||
"main": "dist/index.js", | ||
"engines": { | ||
"node": ">=8.9.0" | ||
}, | ||
"directories": { | ||
@@ -23,16 +26,16 @@ "test": "test" | ||
"devDependencies": { | ||
"@nestjs/common": "^6.0.0", | ||
"@nestjs/core": "^6.0.0", | ||
"@types/jest": "^24.0.8", | ||
"@types/node": "^11.13.8", | ||
"jest": "^23.5.0", | ||
"prettier": "^1.14.2", | ||
"reflect-metadata": "^0.1.12", | ||
"rimraf": "^2.6.3", | ||
"rxjs": "^6.2.2", | ||
"standard-version": "^5.0.2", | ||
"ts-jest": "^23.1.3", | ||
"ts-node": "^7.0.1", | ||
"@nestjs/common": "6.10.13", | ||
"@nestjs/core": "6.10.13", | ||
"@types/jest": "24.0.25", | ||
"@types/node": "11.15.3", | ||
"jest": "24.9.0", | ||
"prettier": "1.19.1", | ||
"reflect-metadata": "0.1.13", | ||
"rimraf": "3.0.0", | ||
"rxjs": "6.5.4", | ||
"standard-version": "7.0.1", | ||
"ts-jest": "24.2.0", | ||
"ts-node": "8.5.4", | ||
"tslint": "5.11.0", | ||
"typescript": "^3.4.3" | ||
"typescript": "3.7.4" | ||
}, | ||
@@ -39,0 +42,0 @@ "peerDependencies": { |
@@ -29,6 +29,10 @@ # nest-authz | ||
- `model` (REQUIRED) is a path string to the casbin model. | ||
- `policy` (REQUIRED) is a path string to the casbin policy file or adapter | ||
- `model` is a path string to the casbin model. | ||
- `policy` is a path string to the casbin policy file or adapter | ||
- `usernameFromContext` (REQUIRED) is a function that accepts `ExecutionContext`(the param of guard method `canActivate`) as the only parameter and returns either the username as a string or null. The `AuthZGuard` uses username to determine user's permission internally. | ||
- `enforcerProvider` Optional enforcer provider | ||
- `imports` Optional list of imported modules that export the providers which are required in this module. | ||
There are two ways to configure enforcer, either `enforcerProvider`(optional with `imports`) or `model` with `policy` | ||
An example configuration which reads username from the http request. | ||
@@ -63,2 +67,34 @@ | ||
or | ||
```typescript | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { ConfigModule, ConfigService } from './config.module'; | ||
import { AUTHZ_ENFORCER } from 'nest-authz'; | ||
@Module({ | ||
imports: [ | ||
ConfigModule, | ||
AuthZModule.register({ | ||
imports: [ConfigModule], | ||
enforcerProvider: { | ||
provide: AUTHZ_ENFORCER, | ||
useFactory: async (configSrv: ConfigService) => { | ||
const config = await configSrv.getAuthConfig(); | ||
return casbin.newEnforcer(config.model, config.policy); | ||
}, | ||
inject: [ConfigService], | ||
}, | ||
usernameFromContext: (ctx) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
return request.user && request.user.username; | ||
} | ||
}), | ||
], | ||
controllers: [AppController], | ||
providers: [AppService] | ||
``` | ||
The latter one is preferred. | ||
### Checking Permissions | ||
@@ -65,0 +101,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
38800
603
188
0