# openapi-security-handler

> A library to process OpenAPI security definitions in parallel.

Latest version **12.1.3** (published 2023-05-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install openapi-security-handler
pnpm add openapi-security-handler
yarn add openapi-security-handler
bun add openapi-security-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 | 12.1.3 |
| Published | 2023-05-24 |
| First published | 2018-06-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 32 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 921 |
| Author | Joseph Spencer |
| Maintainers | jsdevel |
| Keywords | openapi, security, handler, jsonschema |

## Links

- npm: https://www.npmjs.com/package/openapi-security-handler
- Repository: https://github.com/kogosoftwarellc/open-api/tree/master/packages/openapi-security-handler
- Homepage: https://github.com/kogosoftwarellc/open-api/tree/master/packages/openapi-security-handler#readme
- Issues: https://github.com/kogosoftwarellc/open-api/issues
- npm.io page: https://npm.io/package/openapi-security-handler

## Dependencies (1)

- [openapi-types](https://npm.io/package/openapi-types.md) ^12.1.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 12.1.3 (latest) — 2023-05-24
- 12.1.2 — 2023-05-24
- 12.1.1 — 2023-05-20
- 12.1.0 — 2022-12-09
- 12.0.2 — 2022-08-31
- 12.0.0 — 2022-06-03
- 11.1.0 — 2022-06-03
- 11.0.1 — 2022-05-16
- 11.0.0 — 2022-04-13
- 10.0.0 — 2021-12-02
- 9.3.1 — 2021-11-12
- 9.3.0 — 2021-08-30
- 9.2.0 — 2021-08-20
- 9.1.0 — 2021-07-06
- 9.0.3 — 2021-06-01
- … 13 more at https://npm.io/package/openapi-security-handler/versions

## README

# openapi-security-handler [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Coveralls Status][coveralls-image]][coveralls-url]
> A library to process OpenAPI security definitions in parallel.

## Highlights

* Easily handle security schemes.
* Performant.
* Extensively tested.
* Small footprint.
* Promise based interface.

## Example

See `./test/data-driven/*.js` for more examples.

```javascript
import OpenAPISecurityHandler from 'openapi-security-handler';
const handler = new OpenAPISecurityHandler({
  // these are typically taken from the global api doc
  securityDefinitions: {
    keyScheme: {
      type: 'apiKey',
      name: 'api_key',
      in: 'header'
    },
    passwordScheme: {
      type: 'basic'
    }
  },
  // these handle the operation security reference
  securityHandlers: {
    keyScheme: function(req, scopes, securityDefinition) {
      req.user = {name: 'fred'};
      return true; // could also throw or return a Promise.
    },
    passwordScheme: function(req, scopes, securityDefinition) {
      req.user = {name: 'fred'};
      return true;
    }
  },
  // These are typically defined on an operation's openapi document.
  operationSecurity: [
    // we'll execute all the schemes in the object.  If any fail, we'll move to the
    // next object.
    {
      keyScheme: ['write']
    },
    {
      passwordScheme: ['write']
    }
  ],
});
const request = {};
handler.handle(request).then(result => {
  console.log(result); // => true
});
```

## handler.handle
### Return Value

`openapi-security-handler#handle` returns a `Promise`.

* If any of the `securityHandlers` throw an error, the error will be available with `.catch`.
* If all of the `securityHandlers` for the given `operationSecurity` scheme resolve with `true`, then `true` will be resolved.
* If none of the `securityHandlers` resolve with `true` for _all_ of the `operationSecurity` schemes, then a 401 error will be thrown.

## Successful Authentication

Handlers should assign credentials to the request object.

## LICENSE
``````
The MIT License (MIT)

Copyright (c) 2018 Kogo Software LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
``````

[downloads-image]: http://img.shields.io/npm/dm/openapi-security-handler.svg
[npm-url]: https://npmjs.org/package/openapi-security-handler
[npm-image]: http://img.shields.io/npm/v/openapi-security-handler.svg

[coveralls-url]: https://coveralls.io/github/kogosoftwarellc/open-api?branch=main
[coveralls-image]: https://coveralls.io/repos/github/kogosoftwarellc/open-api/badge.svg?branch=main

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