# ECWT

[![npm version](https://img.shields.io/npm/v/ecwt.svg)](https://www.npmjs.com/package/ecwt)
[![Open on npmx.dev](https://npmx.dev/api/registry/badge/version/ecwt?style=shieldsio&label=npmx.dev)](https://npmx.dev/package/ecwt)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

ECWT is a module for creating and verifying encrypted CBOR Web Tokens. It is designed to be used in situations where JWT is used, but there are major differences:

| | JWT | ECWT |
| --- | --- | --- |
| Encoding | 🧐 JSON with base64 | ✅ Compact CBOR with Base62 |
| Binary data | 🧐 Double base64 encoding | ✅ Supported out of the box |
| Security | 📝 Signed <br> Payload is readable by everyone | 🔒 AES-SIV authenticated encryption <br> Payload is readable only with the secret key |
| Metadata | ➕ Type and algorithm, increases size | ✅ One-byte format version |
| Revocation | 🧑‍💻 Requires additional implementation | ✅ Included with Redis |

## Installation

ECWT requires Node.js 22.13 or newer and supports both ESM and CommonJS. Install it with the required `@kirick/snowflake` dependency:

```sh
bun install ecwt @kirick/snowflake
# or
pnpm install ecwt @kirick/snowflake
# or
npm install ecwt @kirick/snowflake
```

### Some dependencies

`EcwtFactory` depends on other modules, so you might be need to install them too.

#### `@kirick/snowflake` to create unique IDs (required)

For documentation, see [snowflake repository](https://github.com/kirick-ts/snowflake).

```javascript
import { SnowflakeFactory } from '@kirick/snowflake';

const snowflakeFactory = new SnowflakeFactory({
  server_id: 0,
  worker_id: 0,
});
```

#### `redis` to store revoked tokens (optional)

Token revocation requires Redis server 7.4 or newer for hash field expiration (`HPEXPIRE`).

```javascript
import { createClient } from 'redis';

const redisClient = createClient({
  socket: {
    host: 'localhost',
    port: 6379,
  },
});

await redisClient.connect();
```

#### `lru-cache` to avoid decrypt the same token multiple times (optional)

`lru-cache` is included with ECWT. Pass cache options as `options.lru_cache` to let each factory create its own private cache. The same options can be reused across factories without sharing cached tokens. Omit `options.lru_cache` to disable caching.

Without a cache, every verification decrypts and validates the token. With a cache, repeated verification reuses the decrypted bytes while still decoding and validating the payload and checking expiration and revocation. Each access to `ecwt.data` decodes and validates a fresh copy of the payload, including its byte arrays.

```javascript
const lru_cache = {
  max: 1000, // maximum of 1000 items
};
```

#### Validation library of your choice (optional)

By specifying the schema, you also validate the payloads. The validator is a synchronous function of type `(value: D) => D` that returns validated data or throws. It should preserve the data's types and accept its CBOR-decoded representation; transformations between strings and class instances require codecs, which are not yet supported.

In our example, we use [valibot](https://valibot.dev) library.

```javascript
import * as v from 'valibot';

const validator = v.parser(
  v.object({
    user_id: v.pipe(
      v.number(),
      v.maxValue(10),
    ),
    nick: v.pipe(
      v.string(),
      v.maxLength(10),
    ),
  }),
);
```

That validator will prevent creating tokens for users with ID greater than 10 and nicknames longer than 10 characters.

## Usage Examples

### Initializing the EcwtFactory

First, configure the EcwtFactory with your environment dependencies:

```javascript
import { EcwtFactory } from 'ecwt';
import { SnowflakeFactory } from '@kirick/snowflake';
import { createClient } from 'redis';

// Required: Initialize SnowflakeFactory for token ID generation
const snowflakeFactory = new SnowflakeFactory({
  server_id: 0,
  worker_id: 0,
});

// Optional: Cache decoded tokens for repeated verification
const lru_cache = {
  max: 1000, // Maximum cache size
};

// Optional: Set up Redis client for token revocation capabilities
const redisClient = createClient({
  socket: {
    host: 'localhost',
    port: 6379,
  },
});
await redisClient.connect();

// Initialize the factory with your configuration
const ecwtFactory = new EcwtFactory({
  redisClient,
  snowflakeFactory,
  options: {
    lru_cache,
    // Unique namespace for Redis keys to prevent collisions
    namespace: 'auth-service',
    // Your 64-byte random secret key for AES-SIV (store securely)
    key: Uint8Array.fromBase64('YOUR_BASE64_KEY'),
    // Optional: maximum serialized token length in Base62 characters, defaults to 4000
    max_token_length: 4000,
    // Schema validator for payload structure validation
    validator: myValidator,
  },
});
```

### Token Generation

Generate tokens with payload and expiration controls:

```javascript
// Create an access token with a 30-minute expiration
const ecwt = await ecwtFactory.create(
  {
    user_id: 123,
    name: "John Doe",
    role: "admin"
  },
  {
    ttl: 30 * 60 // 30 minutes in seconds
  }
);

// Get string representation of the token
const serializedToken = ecwt.token;

// Access token metadata
console.log(`Token ID: ${ecwt.id}`);
console.log(`Expiration timestamp: ${ecwt.ts_expired}`);
console.log(`Remaining validity: ${ecwt.ttl} seconds`);
```

### Token Verification

Starting with 0.5.0:

- only AES-SIV tokens with format version `0xF0` are accepted. Older EvilCrypt tokens must be reissued; verification rejects them with `EcwtParseError`;
- binary values decoded during verification are `Uint8Array` instances instead of Node.js `Buffer`;
  - binary encoding remains compatible with AES-SIV tokens from 0.4.1.

Verify tokens with appropriate error handling:

```javascript
import {
  EcwtExpiredError,
  EcwtRevokedError,
  EcwtParseError,
  EcwtInvalidError
} from 'ecwt';

try {
  // Verify and decode the token
  const verifiedToken = await ecwtFactory.verify(serializedToken);

  // Access verified payload data
  const { user_id, name, role } = verifiedToken.data;

  // Proceed with authenticated operation

} catch (error) {
  // Handle specific verification failures
  if (error instanceof EcwtExpiredError) {
    return respondWithError(401, "Authentication expired");
  } else if (error instanceof EcwtRevokedError) {
    return respondWithError(401, "Authentication revoked");
  } else if (error instanceof EcwtParseError) {
    return respondWithError(400, "Malformed authentication token");
  } else if (error instanceof EcwtInvalidError) {
    return respondWithError(401, "Invalid authentication token");
  } else {
    logger.error("Token verification error", error);
    return respondWithError(500, "Authentication service error");
  }
}
```

For exception-free verification, use `safeVerify`:

```javascript
const { success, ecwt } = await ecwtFactory.safeVerify(serializedToken);

if (success) {
  // Proceed with authenticated request
  const userData = ecwt.data;
  return processAuthenticatedRequest(userData);
} else if (ecwt) {
  // Token structure was valid but failed verification
  logger.info(`Auth failure: token ${ecwt.id} is invalid`);
  return respondWithError(401, "Authentication token invalid");
} else {
  // Unparsable token structure
  logger.warn(`Auth failure: malformed token received`);
  return respondWithError(400, "Malformed authentication token");
}
```

### Token Revocation

Implement secure session termination with token revocation:

```javascript
// Terminate user session by revoking the token
await accessToken.revoke();
logger.info(`Session terminated: Token ${accessToken.id} revoked`);

// Subsequent verification attempts will fail with EcwtRevokedError
try {
  await ecwtFactory.verify(accessToken.token);
} catch (error) {
  if (error instanceof EcwtRevokedError) {
    // Expected behavior for revoked tokens
    logger.debug("Token verification correctly rejected revoked token");
  }
}
```

### Advanced: Token Size Optimization

To reduce token size, use SenML key mapping that replaces string object keys with numeric identifiers throughout your entire payload structure. This compression works at any nesting depth.

> **Important:** The SenML key mapping configuration establishes a permanent relationship between field names and their numeric identifiers. Once deployed, these mappings must remain consistent to maintain compatibility with existing tokens. Adding new fields is acceptable, but changing existing mappings can break previously issued tokens.

```javascript
// Standard configuration without key mapping
const standardFactory = new EcwtFactory({
  /* Core dependencies */
  options: {
    namespace: 'auth-service',
    key: encryptionKey,
  },
});

// Optimized configuration with key mapping
const optimizedFactory = new EcwtFactory({
  /* Core dependencies */
  options: {
    namespace: 'auth-service',
    key: encryptionKey,
    senml_key_map: {
      user_id: 1,
      name: 2,
      roles: 3,
      permissions: 4,
      metadata: 5,
      last_login: 6,
    },
  },
});

// Measure token size difference
const payload = {
  user_id: 12345,
  name: "John Smith",
  roles: ["admin", "editor"],
  permissions: ["read", "write", "delete"],
  metadata: { last_login: Date.now() },
};

const standardToken = await standardFactory.create(payload, { ttl: 3600 });
const optimizedToken = await optimizedFactory.create(payload, { ttl: 3600 });

console.log(`Standard token size: ${standardToken.token.length} bytes`);
console.log(`Optimized token size: ${optimizedToken.token.length} bytes`);
console.log(`Size reduction: ${((1 - optimizedToken.token.length / standardToken.token.length) * 100).toFixed(2)}%`);

// Example output (sizes depend on the payload):
// > Standard token size: 193 bytes
// > Optimized token size: 132 bytes
// > Size reduction: 31.61%
```

## Future Plans

ECWT plans to support [Standard Codecs](https://github.com/standard-schema/standard-schema/pull/180) once the specification is finalized and supported by validation libraries. Codecs will be accepted alongside the existing validation function.

This will allow stored data and application data to use different types. For example, `create()` could accept an IP address class instance, encode it as a string in CBOR, and reconstruct the instance when token data is read. The existing `(value: D) => D` validation function will remain supported for payloads using the same types in both directions.
