|
45 | 45 | )
|
46 | 46 | from synapse.http.site import SynapseRequest
|
47 | 47 | from synapse.logging.context import make_deferred_yieldable
|
| 48 | +from synapse.logging.opentracing import active_span, force_tracing, start_active_span |
48 | 49 | from synapse.types import Requester, UserID, create_requester
|
49 | 50 | from synapse.util import json_decoder
|
50 | 51 | from synapse.util.caches.cached_call import RetryOnExceptionCachedCall
|
@@ -177,6 +178,7 @@ def __init__(self, hs: "HomeServer"):
|
177 | 178 | self._http_client = hs.get_proxied_http_client()
|
178 | 179 | self._hostname = hs.hostname
|
179 | 180 | self._admin_token: Callable[[], Optional[str]] = self._config.admin_token
|
| 181 | + self._force_tracing_for_users = hs.config.tracing.force_tracing_for_users |
180 | 182 |
|
181 | 183 | # # Token Introspection Cache
|
182 | 184 | # This remembers what users/devices are represented by which access tokens,
|
@@ -363,6 +365,55 @@ async def get_user_by_req(
|
363 | 365 | allow_guest: bool = False,
|
364 | 366 | allow_expired: bool = False,
|
365 | 367 | allow_locked: bool = False,
|
| 368 | + ) -> Requester: |
| 369 | + """Get a registered user's ID. |
| 370 | +
|
| 371 | + Args: |
| 372 | + request: An HTTP request with an access_token query parameter. |
| 373 | + allow_guest: If False, will raise an AuthError if the user making the |
| 374 | + request is a guest. |
| 375 | + allow_expired: If True, allow the request through even if the account |
| 376 | + is expired, or session token lifetime has ended. Note that |
| 377 | + /login will deliver access tokens regardless of expiration. |
| 378 | +
|
| 379 | + Returns: |
| 380 | + Resolves to the requester |
| 381 | + Raises: |
| 382 | + InvalidClientCredentialsError if no user by that token exists or the token |
| 383 | + is invalid. |
| 384 | + AuthError if access is denied for the user in the access token |
| 385 | + """ |
| 386 | + parent_span = active_span() |
| 387 | + with start_active_span("get_user_by_req"): |
| 388 | + requester = await self._wrapped_get_user_by_req( |
| 389 | + request, allow_guest, allow_expired, allow_locked |
| 390 | + ) |
| 391 | + |
| 392 | + if parent_span: |
| 393 | + if requester.authenticated_entity in self._force_tracing_for_users: |
| 394 | + # request tracing is enabled for this user, so we need to force it |
| 395 | + # tracing on for the parent span (which will be the servlet span). |
| 396 | + # |
| 397 | + # It's too late for the get_user_by_req span to inherit the setting, |
| 398 | + # so we also force it on for that. |
| 399 | + force_tracing() |
| 400 | + force_tracing(parent_span) |
| 401 | + parent_span.set_tag( |
| 402 | + "authenticated_entity", requester.authenticated_entity |
| 403 | + ) |
| 404 | + parent_span.set_tag("user_id", requester.user.to_string()) |
| 405 | + if requester.device_id is not None: |
| 406 | + parent_span.set_tag("device_id", requester.device_id) |
| 407 | + if requester.app_service is not None: |
| 408 | + parent_span.set_tag("appservice_id", requester.app_service.id) |
| 409 | + return requester |
| 410 | + |
| 411 | + async def _wrapped_get_user_by_req( |
| 412 | + self, |
| 413 | + request: SynapseRequest, |
| 414 | + allow_guest: bool = False, |
| 415 | + allow_expired: bool = False, |
| 416 | + allow_locked: bool = False, |
366 | 417 | ) -> Requester:
|
367 | 418 | access_token = self.get_access_token_from_request(request)
|
368 | 419 |
|
|
0 commit comments