Skip to content

Commit

Permalink
Add oauth proxy (amun-ai#579)
Browse files Browse the repository at this point in the history
* Add docs for setting up auth0 authentication

* Add oauth proxy

* Corret json response

* Fix oauth token response

* Add debug information for token proxy

* bump version

* Fix dependency
  • Loading branch information
oeway authored Jan 24, 2024
1 parent 7bd39ab commit 2c51e8d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion hypha/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.15.35"
"version": "0.15.36"
}
30 changes: 28 additions & 2 deletions hypha/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import traceback
from typing import Any

import httpx
import msgpack
from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse, Response
from fastapi.responses import JSONResponse, Response, RedirectResponse

from imjoy_rpc.hypha import RPC
from hypha.core.auth import login_optional
from hypha.core.auth import login_optional, AUTH0_DOMAIN
from hypha.core.store import RedisStore
from hypha.utils import GzipRoute

Expand Down Expand Up @@ -73,6 +74,31 @@ def __init__(self, store: RedisStore) -> None:
router = APIRouter()
router.route_class = GzipRoute
self.store = store

@router.get("/authorize")
async def auth_proxy(request: Request):
# Construct the full URL for the Auth0 authorize endpoint with the query parameters
auth0_authorize_url = f"https://{AUTH0_DOMAIN}/authorize?{request.query_params}"

# Redirect the client to the constructed URL
return RedirectResponse(url=auth0_authorize_url)


@router.post("/oauth/token")
async def token_proxy(request: Request):
form_data = await request.form()
async with httpx.AsyncClient() as client:
auth0_response = await client.post(
f"https://{AUTH0_DOMAIN}/oauth/token",
data=form_data,
headers={"Content-Type": "application/x-www-form-urlencoded"}
)

return JSONResponse(
status_code=200,
content=auth0_response.json()
)


@router.get("/workspaces")
async def get_all_workspaces(
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pydantic[email]==1.10.9
pymultihash==0.8.2
python-dotenv==0.21.1
python-jose==3.3.0
python-multipart==0.0.6
pyyaml==6.0.1
requests==2.31.0
shortuuid==1.0.11
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"lxml",
"python-dotenv>=0.19.0",
"python-jose>=3.3.0",
"python-multipart>=0.0.6",
"pyyaml",
"fakeredis>=2.14.1",
"shortuuid>=1.0.1",
Expand Down
2 changes: 0 additions & 2 deletions tests/test_services.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Test services."""
import pytest
import requests
import asyncio
import httpx

from imjoy_rpc.hypha import login, connect_to_server
Expand Down

0 comments on commit 2c51e8d

Please sign in to comment.