Authentication Mixin

Server-side authentication via BasicAuthMixin.

BasicAuthMixin

Authentication mixin for AsgiServer.

Provides server.authenticate(scope) as the single extension point for auth. Supports bearer, basic, JWT backends with O(1) lookup at request time.

Usage:
class MyServer(BasicAuthMixin, RoutingClass):
def __init__(self):

BasicAuthMixin.__init__(self, bearer={…}, basic={…}) …

# In middleware or dispatcher: auth = server.authenticate(scope)

class genro_asgi.server.auth_mixin.BasicAuthMixin(**entries)[source]

Bases: object

Auth mixin: bearer, basic, JWT with O(1) credential lookup.

_auth_config

Dict mapping auth type to credentials dict.

__init__(**entries)[source]

Initialize auth config from keyword arguments.

Parameters:

**entries (Any) – Auth configuration by type (bearer, basic, jwt).

authenticate(scope)[source]

Authenticate request using configured backends.

Parameters:

scope (MutableMapping[str, Any]) – ASGI scope with _headers dict (populated by @headers_dict).

Return type:

dict[str, Any] | None

Returns:

Auth dict with tags/identity/backend if valid, None if no header.

Raises:

HTTPException – 401 if credentials present but invalid.

verify_credentials(username, password)[source]

Verify username/password against basic auth config.

Parameters:
  • username (str) – The username to verify.

  • password (str) – The password to verify.

Return type:

dict[str, Any] | None

Returns:

Dict with tags and identity if valid, None otherwise.