sghorbal commited on
Commit
28b2d15
·
1 Parent(s): 343f767

add basic auth

Browse files
Files changed (1) hide show
  1. app.py +28 -4
app.py CHANGED
@@ -1,12 +1,36 @@
1
- from fastapi import FastAPI
2
- from fastapi.responses import JSONResponse, RedirectResponse
3
- import gradio as gr
4
- import threading
5
  import time
 
6
  import requests
 
 
 
 
 
 
 
7
 
8
  app = FastAPI()
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # Liste d'URLs à pinger
11
  url_list = ["https://example.com"]
12
 
 
1
+ import os
2
+ from dotenv import load_dotenv
 
 
3
  import time
4
+ import base64
5
  import requests
6
+ import threading
7
+ import gradio as gr
8
+ from fastapi.responses import JSONResponse, RedirectResponse
9
+ from fastapi import FastAPI, Request, Response, status
10
+
11
+ # Charger les variables d'environnement
12
+ load_dotenv()
13
 
14
  app = FastAPI()
15
 
16
+ USERNAME = os.getenv("USERNAME")
17
+ PASSWORD = os.getenv("PASSWORD")
18
+
19
+ @app.middleware("http")
20
+ async def basic_auth(request: Request, call_next):
21
+ auth = request.headers.get("Authorization")
22
+ expected = "Basic " + base64.b64encode(f"{USERNAME}:{PASSWORD}".encode()).decode()
23
+
24
+ if auth != expected:
25
+ return Response(
26
+ status_code=status.HTTP_401_UNAUTHORIZED,
27
+ headers={"WWW-Authenticate": "Basic"},
28
+ content="Unauthorized",
29
+ )
30
+
31
+ return await call_next(request)
32
+
33
+
34
  # Liste d'URLs à pinger
35
  url_list = ["https://example.com"]
36