1616F = TypeVar ("F" , bound = Callable [..., ResponseReturnValue ])
1717
1818
19+ def _coerce_header (value : str | None ) -> str :
20+ if value is None :
21+ return "unknown"
22+ cleaned = value .strip ()
23+ return cleaned if cleaned else "unknown"
24+
25+
26+ def _normalize_cert_subject (value : str | None ) -> str :
27+ subject = _coerce_header (value )
28+ if subject == "unknown" :
29+ return subject
30+ prefix = "subject="
31+ if subject .lower ().startswith (prefix ):
32+ remainder = subject [len (prefix ) :].lstrip ()
33+ return remainder or "unknown"
34+ return subject
35+
36+
1937def route (rule : str , ** options : Any ) -> Callable [[F ], F ]:
2038 return cast (Callable [[F ], F ], app .route (rule , ** options ))
2139
@@ -42,10 +60,8 @@ def health() -> Dict[str, str]:
4260@route ("/" )
4361def index () -> str :
4462 with record_route_metrics ("index" ):
45- cert_subject = request .headers .get ("X-Client-Subject" , "unknown" ) or "unknown"
46- if cert_subject .startswith ("Subject=" ):
47- cert_subject = cert_subject .replace ("Subject=" , "" , 1 )
48- device_id = request .headers .get ("X-Device-ID" , "unknown" ) or "unknown"
63+ cert_subject = _normalize_cert_subject (request .headers .get ("X-Client-Subject" ))
64+ device_id = _coerce_header (request .headers .get ("X-Device-ID" ))
4965 return (
5066 f"Hello from keep protected app!\n "
5167 f"Client cert subject: { cert_subject } \n "
0 commit comments