b3d90e1 · Redact ids outside the wall · 5 days ago

python34 lines · 939 B
1"""Scope is a wall. Adapters never see ids outside the mapping."""
2
3from __future__ import annotations
4
5import logging
6
7from mapping import Mapping
8
9
10class ScopeError(PermissionError):
11 pass
12
13
14class ScopeGuard:
15 def __init__(self, mapping: Mapping) -> None:
16 self.scope = mapping.scope
17 self._ids = mapping.ids()
18
19 def allow(self, channel_id: str) -> bool:
20 return channel_id in self._ids
21
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