https://github.com/bjerkio/nestjs-oso
Simplify implementation of oso with NestJS.
https://github.com/bjerkio/nestjs-oso
decorator library nestjs oso register
Last synced: about 1 year ago
JSON representation
Simplify implementation of oso with NestJS.
- Host: GitHub
- URL: https://github.com/bjerkio/nestjs-oso
- Owner: bjerkio
- License: apache-2.0
- Created: 2020-11-28T19:32:14.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-17T12:37:39.000Z (over 1 year ago)
- Last Synced: 2025-03-30T21:06:29.217Z (over 1 year ago)
- Topics: decorator, library, nestjs, oso, register
- Language: TypeScript
- Homepage:
- Size: 575 KB
- Stars: 39
- Watchers: 1
- Forks: 6
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# NestJS OSO โ Authorization code for NestJS
[](https://github.com/prettier/prettier)
[](http://commitizen.github.io/cz-cli/)
[](https://github.com/semantic-release/semantic-release)

[](https://codecov.io/gh/bjerkio/nestjs-oso)
[](https://codeclimate.com/github/bjerkio/nestjs-oso/maintainability)
> [oso][] is an open-source policy engine for authorization thatโs embedded in
> your application. It provides a declarative policy language for expressing
> authorization logic. You define this logic separately from the rest of your
> application code, but it executes inside the application and can call directly
> into it.
`nestjs-oso` is a library that simplifies the implementation of [oso][] with
[NestJS][nest].
## Features
- **@OsoClass** decorator (automatically registers the class to use within Oso)
- **OsoService** (a ready to use NestJS service)
## Quickstart
```shell
โถ yarn add nestjs-oso oso
```
```typescript
import { Module } from '@nestjs/common';
import { OsoModule } from 'nestjs-oso';
@Module({
imports: [
OsoModule.forRoot({
loadFiles: ['./permissions.polar'],
// or multiple files
// loadFiles: ['./permissions.polar', './other-policies.polar'],
// or using wildcards
// loadFiles: ['./**/*.polar']
}),
],
})
export class AppModule {}
```
**Tip:** You don't have to apply either `loadFiles` or `loadStr`. You can inject
`OsoService` and access the original API for oso anytime!
### Example
You can easily inject `OsoService` to be used in your services, controllers,
etc.
```typescript
import { Injectable } from '@nestjs/common';
import { OsoService } from 'nestjs-oso';
@Injectable()
export class AuthService {
constructor(private oso: OsoService) {}
/*
Implementation that makes use of this.oso
*/
}
```
To register an class with `oso`, use the decorator:
```typescript
import { OsoClass } from 'nestjs-oso';
@OsoClass()
export class User {
id: string;
}
```
This will automatically be registered using `registerClass` function in `oso`.
### Add Polar files to `assets` in `nest-cli.json`
In the `nest-cli.json` file, we add the `assets` property to distribute non-Typescript
files and watchAssets to turn on watching all non-Typescript assets. In our case, we
probably want to add `*.polar` files to be automatically copied to the `dist` folder
and reloaded when changed.
You can find an example in [osohq/oso-nest-doc-mgmt](https://github.com/osohq/oso-nest-doc-mgmt/blob/main/nest-cli.json).
```json
{
"compilerOptions": {
"assets": ["**/*.polar"],
"watchAssets": true
}
}
```
## Contribute & Disclaimer
We love to get help ๐ Read more about how to get started in
[CONTRIBUTING](CONTRIBUTING.md) ๐ณ
[oso]: https://github.com/osohq/oso
[nest]: https://github.com/nestjs/nest