Skip to content

MeshCore MQTT broker with signature-based authentication using MeshCore public/private keys

Notifications You must be signed in to change notification settings

michaelhart/meshcore-mqtt-broker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

meshcore-mqtt-broker

A WebSocket-based MQTT broker with MeshCore public key authentication.

Features

  • WebSocket MQTT: Uses MQTT over WebSockets (not MQTT over TCP protocol)
  • Public Key Authentication: Clients authenticate using their MeshCore public keys
  • Topic Authorization: Controls access to meshcore/* topics

Authentication

Username Format

v1_{UPPERCASE_PUBLIC_KEY}

Example: v1_7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C9400

Password Format

The password is a JWT-style authentication token signed with your MeshCore Ed25519 private key using orlp/ed25519, which is used in the MeshCore firmware and in the @michaelhart/meshcore-decoder library's createAuthToken function.

import { createAuthToken } from '@michaelhart/meshcore-decoder';

const privateKey = 'YOUR_64_BYTE_PRIVATE_KEY_HEX'; // MeshCore format
const publicKey = 'YOUR_32_BYTE_PUBLIC_KEY_HEX';

const password = await createAuthToken(
  {
    publicKey: publicKey,
    aud: 'mqtt.yourdomain.com', // Must match AUTH_EXPECTED_AUDIENCE in .env
    iat: Math.floor(Date.now() / 1000),
    // Optional: add expiration
    // exp: Math.floor(Date.now() / 1000) + 3600 // 1 hour
  },
  privateKey,
  publicKey
);

The token format is: header.payload.signature where the signature is verified using Ed25519.

Configuration

All configuration is done via environment variables in a .env file.

Copy .env.example to .env and configure:

cp .env.example .env

Edit .env:

# MQTT Server Settings
MQTT_WS_PORT=8883
MQTT_HOST=0.0.0.0

# Authentication Settings
# Expected audience claim in JWT tokens (leave empty to skip validation)
AUTH_EXPECTED_AUDIENCE=mqtt.yourdomain.com

# Subscribe-Only Users (read-only monitoring accounts)
# Format: SUBSCRIBER_N=username:password
# Add as many as you need by incrementing the number
SUBSCRIBER_1=admin:your-secure-password-here
SUBSCRIBER_2=viewer:another-secure-password
SUBSCRIBER_3=monitor:yet-another-password

Subscribe-only users can read all messages but cannot publish. They're useful for monitoring, debugging, and administrative dashboards.

Installation

npm install

Usage

Development

npm run dev

Production

npm run build
npm start

Connecting Clients

JavaScript/Node.js Example

const mqtt = require('mqtt');
const { createAuthToken } = require('@michaelhart/meshcore-decoder');

const privateKey = 'YOUR_64_BYTE_PRIVATE_KEY_HEX'; // MeshCore format
const publicKey = '7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C9400';
const clientId = 'meshcore_test_client';

async function connect() {
  // Generate auth token
  const password = await createAuthToken(
    {
      publicKey: publicKey,
      aud: 'mqtt.yourdomain.com', // Must match AUTH_EXPECTED_AUDIENCE in .env
      iat: Math.floor(Date.now() / 1000),
      exp: Math.floor(Date.now() / 1000) + 86400 // 24 hours
    },
    privateKey,
    publicKey
  );

  const client = mqtt.connect('ws://localhost:8883', {
    clientId: clientId,
    username: `v1_${publicKey}`,
    password: password
  });

  client.on('connect', () => {
    console.log('Connected!');
    client.subscribe('meshcore/#');
  });

  client.on('message', (topic, message) => {
    console.log(`${topic}: ${message.toString()}`);
  });
}

connect();

Topics

Publishers can only publish to topics under meshcore/* with the format:

  • meshcore/{IATA_CODE}/{subtopic} - e.g., meshcore/SEA/packets
  • meshcore/{IATA_CODE}/{PUBLIC_KEY}/{subtopic} - e.g., meshcore/SEA/7E76...9400/packets

Where {IATA_CODE} must be a valid 3-letter IATA airport code (e.g., SEA, PDX, BOS) or test for testing and {PUBLIC_KEY} must be the full 64-character public key.

All published messages must be valid JSON and contain an origin_id field matching your authenticated public key. In the future, this requirement and the origin_id field may be removed, as they are a part of the MQTT session. For now, this is largely for backwards compatibility.

Subscribers (read-only users) can subscribe to any topic including wildcards like meshcore/#.

Deployment

This project is designed to be deployed via Nixpacks (e.g., to Dokploy) similar to the ingestor project.

The build process will:

  1. Install dependencies
  2. Compile TypeScript to JavaScript
  3. Run the compiled server

For setting up with TLS using Cloudflare Tunnels, see docs/cloudflare-tunnels.md. This is the recommended way to deploy the MQTT broker.

License

MIT License

Copyright (c) 2025 Michael Hart michaelhart@michaelhart.me (https://github.com/michaelhart)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

MeshCore MQTT broker with signature-based authentication using MeshCore public/private keys

Topics

Resources

Stars

Watchers

Forks