# http-basic-auth-proxy-worker

> A service worker that proxies browser-initiated fetch request to support HTTP Basic Authentication

Latest version **0.2.2** (published 2019-05-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install http-basic-auth-proxy-worker
pnpm add http-basic-auth-proxy-worker
yarn add http-basic-auth-proxy-worker
bun add http-basic-auth-proxy-worker
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2019-05-25 |
| First published | 2018-10-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | andornaut |
| Maintainers | andornaut |
| Keywords | http basic authentication, basic authentication, service workers |

## Links

- npm: https://www.npmjs.com/package/http-basic-auth-proxy-worker
- Repository: https://github.com/andornaut/http-basic-auth-proxy-worker
- Homepage: https://github.com/andornaut/http-basic-auth-proxy-worker#readme
- Issues: https://github.com/andornaut/http-basic-auth-proxy-worker/issues
- npm.io page: https://npm.io/package/http-basic-auth-proxy-worker

## 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.2.2 (latest) — 2019-05-25
- 0.2.1 — 2019-05-25
- 0.2.0 — 2019-05-25
- 0.1.0 — 2019-05-20
- 0.0.2 — 2018-10-16

## README

# http-basic-auth-proxy-worker

A [service worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) that proxies fetch request in
order to support
[HTTP Basic Authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Basic_authentication_scheme).

This can be used to support HTTP Basic Authentication in
[`<audio>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio) and
[`<video>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video) elements, which
[are not permitted to include credentials](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Access_using_credentials_in_the_URL)
in `src` attributes, eg. `<video src="https://USERNAME:PASSWORD@example.com">`.

Web browsers usually prompt the user to enter access credentials when resources require HTTP Basic Authentication,
but this service worker can be used to avoid this prompt in cases where the access credentials are already known.

Note that resources whose `request.destination` is a [`<script>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
element are _not_ proxied.

## Getting Started

Install from [npm](https://www.npmjs.com/package/http-basic-auth-proxy-worker).

```bash
npm install --save http-basic-auth-proxy-worker
```

Install the `worker.js` file at the root of your application or at a location that matches your desired
[scope](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/scope).

```javascript
// webpack.config.js
module.exports = {
  entry: {
    main: "./src/index.js",
    worker: "./node_modules/http-basic-auth-proxy-worker/worker.js"
  }
};
```

Register the service worker in your entrypoint script:

```javascript
// index.js
const config = {
  baseUrl: "https://example.com/",
  username: "username",
  password: "password",
};

navigator.serviceWorker.addEventListener("message", event => {
  event.ports[0].postMessage(config);
});

navigator.serviceWorker.register("./worker.js");
```

This service worker sends its client(s) messages to request configuration.
This service worker maintains a cache of its configuration, but clients can invalidate this cache by sending the
a message:

```javascript
// You can specify arbitrary message payload.
navigator.serviceWorker.controller.postMessage("invalidate-cache");
```

## Configuration

This service worker must be configured in order for it to proxy requests.
The configuration object should contain the following properties:

| Name     | Description                                                                                               |
| -------- | --------------------------------------------------------------------------------------------------------- |
| baseUrl  | **Required** A URL prefix. If a fetched `request.url` begins with the "baseUrl", then it will be proxied. |
| username | **Required** The username to include in the HTTP Basic Authentication header.                             |
| password | The password to include in the HTTP Basic Authentication header.                                          |

## Web Server

You may need to configure your web server to support [CORS requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS),
by adding the requisite access control headers:

### Example Nginx configuration

```
location / {
    root /var/www/html;
    autoindex on;

    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/cybertron.spacectrl.com.htpasswd;

    add_header Access-Control-Allow-Credentials true always;
    add_header Access-Control-Allow-Origin $http_origin always;
    add_header Access-Control-Allow-Methods GET,POST,HEAD,DELETE,PUT,OPTIONS always;
    add_header Access-Control-Allow-Headers authorization,chrome-proxy,range always;

    if ($request_method = 'OPTIONS') {
        add_header Access-Control-Allow-Credentials true always;
        add_header Access-Control-Allow-Origin $http_origin always;
        add_header Access-Control-Allow-Methods GET,POST,HEAD,DELETE,PUT,OPTIONS always;
        add_header Access-Control-Allow-Headers authorization,chrome-proxy,range always;
        return 204;
    }
}
```

## Developing

### Chrome

- Navigate to chrome://flags/#unsafely-treat-insecure-origin-as-secure.
- Enable this option, and then add your local domain to this list, eg. "localhost"

### Firefox

- Navigate to about:debugging#workers
- Disable "multi content processes" by setting “dom.ipc.multiOptOut” to `true` in about:config.

---
_Source: https://npm.io/package/http-basic-auth-proxy-worker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
