Algoritma: ACL & Yetkilendirme
I₁ invariant'ı: Hiçbir kullanıcı, izin verilmemiş bir topic'e SUBSCRIBE veya PUBLISH yapamaz.
Akış Şeması
ACL Kontrolü (Pseudocode)
FUNCTION aclAllow(userId, topic, action):
// action: "read" (subscribe) veya "write" (publish)
allowedTopics = aclStore.getUserTopics(userId, action)
RETURN topic in allowedTopics
// veya pattern: allowedTopics.some(t => matchTopic(t, topic))Topic Eşleme Örnekleri
- Tam eşleşme:
userId→["risk.alerts", "prices.btc"] - Wildcard:
userId→["prices.*"]→prices.btc,prices.ethizinli - Rol tabanlı:
role→ topic listesi; kullanıcı rolüne göre izin
Node.js: ACL Store (Bellek Örneği)
javascript
const aclStore = {
// userId -> { read: ['topic1', 'topic2'], write: ['topic1'] }
_data: new Map(),
set(userId, permissions) {
this._data.set(userId, permissions);
},
allow(userId, topic, action) {
const p = this._data.get(userId);
if (!p) return false;
const list = p[action]; // 'read' | 'write'
if (!list) return false;
return list.includes(topic) || list.some(t => t.endsWith('*') && topic.startsWith(t.slice(0, -1)));
}
};
// Kullanım
aclStore.set('user1', { read: ['risk.alerts', 'prices.*'], write: ['orders'] });
if (!aclStore.allow(ws.userId, msg.topic, msg.type === 'subscribe' ? 'read' : 'write')) {
ws.send(JSON.stringify({ type: 'error', code: 'ACL_DENIED', topic: msg.topic }));
return;
}Redis ile ACL (Örnek Key Yapısı)
acl:user:<userId>:read -> Set ["risk.alerts", "prices.btc"]
acl:user:<userId>:write -> Set ["orders"]javascript
async function aclAllow(redis, userId, topic, action) {
const key = `acl:user:${userId}:${action}`;
const allowed = await redis.sMembers(key);
return allowed.includes(topic) || allowed.some(t => t.endsWith('*') && topic.startsWith(t.slice(0, -1)));
}Tam örnek: Gateway: ACL + Dedup.