-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
lib/workload/stateless/stacks/client-websocket-conn/deps/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
PyJWT==2.8.0 | ||
requests==2.31.0 |
74 changes: 74 additions & 0 deletions
74
lib/workload/stateless/stacks/client-websocket-conn/lambda/auth.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import os | ||
import logging | ||
import jwt | ||
import requests | ||
from typing import Dict, Any | ||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.INFO) | ||
|
||
# Get environment variables | ||
COGNITO_USER_POOL_ID = os.environ['COGNITO_USER_POOL_ID'] | ||
COGNITO_REGION = os.environ.get('COGNITO_REGION', 'ap-southeast-2') | ||
|
||
def get_public_key(): | ||
"""Get Cognito public key for JWT verification""" | ||
url = f'https://cognito-idp.{COGNITO_REGION}.amazonaws.com/{COGNITO_USER_POOL_ID}/.well-known/jwks.json' | ||
try: | ||
response = requests.get(url) | ||
return response.json()['keys'][0] # Get the first key | ||
except Exception as e: | ||
logger.error(f"Error getting public key: {str(e)}") | ||
raise | ||
|
||
def handler(event: Dict[str, Any], context: Any) -> Dict[str, Any]: | ||
"""Simple Lambda authorizer for WebSocket""" | ||
logger.info("WebSocket authorization request") | ||
|
||
try: | ||
# Get token from headers | ||
token = event.get('headers', {}).get('Authorization', '').replace('Bearer ', '') | ||
if not token: | ||
raise Exception('No token provided') | ||
|
||
# Get public key | ||
public_key = get_public_key() | ||
|
||
# Verify token | ||
decoded = jwt.decode( | ||
token, | ||
public_key, | ||
algorithms=['RS256'], | ||
issuer=f'https://cognito-idp.{COGNITO_REGION}.amazonaws.com/{COGNITO_USER_POOL_ID}' | ||
) | ||
|
||
# Generate allow policy | ||
return { | ||
'principalId': decoded['sub'], | ||
'policyDocument': { | ||
'Version': '2012-10-17', | ||
'Statement': [{ | ||
'Action': 'execute-api:Invoke', | ||
'Effect': 'Allow', | ||
'Resource': event['methodArn'] | ||
}] | ||
}, | ||
'context': { | ||
'userId': decoded['sub'] | ||
} | ||
} | ||
|
||
except Exception as e: | ||
logger.error(f"Authorization failed: {str(e)}") | ||
# Return deny policy | ||
return { | ||
'principalId': 'unauthorized', | ||
'policyDocument': { | ||
'Version': '2012-10-17', | ||
'Statement': [{ | ||
'Action': 'execute-api:Invoke', | ||
'Effect': 'Deny', | ||
'Resource': event['methodArn'] | ||
}] | ||
} | ||
} |