Skip to content

Commit 998b0e3

Browse files
Added Key Vault-based authorization
1 parent cef63c8 commit 998b0e3

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

app.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1-
from flask import Flask
1+
from flask import Flask, request, abort
2+
from azure.identity import DefaultAzureCredential
3+
from azure.keyvault.secrets import SecretClient
4+
import os
25

36
app = Flask(__name__)
47

8+
# Key Vault URL (replace with yours)
9+
KEY_VAULT_URL = "https://yashkeyvaultverysafe.vault.azure.net/"
10+
11+
# Set up Azure Key Vault client
12+
credential = DefaultAzureCredential()
13+
client = SecretClient(vault_url=KEY_VAULT_URL, credential=credential)
14+
15+
# Get secret from Key Vault
16+
retrieved_secret = client.get_secret("app-auth-secret").value
17+
518
@app.route("/")
619
def home():
7-
return "Yo! i made a change"
20+
# Check if 'x-api-key' header matches the secret
21+
api_key = request.headers.get('x-api-key')
22+
if api_key != retrieved_secret:
23+
abort(403) # Forbidden
24+
return "✅ Authorized! You accessed a secure route."
825

926
if __name__ == "__main__":
1027
app.run(host='0.0.0.0', port=8000)

0 commit comments

Comments
 (0)