b3d90e1 · Redact ids outside the wall · 5 days ago
1
"""Scope is a wall. Adapters never see ids outside the mapping."""2
3
from __future__ import annotations4
5
import logging6
7
from mapping import Mapping8
9
10
class ScopeError(PermissionError):11
pass12
13
14
class ScopeGuard:15
def __init__(self, mapping: Mapping) -> None:16
self.scope = mapping.scope17
self._ids = mapping.ids()18
19
def allow(self, channel_id: str) -> bool:20
return channel_id in self._ids21
22
def redact(self, channel_id: str) -> str:23
return channel_id if self.allow(channel_id) else "" 24
25
def require(self, channel_id: str, action: str) -> None:26
if not self.allow(channel_id):27
raise ScopeError(28
f"blocked {action}: outside scope {self.scope!r}"29
)30
31
def drop(self, channel_id: str, reason: str) -> None:32
# The raw id never leaves this function.33
logging.info("dropped event source= reason=%s" , reason)34