# authrite-js

> A system for mutual authentication of two parties over a communications channel

Latest version **0.4.36** (published 2024-10-01) · Open BSV License license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install authrite-js
pnpm add authrite-js
yarn add authrite-js
bun add authrite-js
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.4.36 |
| Published | 2024-10-01 |
| First published | 2022-05-12 |
| Weekly downloads | 0 |
| License | Open BSV License |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 1.4 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Peer-to-peer Privacy Systems Research, LLC |
| Maintainers | tonesnotes, braydude, tyeverett |

## Links

- npm: https://www.npmjs.com/package/authrite-js
- Repository: https://github.com/p2ppsr/authrite-js
- Homepage: https://github.com/p2ppsr/authrite-js#readme
- Issues: https://github.com/p2ppsr/authrite-js/issues
- npm.io page: https://npm.io/package/authrite-js

## Dependencies (7)

- [sendover](https://npm.io/package/sendover.md) ^1.3.27
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.0
- [babbage-bsv](https://npm.io/package/babbage-bsv.md) ^0.2.5
- [authrite-utils](https://npm.io/package/authrite-utils.md) ^0.3.36
- [boomerang-http](https://npm.io/package/boomerang-http.md) ^0.1.0
- [@babbage/sdk-ts](https://npm.io/package/@babbage/sdk-ts.md) ^0.2.54
- [socket.io-client](https://npm.io/package/socket.io-client.md) ^4.7.2

## Recent versions

- 0.4.36 (latest) — 2024-10-01
- 0.4.35 — 2024-09-21
- 0.4.34 — 2024-08-06
- 0.4.33 — 2024-07-17
- 0.4.32 — 2024-07-07
- 0.4.31 — 2024-06-19
- 0.4.30 — 2024-06-07
- 0.4.29 — 2024-06-07
- 0.4.28 — 2024-05-11
- 0.4.27 — 2024-05-07
- 0.4.26 — 2024-05-04
- 0.4.25 — 2024-05-04
- 0.4.24 — 2024-05-01
- 0.4.23 — 2024-04-24
- 0.4.22 — 2024-04-23
- … 96 more at https://npm.io/package/authrite-js/versions

## README

# authrite-js

JavaScript client for Authrite

The code is available [on GitHub](https://github.com/p2ppsr/authrite-js) and the package is published [on NPM](https://www.npmjs.com/package/authrite-js).

## Overview

[Authrite](https://projectbabbage.com/authrite) is a system for mutual authentication over a communications channel where both parties come to know the identity of the counterparty.
**authrite-js** provides an API for making authenticated HTTP requests from a client to a server that uses the authrite-express middleware.

During setup, the client asks for some basic information from the server and provides their identity key. The server sends back a reply, proving custody over the identity key they send back. Then, every message sent between the two parties is signed and verified, enabling everyone to have confidence in message integrity. Messages are not encrypted by Authrite, but encryption is provided by HTTPS.

## Installation

    npm i authrite-js

## Example HTTP Usage

This example demonstrates sending a simple request sent with **authrite-js**

```js
const { Authrite } = require('authrite-js')

// Authrite required parameters
const TEST_CLIENT_PRIVATE_KEY = 
'0d7889a0e56684ba795e9b1e28eb906df43454f8172ff3f6807b8cf9464994df'

const init = async () => {
    // Create a new instance of the Authrite class
    // Provide the server baseUrl, and your private identity key
    const authrite = new Authrite({
        clientPrivateKey: TEST_CLIENT_PRIVATE_KEY
    })
    // Construct a payload to send as the body of your request
    const body = {
        user: 'Bob',
        message: 'message from client'
    }
    // Create a new request to the server
    const response = await authrite.request('http://localhost:5000/sendSomeData', {
        body,
        method: 'POST',
        headers: {
        'Content-Type': 'application/json'
        }
    })
    // Retrieve the response from the server
    const responseData = JSON.parse(Buffer.from(response.body).toString('utf8'))
}

init()
```

## Example WebSocket Usage

```js
const { Authrite } = require('authrite-js')

// Authrite required parameters
// Note: The MetaNet Client can be used as a signing strategy as well
const TEST_CLIENT_PRIVATE_KEY = 
'0d7889a0e56684ba795e9b1e28eb906df43454f8172ff3f6807b8cf9464994df'

const init = async () => {
    // Create a new instance of the Authrite class
    // Provide the server baseUrl, and your private identity key
    // And make a connection request to the server with an open socket connection
    const io = await new Authrite({
        clientPrivateKey: TEST_CLIENT_PRIVATE_KEY
    }).connect('http://localhost:3000')

    // Setup an event handler
    io.on('chatMessage', (msg) => {
        // Mutual authentication has already happened at this point
        console.log(msg.text)
    })

    // Send a message to the server to get a response
    // Note: The server side must be configured correctly to receive a response
    await io.emit('chatMessage', { text: 'Hello server!' })
}

init()
```

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

*   [Authrite](#authrite)
    *   [Parameters](#parameters)
    *   [request](#request)
        *   [Parameters](#parameters-1)
    *   [connect](#connect)
        *   [Parameters](#parameters-2)
    *   [on](#on)
        *   [Parameters](#parameters-3)
    *   [emit](#emit)
        *   [Parameters](#parameters-4)
    *   [addCertificate](#addcertificate)
        *   [Parameters](#parameters-5)

### Authrite

Client-side API for establishing authenticated server communication

#### Parameters

*   `$0` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**  (optional, default `{}`)

    *   `$0.clientPrivateKey` &#x20;
    *   `$0.initialRequestPath`   (optional, default `'/authrite/initialRequest'`)
    *   `$0.signingStrategy`   (optional, default `'Babbage'`)
    *   `$0.certificates`   (optional, default `[]`)
*   `obj` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** All parameters are given in an object.

#### request

Creates a new signed authrite request and returns the result

##### Parameters

*   `requestUrl` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The URL to request on an Authrite-enabled server
*   `fetchConfig` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Config object passed to the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). The current version of Authrite only supports JSON structures for the fetch body. However, you can include a [Buffer](https://nodejs.org/api/buffer.html) as part of the json object. (optional, default `{}`)

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The response object. Fields are 'status', 'headers' and 'body' (containing an ArrayBuffer of the HTTP response body)

#### connect

Support initializing a socket connection to a server
Currently implemented as a drop-in replacement for the socket.io wrapper of WebSockets

##### Parameters

*   `connectionUrl` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the url of the server to connect to over web sockets
*   `config` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** standard socket.io configuration param (optional, default `{}`)

#### on

Configures custom client events for incoming server websocket events

##### Parameters

*   `event` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**&#x20;
*   `callback` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**&#x20;

#### emit

Emits a message to a connected server over web sockets

##### Parameters

*   `event` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**&#x20;
*   `data` **([object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [buffer](https://nodejs.org/api/buffer.html))**&#x20;

#### addCertificate

##### Parameters

*   `certificate` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Certificate produced by createCertificate to be added to the cache.

## License

The license for the code in this repository is the Open BSV License.

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