Skip to content

Commit

Permalink
Add HTTP password authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Physton committed Nov 10, 2023
1 parent 945f009 commit 3d00bd1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
7 changes: 6 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Web page port. Can not be empty and can not conflict with other programs.
# 网页端口。不能为空,并且不能与其他程序冲突。
APP_PORT=17860
APP_PORT=17860

# Web page username and password. If the password is empty, you can access it without a password.
# 网页用户名和密码。如果密码为空,则不需要密码即可访问。
APP_USERNAME=admin
APP_PASSWORD=
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM python:3.10.11
ADD . /app
WORKDIR /app
ENV APP_PORT="17860"
ENV APP_USERNAME="admin"
ENV APP_PASSWORD=""
EXPOSE 17860
RUN pip install -r requirements.txt
Expand Down
14 changes: 11 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This project is an independent version of [sd-webui-prompt-all-in-one](https://g
2. Extract the contents of the zip file and double-click on `startup-windows.bat` to start the application.
3. The first time you start it, the dependencies will be installed automatically, which may take some time.
4. Once the startup is successful, open a web browser and visit [http://localhost:17860](http://localhost:17860).
5. If you need to modify the configuration such as ports, usernames, passwords, etc., you can edit the `.\sd-webui-prompt-all-in-one-app\.env` file.

## Running with Docker

Expand All @@ -40,6 +41,8 @@ This project is an independent version of [sd-webui-prompt-all-in-one](https://g
docker run -d \
-p 17860:17860 \
-e APP_PORT=17860 \
-e APP_USERNAME=admin \
-e APP_PASSWORD= \
-v ./dockertest/storage:/app/sd-webui-prompt-all-in-one/storage \
-v ./dockertest/models:/app/sd-webui-prompt-all-in-one/models \
-v ./dockertest/tags:/app/sd-webui-prompt-all-in-one/tags \
Expand All @@ -59,6 +62,8 @@ This project is an independent version of [sd-webui-prompt-all-in-one](https://g
- 17860:17860
environment:
- APP_PORT=17860
- APP_USERNAME=admin
- APP_PASSWORD=
volumes:
- ./dockertest/storage:/app/sd-webui-prompt-all-in-one/storage
- ./dockertest/models:/app/sd-webui-prompt-all-in-one/models
Expand All @@ -73,9 +78,11 @@ This project is an independent version of [sd-webui-prompt-all-in-one](https://g
[http://localhost:17860](http://localhost:17860)

### Environment Variables
| Parameter | Description | Default Value |
|:-----------:| :----: | :----: |
| APP_PORT | Server port | 17860 |
| Parameter | Description | Default Value | Explanation |
|:-----------:| :----: | :----: | :----: |
| APP_PORT | Service port | 17860 | Cannot be empty |
| APP_USERNAME | Username | admin | Can be empty |
| APP_PASSWORD | Password | | Can be empty. If empty, no password is required to access |

### Mounted Directories
| Directory | Description |
Expand Down Expand Up @@ -109,6 +116,7 @@ This project is an independent version of [sd-webui-prompt-all-in-one](https://g
python app.py
```
6. Access [http://localhost:17860](http://localhost:17860)
7. If you need to modify the configuration such as ports, usernames, passwords, etc., you can edit the `.env` file in the root directory.

## Donation

Expand Down
14 changes: 11 additions & 3 deletions README_CN.MD
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
2. 解压压缩包,双击 `startup-windows.bat` 启动。
3. 第一次启动会自动安装依赖,需要等待一段时间。
4. 启动成功后,使用浏览器访问 [http://localhost:17860](http://localhost:17860)
5. 如果需要修改端口、用户名、密码等配置,编辑 `.\sd-webui-prompt-all-in-one-app\.env` 文件即可。

> 如果你是中国大陆用户,可能因为网络原因无法正常安装依赖,可以尝试通过下面方法修改镜像源,然后再次运行 `startup-windows.bat` 启动。
> 1. 使用记事本编辑 `startup-windows.bat` 文件
Expand All @@ -46,6 +47,8 @@
docker run -d \
-p 17860:17860 \
-e APP_PORT=17860 \
-e APP_USERNAME=admin \
-e APP_PASSWORD= \
-v ./dockertest/storage:/app/sd-webui-prompt-all-in-one/storage \
-v ./dockertest/models:/app/sd-webui-prompt-all-in-one/models \
-v ./dockertest/tags:/app/sd-webui-prompt-all-in-one/tags \
Expand All @@ -65,6 +68,8 @@
- 17860:17860
environment:
- APP_PORT=17860
- APP_USERNAME=admin
- APP_PASSWORD=
volumes:
- ./dockertest/storage:/app/sd-webui-prompt-all-in-one/storage
- ./dockertest/models:/app/sd-webui-prompt-all-in-one/models
Expand All @@ -79,9 +84,11 @@
[http://localhost:17860](http://localhost:17860)

### 环境变量
| 参数 | 说明 | 默认值 |
|:-----------:| :----: | :----: |
| APP_PORT | 服务端口 | 17860 |
| 参数 | 说明 | 默认值 | 说明 |
|:-----------:| :----: | :----: | :----: |
| APP_PORT | 服务端口 | 17860 | 不能为空 |
| APP_USERNAME | 用户名 | admin | 可以为空 |
| APP_PASSWORD | 密码 | | 可以为空,如果为空,将不需要密码即可访问 |

### 挂载目录
| 目录 | 说明 |
Expand Down Expand Up @@ -116,6 +123,7 @@
python app.py
```
6. 访问 [http://localhost:17860](http://localhost:17860)
7. 如果需要修改端口、用户名、密码等配置,编辑根目录下的 `.env` 文件即可。

## 捐赠

Expand Down
27 changes: 26 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import uvicorn
import gradio as gr
from gradio import Blocks
from fastapi import FastAPI, Response
from fastapi import FastAPI, Response, HTTPException, Depends
from fastapi.staticfiles import StaticFiles
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from starlette.requests import Request
from typing import Optional, Dict, Any
from scripts.on_app_started import on_app_started
from modules.script_callbacks import app_started_callback
import secrets
import install

if __name__ == "__main__":
Expand All @@ -26,6 +29,28 @@
app_port = 17860
app = FastAPI()

app_username = os.environ.get('APP_USERNAME')
app_password = os.environ.get('APP_PASSWORD')
if app_username and app_password and app_username != '' and app_password != '':
security = HTTPBasic()
@app.middleware("http")
async def authenticate(request: Request, call_next):
try:
credentials: HTTPBasicCredentials = await security(request)
if not (secrets.compare_digest(credentials.username, app_username) and secrets.compare_digest(credentials.password, app_password)):
return Response(
"Unauthorized",
status_code=401,
headers={"WWW-Authenticate": "Basic"},
)
return await call_next(request)
except:
return Response(
"Unauthorized",
status_code=401,
headers={"WWW-Authenticate": "Basic"},
)

@app.get("/sd-webui-prompt-all-in-one-js")
async def sd_webui_prompt_all_in_one_js():
# 扫描 ../javascript/ 目录下的所有 js 文件,合并为一个 js 文件
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
- 17860:17860
environment:
- APP_PORT=17860
- APP_USERNAME=admin
- APP_PASSWORD=
volumes:
- ./dockertest/storage:/app/sd-webui-prompt-all-in-one/storage
- ./dockertest/models:/app/sd-webui-prompt-all-in-one/models
Expand Down
2 changes: 2 additions & 0 deletions dockertest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ docker build -t sd-webui-prompt-all-in-one-app .
docker run --rm -it \
-p 17860:17860 \
-e APP_PORT=17860 \
-e APP_USERNAME=admin \
-e APP_PASSWORD= \
-v ./dockertest/storage:/app/sd-webui-prompt-all-in-one/storage \
-v ./dockertest/models:/app/sd-webui-prompt-all-in-one/models \
-v ./dockertest/tags:/app/sd-webui-prompt-all-in-one/tags \
Expand Down

0 comments on commit 3d00bd1

Please sign in to comment.