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