# @glenstack/cf-workers-access

> Authenticate with Cloudflare Access from within a Cloudflare Worker

Latest version **1.0.8** (published 2020-09-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install @glenstack/cf-workers-access
pnpm add @glenstack/cf-workers-access
yarn add @glenstack/cf-workers-access
bun add @glenstack/cf-workers-access
```

## 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.8 |
| Published | 2020-09-25 |
| First published | 2020-07-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 9.9 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 60 |
| Author | Greg Brimble |
| Maintainers | gregbrimble |

## Links

- npm: https://www.npmjs.com/package/@glenstack/cf-workers-access
- Repository: https://github.com/glenstack/glenstack
- Homepage: https://github.com/glenstack/glenstack#readme
- Issues: https://github.com/glenstack/glenstack/tree/master/packages/cf-workers-access
- npm.io page: https://npm.io/package/@glenstack/cf-workers-access

## Dependencies (2)

- [cookie](https://npm.io/package/cookie.md) 0.4.1
- [node-jose](https://npm.io/package/node-jose.md) 1.1.4

## Recent versions

- 1.0.8 (latest) — 2020-09-25
- 1.0.6 — 2020-08-18
- 1.0.5 — 2020-07-13
- 1.0.4 — 2020-07-08
- 1.0.3 — 2020-07-08
- 1.0.2 — 2020-07-08
- 1.0.1 — 2020-07-08
- 1.0.0 — 2020-07-08

## README

# Cloudflare Workers Access

Authenticate with [Cloudflare Access](https://teams.cloudflare.com/access/) from within a [Cloudflare Worker](https://workers.cloudflare.com/).

## Installation

```sh
npm install --save @glenstack/cf-workers-access
```

## Usage

```typescript
import { createAuthenticator } from "@glenstack/cf-workers-access";

const AUTHENTICATION_DOMAIN = "glenstack.cloudflareaccess.com";
const POLICY_AUD =
  "f8612530c08484786e83e00ff7b549bc3763747beae55ec909cc28a6a56b81d1";

const handleRequest = async (request: Request) => {
  const authenticator = await createAuthenticator(AUTHENTICATION_DOMAIN, {
    aud: POLICY_AUD,
  });

  const jwt = await authenticator(request);

  if (jwt) {
    return new Response(`Hello, ${jwt.email}!`);
  }

  return new Response("Unauthorized", { status: 401 });
};

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});
```

`createAuthenticator` takes two parameters:

1. The "Login Page Domain" of your Cloudflare Access account e.g. `glenstack.cloudflareaccess.com`.
1. Optionally, an object with the optional parameters:

   - `aud`: The "Audience Tag" of your Access Policy.
   - `iss`: Automatically set to the "Login Page Domain", but optionally overridable.
   - `tolerance`: Number of seconds of leeway for validating `exp` and `nbf` claims. Defaults to `0`.

It returns a Promise of a function which, in turn, takes a Request and returns a Promise of either:

- `false` if the Request failed authentication, or
- [the JWT payload](https://developers.cloudflare.com/access/setting-up-access/json-web-token/#payload).

---
_Source: https://npm.io/package/@glenstack/cf-workers-access · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
