-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoken_manager.py
More file actions
660 lines (540 loc) · 23.6 KB
/
token_manager.py
File metadata and controls
660 lines (540 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import json
import os
import requests
import uuid
from typing import Any, Dict, Optional
# AWS SDK import - only used in Lambda environment
try:
import boto3
from botocore.exceptions import ClientError
AWS_AVAILABLE = True
except ImportError:
AWS_AVAILABLE = False
# Standalone utility functions for token validation and refresh
def is_personal_token_valid(token: str) -> bool:
"""
Check if a personal access token is valid.
Args:
token: The personal access token to validate
Returns:
bool: True if valid, False otherwise
"""
try:
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(
"https://webexapis.com/v1/people/me", headers=headers
)
return response.status_code == 200
except Exception:
return False
def refresh_personal_token_oauth(config: Dict) -> str:
"""
Refresh the personal access token using OAuth.
Args:
config: Configuration dictionary containing clientId, clientSecret, and refreshToken
Returns:
str: New personal access token
Raises:
Exception: If refresh fails
"""
url = "https://webexapis.com/v1/access_token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {
"grant_type": "refresh_token",
"client_id": config["clientId"],
"client_secret": config["clientSecret"],
"refresh_token": config["refreshToken"],
}
response = requests.post(url, headers=headers, data=data)
if response.status_code == 401:
raise Exception(
"OAuth refresh token expired. Please re-authorize your integration."
)
response.raise_for_status()
token_data = response.json()
new_access_token = token_data.get("access_token")
if not new_access_token:
raise Exception("No access token in OAuth refresh response")
return new_access_token
class TokenManager:
"""Manages Webex service app authentication.
Simplified approach: Always fetches fresh service app tokens on demand.
Tokens are kept in memory only and never persisted.
Key Features:
- Fetches fresh service app tokens using personal access token
- OAuth support for automatic personal token refresh
- Data source token extension
- AWS Secrets Manager support for Lambda deployments
"""
def __init__(self, config_path: str = "token-config.json", secret_name: Optional[str] = None):
"""
Initialize TokenManager.
Args:
config_path: Path to the token configuration file (used for local execution)
secret_name: AWS Secrets Manager secret name (used for Lambda execution)
"""
self.config_path = config_path
self.secret_name = secret_name
self.use_aws = self._should_use_aws()
# In-memory token cache (per script execution only)
self._service_app_token = None
self._service_app_refresh_token = None
# Initialize AWS Secrets Manager client if needed
if self.use_aws:
if not AWS_AVAILABLE:
raise Exception("boto3 is not installed. Install it with: pip install boto3")
self.secrets_client = boto3.client('secretsmanager', region_name=os.environ.get('AWS_REGION', 'us-east-1'))
self._secret_cache = None # Cache the secret to reduce API calls
def _should_use_aws(self) -> bool:
"""
Determine if we should use AWS Secrets Manager.
Returns:
bool: True if running in AWS Lambda environment, False otherwise
"""
# Check if we're in Lambda environment
if 'AWS_LAMBDA_FUNCTION_NAME' in os.environ:
return True
# Check if secret_name is provided and AWS is available
if self.secret_name and AWS_AVAILABLE:
return True
return False
def _get_secret_from_aws(self) -> Dict:
"""
Retrieve secret from AWS Secrets Manager.
Returns:
Dict: The secret data containing credentials
"""
if self._secret_cache:
return self._secret_cache
try:
response = self.secrets_client.get_secret_value(SecretId=self.secret_name)
secret_data = json.loads(response['SecretString'])
self._secret_cache = secret_data
return secret_data
except ClientError as e:
error_code = e.response['Error']['Code']
if error_code == 'ResourceNotFoundException':
raise Exception(f"Secret '{self.secret_name}' not found in AWS Secrets Manager")
elif error_code == 'AccessDeniedException':
raise Exception(f"Access denied to secret '{self.secret_name}'. Check IAM permissions.")
else:
raise Exception(f"Failed to retrieve secret from AWS: {e}")
def _update_personal_token_in_config(self, new_personal_token: str) -> None:
"""
Update the personal access token in the config file or AWS Secrets Manager.
Args:
new_personal_token: The new personal access token
"""
# Use AWS Secrets Manager if in Lambda environment
if self.use_aws:
try:
secret_data = self._get_secret_from_aws()
if 'tokenManager' not in secret_data:
secret_data['tokenManager'] = {}
secret_data['tokenManager']['personalAccessToken'] = new_personal_token
# Update the secret
self.secrets_client.update_secret(
SecretId=self.secret_name,
SecretString=json.dumps(secret_data)
)
# Update cache
self._secret_cache = secret_data
return
except Exception as e:
raise Exception(f"Failed to update personal token in AWS Secrets Manager: {e}")
# Otherwise use local config file
try:
import tempfile
with open(self.config_path, "r") as f:
config = json.load(f)
config["tokenManager"]["personalAccessToken"] = new_personal_token
# Write back to file atomically
temp_file = tempfile.NamedTemporaryFile(
mode='w',
delete=False,
dir=os.path.dirname(self.config_path) or '.',
prefix='.token-config-',
suffix='.tmp'
)
try:
json.dump(config, temp_file, indent=4)
temp_file.close()
os.replace(temp_file.name, self.config_path)
except Exception:
try:
os.unlink(temp_file.name)
except Exception:
pass
raise
except Exception as e:
raise Exception(f"Failed to update personal token in config: {e}")
def _load_config(self) -> Dict[str, Any]:
"""
Load configuration from AWS Secrets Manager or local config file.
Returns:
Dict containing the configuration
Raises:
Exception: If config is not found or invalid
"""
# Use AWS Secrets Manager if in Lambda environment
if self.use_aws:
secret_data = self._get_secret_from_aws()
config = {
'serviceApp': secret_data.get('serviceApp', {}),
'tokenManager': secret_data.get('tokenManager', {})
}
else:
# Load from local file
try:
with open(self.config_path, "r") as f:
config = json.load(f)
except FileNotFoundError:
raise Exception(f"Token config file not found: {self.config_path}")
except json.JSONDecodeError:
raise Exception("Invalid JSON in token config file")
try:
# Validate structure
if "serviceApp" not in config:
raise Exception("Missing 'serviceApp' section in config")
if "tokenManager" not in config:
raise Exception("Missing 'tokenManager' section in config")
# Validate service app fields
service_app_fields = ["appId", "clientId", "clientSecret", "targetOrgId"]
missing_service_fields = [
field
for field in service_app_fields
if field not in config["serviceApp"]
]
# Validate token manager fields
token_manager_fields = ["personalAccessToken"]
missing_token_fields = [
field
for field in token_manager_fields
if field not in config["tokenManager"]
]
# OAuth fields are optional but must all be present together
oauth_fields = ["clientId", "clientSecret", "refreshToken"]
oauth_present = [field in config["tokenManager"] for field in oauth_fields]
if any(oauth_present) and not all(oauth_present):
missing_oauth = [field for field in oauth_fields if field not in config["tokenManager"]]
print(
f"Warning: OAuth partially configured. Missing: {missing_oauth}"
)
print("OAuth token refresh will not be available.")
all_missing = []
if missing_service_fields:
all_missing.extend(
[f"serviceApp.{field}" for field in missing_service_fields]
)
if missing_token_fields:
all_missing.extend(
[f"tokenManager.{field}" for field in missing_token_fields]
)
if all_missing:
raise Exception(f"Missing required fields in config: {all_missing}")
return config
except Exception:
raise
def is_personal_token_valid(self, token: str) -> bool:
"""
Check if a personal access token is valid.
Args:
token: The personal access token to validate
Returns:
bool: True if valid, False otherwise
"""
try:
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(
"https://webexapis.com/v1/people/me", headers=headers
)
return response.status_code == 200
except Exception:
return False
def refresh_personal_token_oauth(self, config: Dict) -> str:
"""
Refresh the personal access token using OAuth.
Args:
config: Configuration containing OAuth fields
Returns:
str: New personal access token
"""
url = "https://webexapis.com/v1/access_token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {
"grant_type": "refresh_token",
"client_id": config["clientId"],
"client_secret": config["clientSecret"],
"refresh_token": config["refreshToken"],
}
response = requests.post(url, headers=headers, data=data)
if response.status_code == 401:
raise Exception(
"OAuth refresh token expired. Please re-authorize your integration."
)
response.raise_for_status()
token_data = response.json()
new_access_token = token_data.get("access_token")
if not new_access_token:
raise Exception("No access token in OAuth refresh response")
return new_access_token
def is_token_valid(self) -> bool:
"""
Check if the current personal access token (from config) is valid.
Returns:
bool: True if valid, False otherwise
"""
try:
config = self._load_config()
token = config["tokenManager"]["personalAccessToken"]
return self.is_personal_token_valid(token)
except Exception:
return False
def refresh_token(self) -> str:
"""
Refresh the personal access token via OAuth and return the new token.
Returns:
str: The new personal access token
Raises:
Exception: If OAuth is not configured or refresh fails
"""
config = self._load_config()
return self._try_refresh_personal_token(config)
def _get_current_refresh_token(self) -> Optional[str]:
"""
Get the current OAuth refresh token if configured.
Returns:
Optional[str]: The refresh token string if OAuth is configured,
None otherwise
"""
try:
config = self._load_config()
token_manager = config["tokenManager"]
return token_manager.get("refreshToken")
except Exception:
return None
def get_token_refresh_guidance(self) -> str:
"""
Get guidance for resolving token refresh issues.
Returns:
str: Human-readable guidance for fixing token problems
"""
return (
"Token Refresh Guidance:\n"
"1. If OAuth is not configured: Run setup_oauth.py to configure "
"automatic token refresh.\n"
"2. If OAuth refresh token expired: Re-run setup_oauth.py to "
"re-authorize your integration.\n"
"3. Manual update: Edit token-config.json and set "
"tokenManager.personalAccessToken to a valid token from "
"https://developer.webex.com."
)
def _try_refresh_personal_token(self, config: Dict) -> str:
"""
Attempt to refresh the personal access token via OAuth.
Args:
config: The loaded configuration
Returns:
str: A refreshed personal access token
Raises:
Exception: If refresh is not configured or fails
"""
token_manager = config["tokenManager"]
# Check if OAuth is configured (all three fields must be present)
oauth_configured = all(
key in token_manager
for key in ["clientId", "clientSecret", "refreshToken"]
)
if not oauth_configured:
raise Exception(
"OAuth refresh is not configured. Cannot refresh token automatically. "
"Please run setup_oauth.py to configure automatic token refresh, "
"or manually update personalAccessToken in token-config.json"
)
print("Personal access token expired, refreshing via OAuth...")
personal_token = self.refresh_personal_token_oauth(token_manager)
# Update the config file with new personal token
self._update_personal_token_in_config(personal_token)
print("Personal access token refreshed successfully")
return personal_token
def get_service_app_token(self) -> str:
"""
Get a fresh service app access token.
This method fetches a token from the Webex Token Manager API.
If the personal access token is expired, it will attempt to refresh it
automatically using OAuth (if configured) and retry.
The token is cached in memory for the duration of the script execution.
Returns:
str: Service app access token
Raises:
Exception: If token request fails
"""
# Return cached token if we already have one for this execution
if self._service_app_token:
return self._service_app_token
config = self._load_config()
# Try to get token with current personal token
try:
return self._fetch_service_app_token(config)
except requests.exceptions.HTTPError as e:
# If we get a 401, the personal token might be expired
if e.response.status_code == 401:
print("Token Manager authentication failed (401), attempting to refresh personal token...")
try:
# Try to refresh the personal token
new_personal_token = self._try_refresh_personal_token(config)
# Update config in memory with refreshed token
config["tokenManager"]["personalAccessToken"] = new_personal_token
# Retry fetching service app token with refreshed personal token
print("Retrying with refreshed token...")
return self._fetch_service_app_token(config)
except Exception as refresh_error:
raise Exception(
f"Failed to refresh personal token: {refresh_error}. "
"Please run setup_oauth.py to re-authorize."
)
else:
# Some other HTTP error
raise Exception(f"Token request failed with status {e.response.status_code}: {e.response.text}")
except Exception as e:
raise Exception(f"Failed to get service app token: {e}")
def _fetch_service_app_token(self, config: Dict) -> str:
"""
Internal method to fetch service app token from the API.
Args:
config: The loaded configuration
Returns:
str: Service app access token
Raises:
requests.exceptions.HTTPError: If the API request fails
"""
personal_token = config["tokenManager"]["personalAccessToken"]
service_app = config["serviceApp"]
url = f"https://webexapis.com/v1/applications/{service_app['appId']}/token"
headers = {
"Authorization": f"Bearer {personal_token}",
"Content-Type": "application/json",
}
payload = {
"clientId": service_app["clientId"],
"clientSecret": service_app["clientSecret"],
"targetOrgId": service_app["targetOrgId"],
}
# Make the API call
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status() # Raises HTTPError for bad status codes
token_data = response.json()
access_token = token_data.get("access_token")
refresh_token = token_data.get("refresh_token")
if not access_token:
raise Exception("No access token in API response")
# Cache tokens in memory for this execution
self._service_app_token = access_token
self._service_app_refresh_token = refresh_token
return access_token
def extend_data_source_token(
self, data_source_id: str, token_lifetime_minutes: int = 1440
) -> Dict[str, Any]:
"""
Extend a data source token by updating only the nonce.
Args:
data_source_id: The ID of the data source to update
token_lifetime_minutes: Token lifetime in minutes (default: 1440 = 24 hours, max: 1440)
Returns:
Dict containing the result of the operation
"""
try:
# Validate token lifetime
if token_lifetime_minutes > 1440:
return {
"success": False,
"error": f"Token lifetime cannot exceed 1440 minutes (24 hours). Requested: {token_lifetime_minutes} minutes",
}
if token_lifetime_minutes <= 0:
return {
"success": False,
"error": f"Token lifetime must be positive. Requested: {token_lifetime_minutes} minutes",
}
# Get fresh service app token
access_token = self.get_service_app_token()
# First, get the current data source configuration
headers = {"Authorization": f"Bearer {access_token}"}
get_url = f"https://webexapis.com/v1/dataSources/{data_source_id}"
response = requests.get(get_url, headers=headers)
if response.status_code != 200:
return {
"success": False,
"error": f"Failed to retrieve data source: {response.text}",
"status_code": response.status_code,
}
current_config = response.json()
# Parse the JWT token to extract audience, subject, and schema info
jws_token = current_config.get("jwsToken", "")
audience = None
subject = None
schema_uuid = None
if jws_token:
try:
# Decode JWT without verification to extract claims
import jwt
decoded = jwt.decode(jws_token, options={"verify_signature": False})
audience = decoded.get("aud")
subject = decoded.get("sub")
schema_uuid = decoded.get("com.cisco.datasource.schema.uuid")
except Exception as e:
print(f"Warning: Could not decode JWT token: {e}")
# Fallback to current config values if JWT parsing failed
if not audience:
audience = current_config.get("audience", "")
if not subject:
subject = current_config.get("subject", "subject")
if not schema_uuid:
schema_uuid = current_config.get("schemaId", "")
# Generate a new nonce (this is what triggers the token refresh)
new_nonce = str(uuid.uuid4())
# Create update configuration with all required fields
update_config = {
"audience": audience,
"nonce": new_nonce,
"schemaId": schema_uuid,
"subject": subject,
"url": current_config.get("url"),
"tokenLifetimeMinutes": token_lifetime_minutes,
"status": current_config.get("status", "active"),
}
# Validate that we have all required fields
required_fields = ["audience", "schemaId", "url"]
missing_fields = [
field for field in required_fields if not update_config.get(field)
]
if missing_fields:
return {
"success": False,
"error": f"Missing required fields: {missing_fields}. Could not extract from current data source.",
}
# Update the data source
update_url = f"https://webexapis.com/v1/dataSources/{data_source_id}"
headers["Content-Type"] = "application/json"
update_response = requests.put(
update_url, headers=headers, json=update_config
)
if update_response.status_code == 200:
result_data = update_response.json()
return {
"success": True,
"data": result_data,
"nonce_updated": new_nonce,
"token_lifetime_minutes": token_lifetime_minutes,
"token_expiry": result_data.get("tokenExpiryTime"),
"message": f"Data source token extended successfully. New expiry: {result_data.get('tokenExpiryTime')}",
}
else:
return {
"success": False,
"error": f"Failed to update data source: {update_response.text}",
"status_code": update_response.status_code,
}
except requests.exceptions.RequestException as e:
return {"success": False, "error": f"Request failed: {str(e)}"}
except Exception as e:
return {"success": False, "error": f"Unexpected error: {str(e)}"}