Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
zap_auth.hpp
Go to the documentation of this file.
1/*
2 _____ _ _ _
3 |__ /__ _ _ __ / \ _ _| |_| |__
4 / // _` | '_ \ / _ \| | | | __| '_ \
5 / /| (_| | |_) |/ ___ \ |_| | |_| | | |
6 /____\__,_| .__//_/ \_\__,_|\__|_| |_|
7 |_|
8
9ZeroMQ Authentication Protocol (ZAP, RFC 27) handler
10Copyright (C) 2026 Paolo Bosetti
11*/
12
13#pragma once
14#ifdef _WIN32
15#ifndef NOMINMAX
16#define NOMINMAX
17#endif
18#ifndef WIN32_LEAN_AND_MEAN
19#define WIN32_LEAN_AND_MEAN
20#endif
21#include <winsock2.h>
22#endif
23
24#include <atomic>
25#include <memory>
26#include <mutex>
27#include <set>
28#include <string>
29#include <thread>
30
31#include <zmq.hpp>
32
33namespace Mads {
34
54class ZapAuth {
55public:
61 explicit ZapAuth(zmq::context_t &context) : _context(context) {}
62
63 ~ZapAuth() { stop(); }
64
65 ZapAuth(const ZapAuth &) = delete;
66 ZapAuth &operator=(const ZapAuth &) = delete;
67 ZapAuth(ZapAuth &&) = delete;
68 ZapAuth &operator=(ZapAuth &&) = delete;
69
80 void start();
81
89 void stop();
90
92 void set_verbose(bool verbose) { _verbose.store(verbose); }
93
100 void configure_domain(const std::string &domain);
101
110 void allow(const std::string &address);
111
121 void configure_curve(const std::string &z85_public_key);
122
124 int granted() const { return _granted.load(); }
125
127 int denied() const { return _denied.load(); }
128
130 static constexpr const char *endpoint = "inproc://zeromq.zap.01";
131
132private:
134 void _serve();
135
147 bool _authorise(const std::string &domain, const std::string &address,
148 const std::string &mechanism,
149 const std::string &credentials, std::string &reason) const;
150
151 zmq::context_t &_context;
152 std::unique_ptr<zmq::socket_t> _socket;
153 std::thread _thread;
154 std::atomic<bool> _stop{false};
155 std::atomic<bool> _running{false};
156 std::atomic<bool> _verbose{false};
157 std::atomic<int> _granted{0};
158 std::atomic<int> _denied{0};
159 mutable std::mutex _config_mutex;
160 std::string _domain = "*";
161 std::set<std::string> _allowed_addresses;
162 std::set<std::string> _allowed_curve_keys;
163};
164
167 std::string public_key;
168 std::string secret_key;
169};
170
182
189std::string z85_encode(const std::string &data);
190
191} // namespace Mads
In-process ZAP (ZeroMQ Authentication Protocol, RFC 27) handler.
Definition zap_auth.hpp:54
ZapAuth(ZapAuth &&)=delete
int denied() const
Number of requests denied so far (test/diagnostic aid).
Definition zap_auth.hpp:127
void configure_curve(const std::string &z85_public_key)
Add a client public key to the CURVE allowlist.
ZapAuth & operator=(ZapAuth &&)=delete
void start()
Bind the ZAP endpoint and start servicing requests.
ZapAuth(zmq::context_t &context)
Construct a handler for the given context. Does not bind yet.
Definition zap_auth.hpp:61
ZapAuth & operator=(const ZapAuth &)=delete
void stop()
Stop servicing requests and join the worker thread.
ZapAuth(const ZapAuth &)=delete
void set_verbose(bool verbose)
Log every authentication decision to stdout.
Definition zap_auth.hpp:92
void allow(const std::string &address)
Add an address to the whitelist.
void configure_domain(const std::string &domain)
Restrict the handler to one ZAP domain.
static constexpr const char * endpoint
The well-known ZAP endpoint mandated by RFC 27.
Definition zap_auth.hpp:130
int granted() const
Number of requests granted so far (test/diagnostic aid).
Definition zap_auth.hpp:124
Definition agent.hpp:67
CurveKeypair generate_keypair()
Generate a fresh CURVE keypair.
std::string z85_encode(const std::string &data)
Z85-encode a binary buffer.
A CURVE keypair, both keys Z85-encoded (40 characters each).
Definition zap_auth.hpp:166
std::string public_key
Definition zap_auth.hpp:167
std::string secret_key
Definition zap_auth.hpp:168