Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
b5bd07a0
Commit
b5bd07a0
authored
Feb 23, 2024
by
Timothy J. Baek
Browse files
feat: secure litellm api
parent
af388dfe
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
2 deletions
+30
-2
backend/main.py
backend/main.py
+19
-2
backend/utils/utils.py
backend/utils/utils.py
+11
-0
No files found.
backend/main.py
View file @
b5bd07a0
...
@@ -4,9 +4,10 @@ import markdown
...
@@ -4,9 +4,10 @@ import markdown
import
time
import
time
from
fastapi
import
FastAPI
,
Request
from
fastapi
import
FastAPI
,
Request
,
Depends
from
fastapi.staticfiles
import
StaticFiles
from
fastapi.staticfiles
import
StaticFiles
from
fastapi
import
HTTPException
from
fastapi
import
HTTPException
from
fastapi.responses
import
JSONResponse
from
fastapi.middleware.wsgi
import
WSGIMiddleware
from
fastapi.middleware.wsgi
import
WSGIMiddleware
from
fastapi.middleware.cors
import
CORSMiddleware
from
fastapi.middleware.cors
import
CORSMiddleware
from
starlette.exceptions
import
HTTPException
as
StarletteHTTPException
from
starlette.exceptions
import
HTTPException
as
StarletteHTTPException
...
@@ -19,10 +20,11 @@ from apps.openai.main import app as openai_app
...
@@ -19,10 +20,11 @@ from apps.openai.main import app as openai_app
from
apps.audio.main
import
app
as
audio_app
from
apps.audio.main
import
app
as
audio_app
from
apps.images.main
import
app
as
images_app
from
apps.images.main
import
app
as
images_app
from
apps.rag.main
import
app
as
rag_app
from
apps.rag.main
import
app
as
rag_app
from
apps.web.main
import
app
as
webui_app
from
apps.web.main
import
app
as
webui_app
from
config
import
WEBUI_NAME
,
ENV
,
VERSION
,
CHANGELOG
,
FRONTEND_BUILD_DIR
from
config
import
WEBUI_NAME
,
ENV
,
VERSION
,
CHANGELOG
,
FRONTEND_BUILD_DIR
from
utils.utils
import
get_http_authorization_cred
,
get_current_user
class
SPAStaticFiles
(
StaticFiles
):
class
SPAStaticFiles
(
StaticFiles
):
...
@@ -59,6 +61,21 @@ async def check_url(request: Request, call_next):
...
@@ -59,6 +61,21 @@ async def check_url(request: Request, call_next):
return
response
return
response
@
litellm_app
.
middleware
(
"http"
)
async
def
auth_middleware
(
request
:
Request
,
call_next
):
auth_header
=
request
.
headers
.
get
(
"Authorization"
,
""
)
if
ENV
!=
"dev"
:
try
:
user
=
get_current_user
(
get_http_authorization_cred
(
auth_header
))
print
(
user
)
except
Exception
as
e
:
return
JSONResponse
(
status_code
=
400
,
content
=
{
"detail"
:
str
(
e
)})
response
=
await
call_next
(
request
)
return
response
app
.
mount
(
"/api/v1"
,
webui_app
)
app
.
mount
(
"/api/v1"
,
webui_app
)
app
.
mount
(
"/litellm/api"
,
litellm_app
)
app
.
mount
(
"/litellm/api"
,
litellm_app
)
...
...
backend/utils/utils.py
View file @
b5bd07a0
...
@@ -58,6 +58,17 @@ def extract_token_from_auth_header(auth_header: str):
...
@@ -58,6 +58,17 @@ def extract_token_from_auth_header(auth_header: str):
return
auth_header
[
len
(
"Bearer "
)
:]
return
auth_header
[
len
(
"Bearer "
)
:]
def
get_http_authorization_cred
(
auth_header
:
str
):
try
:
scheme
,
credentials
=
auth_header
.
split
(
" "
)
return
{
"scheme"
:
scheme
,
"credentials"
:
credentials
,
}
except
:
raise
ValueError
(
ERROR_MESSAGES
.
INVALID_TOKEN
)
def
get_current_user
(
def
get_current_user
(
auth_token
:
HTTPAuthorizationCredentials
=
Depends
(
bearer_security
),
auth_token
:
HTTPAuthorizationCredentials
=
Depends
(
bearer_security
),
):
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment