Skip to content

Commit 7512a1c

Browse files
davidhuserJonasKs
authored andcommitted
feat: include feedback to have backwards-compatible InvalidAuth
1 parent 8f388af commit 7512a1c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

fastapi_azure_auth/auth.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
Forbidden,
2222
ForbiddenHttp,
2323
ForbiddenWebSocket,
24+
InvalidAuthHttp,
25+
InvalidAuthWebSocket,
2426
InvalidRequest,
2527
InvalidRequestHttp,
2628
Unauthorized,
@@ -234,6 +236,8 @@ async def __call__(self, request: HTTPConnection, security_scopes: SecurityScope
234236
log.warning('Unable to verify token. No signing keys found')
235237
raise Unauthorized(detail='Unable to verify token, no signing keys found', request=request)
236238
except (
239+
InvalidAuthHttp,
240+
InvalidAuthWebSocket,
237241
InvalidRequestHttp,
238242
UnauthorizedHttp,
239243
UnauthorizedWebSocket,

fastapi_azure_auth/exceptions.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,40 @@ def __init__(self, detail: str) -> None:
6262
)
6363

6464

65+
# --- start backwards-compatible code ---
66+
def InvalidAuth(detail: str, request: HTTPConnection) -> UnauthorizedHttp | UnauthorizedWebSocket:
67+
"""
68+
Legacy factory function that maps to Unauthorized for backwards compatibility.
69+
Returns the correct exception based on the connection type.
70+
TODO: Remove in v6.0.0
71+
"""
72+
if request.scope['type'] == 'http':
73+
# Convert the legacy format to new format
74+
return UnauthorizedHttp(detail)
75+
return UnauthorizedWebSocket(detail)
76+
77+
78+
class InvalidAuthHttp(UnauthorizedHttp):
79+
"""Legacy HTTP exception class that maps to UnauthorizedHttp
80+
TODO: Remove in v6.0.0
81+
"""
82+
83+
def __init__(self, detail: str) -> None:
84+
super().__init__(detail)
85+
86+
87+
class InvalidAuthWebSocket(UnauthorizedWebSocket):
88+
"""Legacy WebSocket exception class that maps to UnauthorizedWebSocket
89+
TODO: Remove in v6.0.0
90+
"""
91+
92+
def __init__(self, detail: str) -> None:
93+
super().__init__(detail)
94+
95+
96+
# --- end backwards-compatible code ---
97+
98+
6599
def InvalidRequest(detail: str, request: HTTPConnection) -> InvalidRequestHttp | InvalidRequestWebSocket:
66100
"""Factory function for invalid request exceptions (HTTP only, as request validation happens pre-connection)"""
67101
if request.scope['type'] == 'http':

0 commit comments

Comments
 (0)