# identity-controls

> Identity access controls for your application

Latest version **1.0.1** (published 2021-01-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install identity-controls
pnpm add identity-controls
yarn add identity-controls
bun add identity-controls
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-01-17 |
| First published | 2021-01-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 12.0.0 |
| Dependencies | 2 |
| Unpacked size | 11.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | James D |
| Maintainers | jdrydn |
| Keywords | identity, controls, access, management, iam, permissions |

## Links

- npm: https://www.npmjs.com/package/identity-controls
- Repository: https://github.com/someimportantcompany/identity-controls
- Homepage: https://github.com/someimportantcompany/identity-controls#readme
- Issues: https://github.com/someimportantcompany/identity-controls/issues
- npm.io page: https://npm.io/package/identity-controls

## Dependencies (2)

- [micromatch](https://npm.io/package/micromatch.md) ^4.0.2
- [lodash.isplainobject](https://npm.io/package/lodash.isplainobject.md) ^4.0.6

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-01-17

## README

# identity-controls

[![NPM](https://badge.fury.io/js/identity-controls.svg)](https://npm.im/identity-controls)
[![CI](https://github.com/someimportantcompany/identity-controls/workflows/Test/badge.svg?branch=master)](https://github.com/someimportantcompany/identity-controls/actions?query=branch%3Amaster)
<!-- [![Coverage](https://coveralls.io/repos/github/someimportantcompany/identity-controls/badge.svg?branch=master)](https://coveralls.io/github/someimportantcompany/identity-controls?branch=master) -->

Identity access controls for your application. Given an array of policy statements, work out if this user is allowed to perform an action on a resource. Follows the flexible pattern you encounter when working with [AWS IAM permission statements](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_boundaries.html).

```js
const { buildPermissions } = require('identity-controls');

const identity = 'urn:someimportantcompany:users:11211';
const permissions = [
  {
    effect: 'ALLOW',
    actions: [ 'posts:*' ],
    resources: [ 'urn:someimportantcompany:posts:*' ],
  },
  {
    effect: 'ALLOW',
    action: [ 'users:*' ],
    resource: [ '{{identity}}' ],
  },
  {
    effect: 'DENY',
    action: [ 'users:delete' ],
    resource: [ '{{identity}}' ],
  },
];

const permissions = buildPermissions(identity, permissions);

permissions.can('posts:edit', 'urn:someimportantcompany:posts:a6db9385');
// true
permissions.can('users:changePassword', 'urn:someimportantcompany:users:11211');
// true
permissions.can('users:delete', 'urn:someimportantcompany:users:11211');
// false

permissions.assert('posts:edit', 'urn:someimportantcompany:posts:a6db9385');
// true
permissions.assert('users:changePassword', 'urn:someimportantcompany:users:11211');
// true
permissions.assert('users:delete', 'urn:someimportantcompany:users:11211');
// ERROR: Permission denied to urn:someimportantcompany:users:11211
//   for users:delete on urn:someimportantcompany:users:11211
```

## Installation

```
npm install --save identity-controls
```

## API

### `buildPermissions(identity, permissions[, defaultConditions])`

Create a permissions object to capable of checking permissions.

| Argument | Description |
| ---- | ---- |
| `identity` | A string defining the current identity |
| `permissions` | An array of permission statements |

This returns a permissions object with the following methods:

#### `can(action, resource[, conditions])`

Returns a boolean on whether the `permissions` given earlier allow `action` to be undertaken on `resource`.

| Argument | Description |
| ---- | ---- |
| `action` | A string defining the select action |
| `resource` | A string defining the selected resource |
| `conditions` | An optional object defining conditions |

#### `assert(action, resource[, conditions])`

Throws a `PermissionDeniedError` if the `permissions` given earlier does not allow `action` to be undertaken on `resource`.

| Argument | Description |
| ---- | ---- |
| `action` | A string defining the select action |
| `resource` | A string defining the selected resource |
| `conditions` | An optional object defining conditions |

### `can(permissions, action, resource[, conditions])`

Returns a boolean on whether `permissions` allows `action` to be undertaken on `resource`.

| Argument | Description |
| ---- | ---- |
| `permissions` | An array of permission statements. |
| `action` | A string defining the select action |
| `resource` | A string defining the selected resource |
| `conditions` | An optional object defining conditions |

## Conditions

Conditions are key-value objects allowing you to create permissions with dynamic resources (e.g. I can change my own password). They're are simple mustache-like variables that drop into `resources`. For example:

```js
const { can } = require('identity-controls');

const permissions = [
  {
    effect: 'ALLOW',
    actions: [ 'posts:*' ],
    resources: [ 'urn:someimportantcompany:posts:*' ],
  },
  {
    effect: 'ALLOW',
    action: [ 'users:*' ],
    resource: [ '{{identity}}' ],
  },
  {
    effect: 'DENY',
    action: [ 'users:delete' ],
    resource: [ '{{identity}}' ],
  },
];

can(permissions, 'users:read', 'urn:someimportantcompany:users:1', {
  identity: 'urn:someimportantcompany:users:1',
});
// true

can(permissions, 'users:delete', 'urn:someimportantcompany:users:1', {
  identity: 'urn:someimportantcompany:users:1',
});
// false
```

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