Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
68d9cdd
fix: correct for "error: Value of type "Collection[str]" is not index…
jason-famedly Apr 28, 2026
ad2f438
chore: all functions should have return typing. This should be all th…
jason-famedly Apr 28, 2026
3f186eb
chore: all functions should have return typing. This should be all th…
jason-famedly Apr 29, 2026
680f280
chore: type the global admins object used for the ModuleApiTestCase c…
jason-famedly Apr 29, 2026
c1718d7
chore: correct arg typing on parse_auth(), and remove potential shado…
jason-famedly Apr 29, 2026
98839d4
chore: Move the auth header generation classes to the types.py file a…
jason-famedly Apr 29, 2026
162db6c
chore: Remove explicit Deferred creation as these functions are regul…
jason-famedly Apr 29, 2026
65de299
chore: remove unnecessary Optional typing import
jason-famedly Apr 29, 2026
5f676bb
chore: correct typing and arg name on __init__() for TokenAuthenticator
jason-famedly Apr 29, 2026
f26d04a
chore: resource rendering functions return bytes
jason-famedly Apr 29, 2026
f105893
chore: move all rest Resource to their own file
jason-famedly Apr 29, 2026
b438108
chore: break nested class definitions out to top level
jason-famedly Apr 29, 2026
b88bd7f
chore: correct typing from 'object' to 'OIDCConfig' to fix mypy warni…
jason-famedly Apr 29, 2026
40850c5
chore: Resources should have a super() call to establish it's 'childr…
jason-famedly Apr 29, 2026
c16963d
chore: correct argument type for parse_validator() to reflect that it…
jason-famedly Apr 29, 2026
b3e2506
chore: clean up the signatures of the four main auth checking functio…
jason-famedly Apr 29, 2026
c0f5063
refactor: mount 'hs.mockmod' onto the ModuleApiTestCase directly and …
jason-famedly Apr 29, 2026
2616381
chore: add guaranteed attributes to TokenAuthenticatorConfig and type…
jason-famedly Apr 29, 2026
5837066
chore: avoid attribute magic initializing TokenAuthenticator, these a…
jason-famedly Apr 29, 2026
966098e
chore: refactor and unify how config attributes are pulled out and na…
jason-famedly Apr 29, 2026
05bb69f
fix: typing on test construction for JWE token. One is ignored, the o…
jason-famedly Apr 29, 2026
891cf93
chore: Strictly type the auth_checkers object passed to register_pass…
jason-famedly Apr 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions synapse_token_authenticator/auth_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from __future__ import annotations

from base64 import b64encode
from dataclasses import dataclass
from typing import Protocol


class HttpAuth(Protocol):
def header_map(self) -> dict[bytes, list[bytes]]:
"""Retrieve the mapping for the authorization for Header generation"""
...


@dataclass
class NoAuth:
def header_map(self) -> dict[bytes, list[bytes]]:
return {}


@dataclass
class BasicAuth:
username: str
password: str

def header_map(self) -> dict[bytes, list[bytes]]:
return basic_auth(self.username, self.password)


@dataclass
class BearerAuth:
token: str

def header_map(self) -> dict[bytes, list[bytes]]:
return bearer_auth(self.token)


def parse_auth(d: dict | list) -> HttpAuth:
if isinstance(d, dict):
_type = d.pop("type")
if _type is None:
return NoAuth()
elif _type == "basic":
return BasicAuth(**d)
elif _type == "bearer":
return BearerAuth(**d)
else:
raise Exception(f"Unknown HttpAuth type {_type}")
elif isinstance(d, list):
_type = d.pop(0)
if _type is None:
return NoAuth()
elif _type == "basic":
return BasicAuth(*d)
elif _type == "bearer":
return BearerAuth(*d)
else:
raise Exception(f"Unknown HttpAuth type {_type}")
else:
raise Exception("HttpAuth parsing failed, expected list or dict")


def basic_auth(username: str, password: str) -> dict[bytes, list[bytes]]:
authorization = b64encode(
b":".join((username.encode("utf8"), password.encode("utf8")))
)
return {b"Authorization": [b"Basic " + authorization]}


def bearer_auth(token: str) -> dict[bytes, list[bytes]]:
return {b"Authorization": [b"Bearer " + token.encode("utf8")]}
16 changes: 8 additions & 8 deletions synapse_token_authenticator/claims_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
]


def parse_validator(d: dict) -> Validator:
def parse_validator(d: dict | list) -> Validator:
if isinstance(d, dict):
type = d.pop("type")
if type == "exist":
Expand Down Expand Up @@ -94,7 +94,7 @@ def validate(self, x: Any) -> bool:
class Not:
validator: Validator

def __post_init__(self):
def __post_init__(self) -> None:
self.validator = parse_validator(self.validator)

def validate(self, x: Any) -> bool:
Expand All @@ -114,7 +114,7 @@ class MatchesRegex:
regex: str
full_match: bool | None = True

def __post_init__(self):
def __post_init__(self) -> None:
self.regex_prog = re.compile(self.regex)

def validate(self, s: Any) -> bool:
Expand All @@ -130,7 +130,7 @@ def validate(self, s: Any) -> bool:
class AnyOf:
validators: List[Validator]

def __post_init__(self):
def __post_init__(self) -> None:
self.validators = list(map(lambda v: parse_validator(v), self.validators))

def validate(self, x: Any) -> bool:
Expand All @@ -141,7 +141,7 @@ def validate(self, x: Any) -> bool:
class AllOf:
validators: List[Validator]

def __post_init__(self):
def __post_init__(self) -> None:
self.validators = list(map(lambda v: parse_validator(v), self.validators))

def validate(self, x: Any) -> bool:
Expand All @@ -153,7 +153,7 @@ class In:
path: str | List[str]
validator: Optional[Validator] = None

def __post_init__(self):
def __post_init__(self) -> None:
if not self.path:
raise Exception("Path list is empty")
if self.validator:
Expand All @@ -172,7 +172,7 @@ def validate(self, x: Any) -> bool:
class ListAllOf:
validator: Validator

def __post_init__(self):
def __post_init__(self) -> None:
if self.validator:
self.validator = parse_validator(self.validator)

Expand All @@ -186,7 +186,7 @@ def validate(self, list_: Any) -> bool:
class ListAnyOf:
validator: Validator

def __post_init__(self):
def __post_init__(self) -> None:
if self.validator:
self.validator = parse_validator(self.validator)

Expand Down
Loading
Loading