|
1 | | -import logging |
2 | 1 | import json |
3 | | -import pandas as pd |
4 | | -import azure.functions as func |
| 2 | +import logging |
| 3 | +import os |
5 | 4 | from io import StringIO |
| 5 | + |
| 6 | +import azure.functions as func |
| 7 | +import jwt |
| 8 | +import pandas as pd |
6 | 9 | from sklearn.preprocessing import LabelEncoder |
7 | 10 |
|
8 | 11 | # Decree and declare our project as an Azure Function App subsidiary |
@@ -60,10 +63,25 @@ def blob_trigger(inbound: func.InputStream, outbound: func.Out[str]): |
60 | 63 | return f"Error: {str(e)}" |
61 | 64 |
|
62 | 65 |
|
| 66 | +def validate_jwt(token: str, audience: str) -> bool: |
| 67 | + try: |
| 68 | + decoded = jwt.decode(token, audience=audience, options={"verify_signature": False}) |
| 69 | + # Optionally check claims like roles or scopes |
| 70 | + return True |
| 71 | + except Exception as e: |
| 72 | + logging.error(f"JWT validation failed: {e}") |
| 73 | + return False |
| 74 | + |
| 75 | + |
63 | 76 | @app.route(route="upload_csv", auth_level=func.AuthLevel.ANONYMOUS) |
64 | | -@app.blob_output(arg_name="outbound", path="hvalfangstcontainer/in/input.csv", connection="") # AzureWebJobsStorage |
| 77 | +@app.blob_output(arg_name="outbound", path="hvalfangstcontainer/in/input.csv", connection="AzureWebJobsStorage") |
65 | 78 | def upload_csv(req: func.HttpRequest, outbound: func.Out[str]) -> str: |
66 | 79 | try: |
| 80 | + |
| 81 | + token = req.headers.get("Authorization").split(" ")[1] # Extract Bearer token |
| 82 | + if not validate_jwt(token, audience=os.environ.get("FUNCTION_APP_CLIENT_ID")): |
| 83 | + return func.HttpResponse("Unauthorized", status_code=401) |
| 84 | + |
67 | 85 | logging.info("Received HTTP request to upload CSV") |
68 | 86 |
|
69 | 87 | # Parse raw bytes derived from request body to string |
|
0 commit comments