# @mest-fe/nestjs-sso

> Common authentication SDK for internal services (NestJS project).

Latest version **0.2.0** (published 2023-09-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mest-fe/nestjs-sso
pnpm add @mest-fe/nestjs-sso
yarn add @mest-fe/nestjs-sso
bun add @mest-fe/nestjs-sso
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2023-09-13 |
| First published | 2022-09-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 45.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| Author | Mest team |
| Maintainers | pingxi, jason_at_mest, thezjy, mest-finch, echo_unix |

## Links

- npm: https://www.npmjs.com/package/@mest-fe/nestjs-sso
- npm.io page: https://npm.io/package/@mest-fe/nestjs-sso

## Dependencies (6)

- [axios](https://npm.io/package/axios.md) ^1.5.0
- [jwks-rsa](https://npm.io/package/jwks-rsa.md) ^3.0.1
- [passport](https://npm.io/package/passport.md) ^0.6.0
- [passport-jwt](https://npm.io/package/passport-jwt.md) ^4.0.1
- [@nestjs/passport](https://npm.io/package/@nestjs/passport.md) ^10.0.2
- [passport-http-bearer](https://npm.io/package/passport-http-bearer.md) ^1.0.1

## Recent versions

- 0.2.0 (latest) — 2023-09-13
- 0.3.0-dev.1 (dev) — 2023-12-27
- 0.3.0-dev.0 — 2023-12-24
- 0.1.3 — 2022-10-20
- 0.1.2-dev.1 — 2022-10-20
- 0.1.2-dev.0 — 2022-10-20
- 0.1.2 — 2022-10-20
- 0.1.1 — 2022-10-20
- 0.1.0 — 2022-10-20
- 0.0.6 — 2022-09-27
- 0.0.5 — 2022-09-27
- 0.0.4 — 2022-09-27
- 0.0.3 — 2022-09-27
- 0.0.2 — 2022-09-27
- 0.0.1 — 2022-09-15

## README

## NestJS SSO

Common authentication SDK for internal services (NestJS project).

## Installation

1. Install package: `yarn add @mest-fe/nestjs-sso`
2. Add `SSOModule.forRoot()` to your module imports.

## Usage

### Authentication

Add a decorator to any controller class or method, and the user will be authenticated before the method is called.

```ts
// controller
@MestAuth()
@Controller('/users')
export class UsersController {
}

// method
@MestAuth()
@Post('/login')
async login() {
  return 'ok'
}
```

### Scope

Specify roles for controller classes or methods:

```ts
// Require authenticated and admin role
@MestRoleAuth(MestRoles.ADMIN)
@Delete('/users')
async removeUsers() {
  return 'ok'
}
```

### Get current user

On any controller method, the user of the current session can be injected:

```ts
@Get('/users/self')
@MestAuth()
async getSelf(@MestUser() user: MestUserType){
  return user
}
```

## HTTP Bearer Auth

When you just need to authenticate a fixed token, i.e. string comparison, use bearer authentication mode:

```ts
// module
HttpBearerModule.forRoot({ token: 'my-token' })

// controller
@Get('/token')
@MestTokenAuth()
async getMessage() {
  return `hello world`
}

// fetch('...', { headers: { Authorization: 'Bearer my-token' }}>
```

## Errors

When a user without a session request an api that requires authentication, a `401` error will be returned in http;
If the user simply has a role that does not match, a `403` error will be thrown.

The server will not handle these errors and you should direct the user on the client side to the [sso.mest.sh](https://sso.mest.sh) to log in.

---
_Source: https://npm.io/package/@mest-fe/nestjs-sso · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
