I followed this guide to set Simple HTTP Basic Auth and it works. I dockerized the application and all of a sudden the Auth pop-up which ask for user name and password does not show up anymore. I am running the application identically (i.e the same as I ran it locally but with docker)
I dont know what running it in docker would somehow remove that functionality. Is there something I am missing or not understanding? Meaning what would I have to change for this to be resolved.
I different configurations of Simple HTTP Basic Auth in FastApi. I also tried to ask AI chatbots
# HTTPBasic Logic
import secrets
from typing import Annotated
from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
security = HTTPBasic()
def get_current_username(
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
):
current_username_bytes = credentials.username.encode("utf8")
correct_username_bytes = b"stanleyjobson"
is_correct_username = secrets.compare_digest(
current_username_bytes, correct_username_bytes
)
current_password_bytes = credentials.password.encode("utf8")
correct_password_bytes = b"swordfish"
is_correct_password = secrets.compare_digest(
current_password_bytes, correct_password_bytes
)
if not (is_correct_username and is_correct_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Basic"},
)
return credentials.username
app = FastAPI(dependencies=[Depends(get_current_username)])
# Docker
FROM python:3.11
Create a user and group for the application
RUN addgroup --system app && adduser --system --group app
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV ENVIRONMENT=prod
ENV TESTING=0
RUN apt-get update -y && apt-get install -y
python3-dev
python3-venv
graphviz
libgraphviz-dev
pkg-config
WORKDIR /app/
UN python3 -m venv /app/venv
# Activate virtual environment and install requirements
RUN . /app/venv/bin/activate && pip install -r requirements.txt
# Set environment variable to ensure Python packages execute from the virtualenv
ENV PATH="/app/venv/bin:$PATH"
EXPOSE 8000
uvicorn --reload --host "127.0.0.1" "main:app" --log-config=log_conf.yaml
AI Artist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.