# @xmpp/sasl2

> XMPP SASL2 for JavaScript

Latest version **0.14.0** (published 2025-10-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install @xmpp/sasl2
pnpm add @xmpp/sasl2
yarn add @xmpp/sasl2
bun add @xmpp/sasl2
```

## Health

**Score 43/100 (D)** — status: stable.

Positive: has types package; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.14.0 |
| Published | 2025-10-29 |
| First published | 2025-10-29 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | separate (@types/xmpp__sasl2) |
| Module format | ESM + CommonJS |
| Node | >= 14 |
| Dependencies | 6 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2282 |
| Maintainers | sonny |
| Keywords | XMPP, sasl |

## Links

- npm: https://www.npmjs.com/package/@xmpp/sasl2
- Repository: https://github.com/xmppjs/xmpp.js
- Homepage: https://github.com/xmppjs/xmpp.js/tree/main/packages/sasl2
- Issues: http://github.com/xmppjs/xmpp.js/issues
- npm.io page: https://npm.io/package/@xmpp/sasl2

## Dependencies (6)

- [@xmpp/jid](https://npm.io/package/@xmpp/jid.md) ^0.14.0
- [@xmpp/xml](https://npm.io/package/@xmpp/xml.md) ^0.14.0
- [@xmpp/sasl](https://npm.io/package/@xmpp/sasl.md) ^0.14.0
- [@xmpp/error](https://npm.io/package/@xmpp/error.md) ^0.14.0
- [@xmpp/base64](https://npm.io/package/@xmpp/base64.md) ^0.14.0
- [@xmpp/events](https://npm.io/package/@xmpp/events.md) ^0.14.0

## Recent versions

- 0.14.0 (latest) — 2025-10-29

## README

# SASL2

SASL2 Negotiation for `@xmpp/client`.

Included and enabled in `@xmpp/client`.

## Usage

### object

```js
import { xmpp } from "@xmpp/client";

const client = xmpp({
  credentials: {
    username: "foo",
    password: "bar",
  },
});
```

### function

Instead, you can provide a function that will be called every time authentication occurs (every (re)connect).

Uses cases:

- Have the user enter the password every time
- Do not ask for password before connection is made
- Debug authentication
- Using a SASL mechanism with specific requirements
- Fetch credentials from a secure database

```js
import { xmpp, xml } from "@xmpp/client";

const client = xmpp({
  credentials: authenticate,
});

async function onAuthenticate(authenticate, mechanisms) {
  console.debug("authenticate", mechanisms);
  const credentials = {
    username: await prompt("enter username"),
    password: await prompt("enter password"),
  };
  console.debug("authenticating");

  // userAgent is optional
  const userAgent = await getUserAgent();

  await authenticate(credentials, mechanisms[0], userAgent);
  console.debug("authenticated");
}

async function getUserAgent() {
  let id = localStorage.get("user-agent-id");
  if (!id) {
    id = await crypto.randomUUID();
    localStorage.set("user-agent-id", id);
  }
  // https://xmpp.org/extensions/xep-0388.html#initiation
  return xml("user-agent", { id }, [
    xml("software", {}, "xmpp.js"),
    xml("device", {}, "Sonny's laptop"),
  ]);
}
```

## References

- [XEP-0388: Extensible SASL Profile](https://xmpp.org/extensions/xep-0388.html)

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