Skip to content

Commit 982c671

Browse files
committed
CM-38320 - Fix invalid access token exception
1 parent be79388 commit 982c671

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

cycode/cli/commands/main_cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from cycode.cli.consts import (
1313
CLI_CONTEXT_SETTINGS,
1414
)
15+
from cycode.cli.sentry import add_breadcrumb, init_sentry
1516
from cycode.cli.user_settings.configuration_manager import ConfigurationManager
1617
from cycode.cli.utils.progress_bar import SCAN_PROGRESS_BAR_SECTIONS, get_progress_bar
1718
from cycode.cyclient.config import set_logging_level
@@ -60,6 +61,9 @@
6061
def main_cli(
6162
context: click.Context, verbose: bool, no_progress_meter: bool, output: str, user_agent: Optional[str]
6263
) -> None:
64+
init_sentry()
65+
add_breadcrumb('cycode')
66+
6367
context.ensure_object(dict)
6468
configuration_manager = ConfigurationManager()
6569

cycode/cli/main.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
import pip_system_certs.wrapt_requests # noqa: F401
66

77
from cycode.cli.commands.main_cli import main_cli
8-
from cycode.cli.sentry import add_breadcrumb, init_sentry
98

109
if __name__ == '__main__':
1110
# DO NOT REMOVE OR MOVE THIS LINE
1211
# this is required to support multiprocessing in executables files packaged with PyInstaller
1312
# see https://pyinstaller.org/en/latest/common-issues-and-pitfalls.html#multi-processing
1413
freeze_support()
1514

16-
init_sentry()
17-
add_breadcrumb('cycode')
18-
1915
main_cli()

cycode/cli/sentry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def init_sentry() -> None:
5151
dsn=consts.SENTRY_DSN,
5252
debug=consts.SENTRY_DEBUG,
5353
release=_get_sentry_release(),
54+
server_name='',
5455
before_send=_before_sentry_event_send,
5556
sample_rate=consts.SENTRY_SAMPLE_RATE,
5657
send_default_pii=consts.SENTRY_SEND_DEFAULT_PII,
@@ -61,7 +62,6 @@ def init_sentry() -> None:
6162
AtexitIntegration(lambda _, __: None) # disable output to stderr about pending events
6263
],
6364
)
64-
sentry_sdk.set_user(None)
6565

6666

6767
def setup_scope_from_access_token(access_token: Optional[str]) -> None:

cycode/cli/utils/jwt_utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
from typing import Tuple
1+
from typing import Optional, Tuple
22

33
import jwt
44

5+
_JWT_PAYLOAD_POSSIBLE_USER_ID_FIELD_NAMES = ('userId', 'internalId', 'token-user-id')
56

6-
def get_user_and_tenant_ids_from_access_token(access_token: str) -> Tuple[str, str]:
7+
8+
def get_user_and_tenant_ids_from_access_token(access_token: str) -> Tuple[Optional[str], Optional[str]]:
79
payload = jwt.decode(access_token, options={'verify_signature': False})
8-
user_id = payload.get('userId')
9-
tenant_id = payload.get('tenantId')
1010

11-
if not user_id or not tenant_id:
12-
raise ValueError('Invalid access token')
11+
user_id = None
12+
for field in _JWT_PAYLOAD_POSSIBLE_USER_ID_FIELD_NAMES:
13+
user_id = payload.get(field)
14+
if user_id:
15+
break
16+
17+
tenant_id = payload.get('tenantId')
1318

1419
return user_id, tenant_id

0 commit comments

Comments
 (0)