# authority-handler

> An authority handler of backend server

Latest version **1.0.12** (published 2022-12-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install authority-handler
pnpm add authority-handler
yarn add authority-handler
bun add authority-handler
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.12 |
| Published | 2022-12-28 |
| First published | 2022-09-08 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Leon Shih |
| Maintainers | leonshih |
| Keywords | authority |

## Links

- npm: https://www.npmjs.com/package/authority-handler
- Repository: https://github.com/leonshih/authority-handler
- Homepage: https://github.com/leonshih/authority-handler#readme
- Issues: https://github.com/leonshih/authority-handler/issues
- npm.io page: https://npm.io/package/authority-handler

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@oxyhq/services](https://npm.io/package/@oxyhq/services.md) — 2.3K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads

## Recent versions

- 1.0.12 (latest) — 2022-12-28
- 1.0.11 — 2022-12-26
- 1.0.10 — 2022-12-26
- 1.0.9 — 2022-12-26
- 1.0.8 — 2022-10-18
- 1.0.7 — 2022-10-18
- 1.0.6 — 2022-10-07
- 1.0.5 — 2022-10-07
- 1.0.4 — 2022-10-07
- 1.0.3 — 2022-09-13
- 1.0.2 — 2022-09-12
- 1.0.1 — 2022-09-08
- 1.0.0 — 2022-09-08

## README

# authority-handler
[![npm version](https://img.shields.io/npm/v/authority-handler)](https://www.npmjs.com/package/authority-handler)

## Install

```
npm install authority-handler
```

or use yarn

```
yarn add authority-handler
```

## Configuration
* Permission Map
```js
/** 權限Map */
export const functionPermissionMap = {
    NONE: 0,
    READ: 1 << 0, // 1 讀取
    CREATE: 1 << 1, // 2 建立
    UPDATE: 1 << 2, // 4 更新
    DELETE: 1 << 3, // 8 刪除
    EXPORT: 1 << 4, // 16 匯出
    IMPORT: 1 << 5, // 32 匯入
}
```

* Determine the permission of services' functions
```js
/** 服務權限值對應表 */
export const servicePermissionMap = {
    /** 服務一 */
    F01:
        functionPermissionMap.READ |
        functionPermissionMap.CREATE |
        functionPermissionMap.UPDATE |
        functionPermissionMap.DELETE,
    /** 服務二 */
    F02:
        functionPermissionMap.READ |
        functionPermissionMap.CREATE |
        functionPermissionMap.UPDATE |
        functionPermissionMap.DELETE |
        functionPermissionMap.EXPORT |
        functionPermissionMap.IMPORT,
    /** 服務三 */
    F03:
        functionPermissionMap.READ | 
        functionPermissionMap.EXPORT | 
        functionPermissionMap.IMPORT,
};
```


## Usage

* Create an instance
```js
import AuthorityHandler from 'authority-handler'

const authorityHandler = new AuthorityHandler({ 
    servicePermissionMap, 
    permissionMap, // optional
});
``` 

* Verify the authoriy of service
```js
const functionPermissionMap = authorityHandler.functionPermissionMap;

const result = authorityHandler.verifyServicePermission('F01', functionPermissionMap.READ);
// true / false
```

* Verify the user's permission of service
```js
// Data to be verified
const userAuthorities = 
    [
        { serviceKey: 'F01', permission: 3 },
        { serviceKey: 'F02', permission: 3 },
        { serviceKey: 'F03', permission: 16 },
    ];

const result = authorityHandler.verifyUserAuthorities(userAuthorities, 'F01', 'READ');

// true / false
```

---
_Source: https://npm.io/package/authority-handler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
