Skip to content

Commit

Permalink
prefer more generic Mapping over dict type (#10)
Browse files Browse the repository at this point in the history
* prefer more generic Mapping over dict type

typically it's better to specify more generic types for input parameters,
and specific types our return values. In this case changing headers
param from dict to Mapping should resolve typing issues when eg.
starlette.requests.headers.Headers instance is passed to
get_principal_from_headers()

* collections.abc.Mapping instead of deprecated typing.Mapping
  • Loading branch information
edvardm authored Jul 16, 2024
1 parent e364265 commit 98aa39a
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions audit_log/headers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from collections.abc import Mapping
from dataclasses import dataclass
from urllib.parse import urlparse

Expand Down Expand Up @@ -65,14 +66,14 @@ def parse_spiffe(xfcc_header: str) -> ParsedSPIFFE:


def get_principal_from_headers(
headers: dict[str, str],
headers: Mapping[str, str],
) -> Principal:
"""Get principal from headers, supports mTLS, headers set in Istio, and JWTs.
Note: Do not use this to handle your auth, it expects auth to already be handled elsewhere and this is just to help get principals.
Args:
headers (dict[str, str]): Headers with all keys lowercase
headers (Mapping[str, str]): Headers with all keys lowercase
Raises:
AuditPrincipalError: Cannot get a principal from the headers
Expand Down

0 comments on commit 98aa39a

Please sign in to comment.