File size: 816 Bytes
09da05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""Authentication and authorization helpers for UI clients."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Dict


@dataclass
class AuthService:
    """Very small in-memory auth registry for illustration purposes.
    It has to manage authentication and authorization, user access, permissions, and ensure only authorized users can interact
    with certain parts of the system"""

    _tokens: Dict[str, str] = field(default_factory=dict)

    def register(self, user: str, token: str) -> None:
        """Register a token for a given user."""

        self._tokens[user] = token

    def is_authorized(self, user: str, token: str) -> bool:
        """Validate whether the provided token matches the stored one."""

        return self._tokens.get(user) == token