@@ -25,11 +25,16 @@ def __init__(self, detail: str) -> None:
25
25
class UnauthorizedHttp (HTTPException ):
26
26
"""HTTP exception for authentication failures"""
27
27
28
- def __init__ (self , detail : str ) -> None :
28
+ def __init__ (self , detail : str , authorization_url : str | None = None , client_id : str | None = None ) -> None :
29
+ header_value = 'Bearer'
30
+ if authorization_url :
31
+ header_value += f', authorization_uri="{ authorization_url } "'
32
+ if client_id :
33
+ header_value += f', client_id="{ client_id } "'
29
34
super ().__init__ (
30
35
status_code = status .HTTP_401_UNAUTHORIZED ,
31
36
detail = {"error" : "invalid_token" , "message" : detail },
32
- headers = {"WWW-Authenticate" : "Bearer" },
37
+ headers = {"WWW-Authenticate" : header_value },
33
38
)
34
39
35
40
@@ -103,10 +108,12 @@ def InvalidRequest(detail: str, request: HTTPConnection) -> InvalidRequestHttp |
103
108
return InvalidRequestWebSocket (detail )
104
109
105
110
106
- def Unauthorized (detail : str , request : HTTPConnection ) -> UnauthorizedHttp | UnauthorizedWebSocket :
111
+ def Unauthorized (
112
+ detail : str , request : HTTPConnection , authorization_url : str | None = None , client_id : str | None = None
113
+ ) -> UnauthorizedHttp | UnauthorizedWebSocket :
107
114
"""Factory function for unauthorized exceptions"""
108
115
if request .scope ["type" ] == "http" :
109
- return UnauthorizedHttp (detail )
116
+ return UnauthorizedHttp (detail , authorization_url , client_id )
110
117
return UnauthorizedWebSocket (detail )
111
118
112
119
0 commit comments