# passport-paseto

> Paseto passport

Latest version **0.1.0-alpha.2** (published 2023-11-20) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install passport-paseto
pnpm add passport-paseto
yarn add passport-paseto
bun add passport-paseto
```

## Health

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

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

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0-alpha.2 |
| Published | 2023-11-20 |
| First published | 2023-03-21 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 16.0.0 |
| Dependencies | 2 |
| Unpacked size | 44.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Han |
| Maintainers | deskbtm |
| Keywords | passport, paseto, jwt, security |

## Links

- npm: https://www.npmjs.com/package/passport-paseto
- Repository: https://github.com/indiebase/passport-paseto
- Homepage: https://github.com/indiebase/passport-paseto#readme
- Issues: https://github.com/indiebase/passport-paseto/issues
- npm.io page: https://npm.io/package/passport-paseto

## Dependencies (2)

- [paseto](https://npm.io/package/paseto.md) ^3.1.2
- [passport-strategy](https://npm.io/package/passport-strategy.md) ^1.0.0

## Alternatives

- [@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
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 0.1.0-alpha.2 (latest) — 2023-11-20
- 0.0.1-alpha.2 — 2023-09-28
- 0.0.1-alpha.1 — 2023-09-26
- 0.1.0-alpha.1 — 2023-08-20
- 0.1.0-alpha.0 — 2023-08-20
- 0.0.0-alpha.3 — 2023-03-21
- 0.0.0-alpha.2 — 2023-03-21
- 0.0.0-alpha.1 — 2023-03-21

## README

[![nawbc](https://img.shields.io/badge/Github-Follow_nawbc-green?style=flat-square&logo=github)](https://github.com/nawbc)
![workflow](https://img.shields.io/github/actions/workflow/status/indiebase/passport-paseto/ci.yml?style=flat-square)

## Install

```shell
npm i passport-paseto
```

## Usage

### LocalPasetoStrategy (symmetric key)

```ts
const app = fastify();
// Must have due to @fastify/passport depends on @fastify/flash
app.register(fastifySession, {
  secret: "secret with minimum length of 32 characters",
});
app.register(fastifyPassport.initialize());

const key = await V3.generateKey("local");
const token = await V3.encrypt(
  {
    username: "test",
  },
  key,
  {
    expiresIn: "99999999s",
  }
);

fastifyPassport.use(
  "local-paseto",
  new LocalPasetoStrategy(
    {
      getToken: fromAuthBearer(),
      key,
      version: "V3",
    },
    (payload, done) => {
      done(null, { username: "username_test" });
    }
  )
);

app.get(
  "/test/bearer",
  {
    preValidation: fastifyPassport.authenticate("local-paseto", {
      authInfo: false,
      session: false,
    }),
  },
  async function (req, reply) {
    reply.send();
  }
);

app.listen();
```

### LocalPasetoStrategy(options: LocalPasetoStrategyOptions, verify);

#### LocalPasetoStrategyOptions:

- `key`: `<KeyObject>` The secret key to decrypt with. Alternatively a `'k3.local.[data]'`
  PASERK string or any input that works for `crypto.createSecretKey()`.
- `passReqToCallback`: `<boolean>` default `false`.
- `getToken`: `<Function>` `(...args) => (req) => string`
  - `fromHeader`: extract token from header default `X-Paseto-Token`.
  - `fromAuthBearer`: extract token from Authorization Bearer.
  - `fromAuthScheme`: extract token from Authorization, e.g. Basic, Digest ...
  - `fromBody`: extract token from request body.
  - `fromQuery`: extract token from request query.
- version：`V1` | `V3`
- `consumeOptions`: `<Object>`
  - `assertion`: `<string>` &vert; `<Buffer>` PASETO Implicit Assertion
  - `audience`: `<string>` Expected audience value. An exact match must be found in the payload.
  - `clockTolerance`: `<string>` Clock Tolerance for comparing timestamps, provided as timespan
    string e.g. `120s`, `2 minutes`, etc. **Default:** no clock tolerance
  - `complete`: `<Boolean>` When false only the parsed payload is returned, otherwise an object with
    a parsed payload and footer (as a Buffer) will be returned.
    **Default:** 'false'
  - `ignoreExp`: `<Boolean>` When true will not be validating the "exp" claim value to be in the
    future from now. **Default:** 'false'
  - `ignoreIat`: `<Boolean>` When true will not be validating the "iat" claim value to be in the
    past from now. **Default:** 'false'
  - `ignoreNbf`: `<Boolean>` When true will not be validating the "nbf" claim value to be in the
    past from now. **Default:** 'false'
  - `issuer`: `<string>` Expected issuer value. An exact match must be found in the payload.
  - `maxTokenAge`: `<string>` When provided the payload is checked to have the "iat" claim and its
    value is validated not to be older than the provided timespan string e.g. `30m`, `24 hours`.
  - `now`: `<Date>` Date object to be used instead of the current unix epoch timestamp.
    **Default:** 'new Date()'
  - `subject`: `<string>` Expected subject value. An exact match must be found in the payload.

#### Verify callback `([req], payload, next) => void`

### PublicPasetoStrategy(options: PublicPasetoStrategyOptions, verify)

#### PublicPasetoStrategyOptions:

- version：`V1` | `V2` | `V3` | `V4`

```ts
const fastifyPassport = require("@fastify/passport");
const { LocalPasetoStrategy, fromAuthBearer } = require("passport-paseto");
const { V3 } = require("paseto");

const { secretKey, publicKey } = await V3.generateKey("public", {
  format: "paserk",
});

const token = await V3.sign(
  {
    username: "test",
  },
  secretKey,
  {
    expiresIn: "99999999s",
  }
);

fastifyPassport.use(
  "public-paseto",
  new PublicPasetoStrategy(
    {
      getToken: fromAuthBearer(),
      publicKey,
      version: "V3", //default V4
    },
    (payload, done) => {
      done(null, { username: "username_test" });
    }
  )
);

app.get(
  "/test/bearer",
  {
    preValidation: fastifyPassport.authenticate("public-paseto", {
      authInfo: false,
      session: false,
    }),
  },
  async function (req, reply) {
    reply.send();
  }
);
```

[#Samples](https://github.com/Nawbc/passport-paseto/tree/main/spec)

## Tests

```shell
npm i
npm test
```

## Note Bene

Only test with Fastify.

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