1
1
import json
2
2
import threading
3
3
import time
4
- from typing import Dict , Optional , Any , Callable
4
+ from typing import Any
5
5
from unitsnet_py import Duration
6
6
from chkp_harmony_endpoint_management_sdk .classes import harmony_endpoint_saas_options
7
7
from chkp_harmony_endpoint_management_sdk .classes .harmony_endpoint_saas_options import HarmonyEndpointSaaSOptions
18
18
import uuid
19
19
from enum import Enum
20
20
from urllib .parse import urlparse
21
+ import jwt
21
22
import requests
22
23
23
24
class WorkMode (Enum ):
@@ -32,6 +33,8 @@ class WorkMode(Enum):
32
33
33
34
SOURCE_HEADER = 'harmony-endpoint-py-sdk'
34
35
36
+ VERIFY_CONTENT = False
37
+
35
38
class SessionManager :
36
39
def __init__ (self ):
37
40
self .__session_operations : SessionOperations = None
@@ -47,7 +50,7 @@ def __init__(self):
47
50
The CI token expiration
48
51
"""
49
52
50
- self .__on_premise_portal_auth = OnPremisePortalAuth = None
53
+ self .__on_premise_portal_auth : OnPremisePortalAuth = None
51
54
"""
52
55
The CI token expiration
53
56
"""
@@ -189,9 +192,10 @@ def __perform_ci_login(self):
189
192
if not response_json ['success' ]:
190
193
error_logger (f'Failed to login to CI GW for session "{ self .__session_id } " url "{ auth_url } ", error payload: { response_json } ' )
191
194
raise response_json
192
- logger (f'Preforming CI login to session id "{ self .__session_id } " succeeded' );
195
+ logger (f'Preforming CI login to session id "{ self .__session_id } " succeeded' )
193
196
194
197
self .__infinity_portal_token = response_json ['data' ]['token' ]
198
+ self .__assert_token_is_for_correct_application (self .__infinity_portal_token )
195
199
self .__next_ci_expiration = Duration .from_seconds (time .time ()) + Duration .from_seconds (response_json ['data' ]['expiresIn' ])
196
200
except Exception as e :
197
201
error_logger (f'Failed to login to CI GW for session "{ self .__session_id } " url "{ auth_url } ", error: { e } ' )
@@ -367,9 +371,42 @@ def __validate_premise_params(self, on_premise_portal_auth: OnPremisePortalAuth)
367
371
message = message ,
368
372
error_scope = HarmonyErrorScope .INVALID_PARAMS ,
369
373
)
370
-
374
+
375
+ def __assert_token_is_for_correct_application (self , bearer_token : str ) -> None :
376
+ if not bearer_token :
377
+ error_logger ('No bearer token was given. Ignoring. Requests may fail' )
378
+ return
379
+
380
+ try :
381
+ # Token verification is NOT required here - this is just to determine whether
382
+ # the Infinity Portal bearer is for the 'Endpoint' application
383
+ decoded_token = jwt .decode (bearer_token , verify = VERIFY_CONTENT )
384
+ if not decoded_token :
385
+ error_logger ('Bearer decoding yielded nothing. Ignoring. Requests may fail' )
386
+ return
387
+
388
+ app_id = decoded_token ['appId' ]
389
+ if not app_id :
390
+ error_logger ('An Application ID claim was not present in the bearer token. Ignoring. Requests may fail' )
391
+ return
392
+
393
+ endpoint_app_id = '12345678-8888-1234-1234-123456789123'
394
+ if app_id != endpoint_app_id :
395
+ error_logger (f"Target application is incorrect - expected '{ endpoint_app_id } ' but got '{ app_id } '. Raising an error" )
396
+ raise HarmonyApiException (
397
+ message = (
398
+ "The provided API key must be for the 'Endpoint' service. Please refer to the documentation at "
399
+ 'https://app.swaggerhub.com/apis/Check-Point/web-mgmt-external-api-production for more details'
400
+ ),
401
+ error_scope = HarmonyErrorScope .INVALID_PARAMS ,
402
+ )
403
+
404
+ except jwt .InvalidTokenError :
405
+ error_logger ('The given token could not be decoded. Ignoring. Requests may fail' )
406
+ return
407
+
371
408
def connect_cloud (self , infinity_portal_auth : InfinityPortalAuth , session_operations : SessionOperations ):
372
- self .__work_mode = WorkMode .CLOUD
409
+ self .__work_mode = WorkMode .CLOUD
373
410
self .__sdk_connection_state = SDKConnectionState .CONNECTING
374
411
self .__validate_cloud_params (infinity_portal_auth )
375
412
self .__session_operations = session_operations
@@ -383,9 +420,8 @@ def connect_cloud(self, infinity_portal_auth: InfinityPortalAuth, session_operat
383
420
384
421
self .__activate_keep_alive ()
385
422
386
-
387
423
def connect_saas (self , infinity_portal_auth : InfinityPortalAuth , harmony_endpoint_saas_options : HarmonyEndpointSaaSOptions , session_operations : SessionOperations ):
388
- self .__work_mode = WorkMode .SAAS
424
+ self .__work_mode = WorkMode .SAAS
389
425
self .__sdk_connection_state = SDKConnectionState .CONNECTING
390
426
self .__validate_cloud_params (infinity_portal_auth )
391
427
self .__harmony_endpoint_saas_options = harmony_endpoint_saas_options
0 commit comments