@@ -73,22 +73,25 @@ async def get_current_user(
7373 whose username is the site client's `client_id`.
7474
7575 Raises an exception if the token is invalid.
76+
77+ Reference: The following web page contains information about JWT claims:
78+ https://auth0.com/docs/secure/tokens/json-web-tokens/json-web-token-claims
7679 """
7780
7881 # Define some exceptions, which contain actionable—but not sensitive—information.
7982 invalid_subject_exception = HTTPException (
8083 status_code = status .HTTP_401_UNAUTHORIZED ,
81- detail = "Access token is invalid. Please log in again. Details: The access token contains an invalid subject. " ,
84+ detail = "Access token is invalid. Please log in again." ,
8285 headers = {"WWW-Authenticate" : "Bearer" },
8386 )
8487 invalid_claims_exception = HTTPException (
8588 status_code = status .HTTP_401_UNAUTHORIZED ,
86- detail = "Access token is invalid. Please log in again. Details: The access token contains invalid claims. " ,
89+ detail = "Access token is invalid. Please log in again." ,
8790 headers = {"WWW-Authenticate" : "Bearer" },
8891 )
8992 invalid_token_exception = HTTPException (
9093 status_code = status .HTTP_401_UNAUTHORIZED ,
91- detail = "Access token is invalid. Please log in again." , # no details
94+ detail = "Access token is invalid. Please log in again." ,
9295 headers = {"WWW-Authenticate" : "Bearer" },
9396 )
9497 invalidated_token_exception = HTTPException (
@@ -134,17 +137,21 @@ async def get_current_user(
134137 elif subject .startswith ("client:" ):
135138 subject_prefix = "client:"
136139 else :
137- raise invalid_subject_exception # invalid prefix
140+ logging .warning ("The subject contains an invalid prefix." )
141+ raise invalid_subject_exception
138142 username = subject .removeprefix (subject_prefix )
139143 if username == "" :
140- raise invalid_subject_exception # nothing follows prefix
144+ logging .warning ("The subject contains nothing after the prefix." )
145+ raise invalid_subject_exception
141146 else :
142- raise invalid_subject_exception # not a string
147+ logging .warning ("The subject is not a string." )
148+ raise invalid_subject_exception
143149 token_data = TokenData (subject = username )
144150
145151 # Coerce a "client" into a "user"
146152 # TODO: consolidate the client/user distinction.
147153 if not isinstance (token_data .subject , str ):
154+ logging .warning ("The subject is not a string." )
148155 raise invalid_subject_exception
149156 elif subject_prefix == "user:" :
150157 user = get_user (mdb , username = token_data .subject )
@@ -153,6 +160,7 @@ async def get_current_user(
153160 user = get_client_user (mdb , client_id = token_data .subject )
154161 else :
155162 # Note: We already validate the subject's prefix above, so we expect this case to never occur.
163+ logging .warning ("The subject prefix is not something we recognize." )
156164 user = None
157165
158166 if user is None :
0 commit comments