Skip to content

Commit

Permalink
feat(auth): add token to API GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Mar 25, 2024
1 parent 19e4816 commit 5e963f8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
7 changes: 6 additions & 1 deletion gateway/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import requests
from fastapi import Security, HTTPException
from fastapi.security import OAuth2AuthorizationCodeBearer, OAuth2PasswordBearer
from fastapi.security import OAuth2AuthorizationCodeBearer, OAuth2PasswordBearer, HTTPBearer
from jose import jwt, JOSEError
from starlette import status
from starlette.datastructures import MutableHeaders
Expand Down Expand Up @@ -31,6 +31,11 @@

idp_oauth2_scheme_pass = OAuth2PasswordBearer(tokenUrl=realm_idp_settings.token_url)

httpbearer = HTTPBearer(
scheme_name="JWT",
description="Pass a valid JWT here for authentication. Can be obtained from /token endpoint."
)


# Debugging methods
async def get_idp_public_key() -> str:
Expand Down
5 changes: 3 additions & 2 deletions gateway/routers/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
from starlette.requests import Request
from starlette.responses import Response

from gateway.auth import add_hub_jwt, verify_idp_token, idp_oauth2_scheme_pass
from gateway.auth import add_hub_jwt, verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from gateway.conf import gateway_settings
from gateway.core import route
from gateway.models.hub import Project, AllProjects, ApprovalStatus, AnalysisOrProjectNode, ListAnalysisOrProjectNodes, \
AnalysisNode
from gateway.models.k8s import ImageDataResponse, ContainerResponse

hub_router = APIRouter(
dependencies=[Security(verify_idp_token), Depends(add_hub_jwt), Security(idp_oauth2_scheme_pass)],
dependencies=[Security(verify_idp_token), Depends(add_hub_jwt), Security(idp_oauth2_scheme_pass),
Security(httpbearer)],
tags=["Hub"],
responses={404: {"description": "Not found"}},
)
Expand Down
4 changes: 2 additions & 2 deletions gateway/routers/kong.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from kong_admin_client.rest import ApiException
from starlette import status

from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass
from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from gateway.conf import gateway_settings
from gateway.models.kong import ServiceRequest, HttpMethodCode, ProtocolCode, LinkDataStoreProject, \
Disconnect, LinkProjectAnalysis

kong_router = APIRouter(
dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass)],
dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass), Security(httpbearer)],
tags=["Kong"],
responses={404: {"description": "Not found"}},
)
Expand Down
4 changes: 2 additions & 2 deletions gateway/routers/podorc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from starlette.requests import Request
from starlette.responses import Response

from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass
from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from gateway.conf import gateway_settings
from gateway.core import route

po_router = APIRouter(
dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass)],
dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass), Security(httpbearer)],
tags=["PodOrc"],
responses={404: {"description": "Not found"}},
)
Expand Down
4 changes: 2 additions & 2 deletions gateway/routers/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from starlette.requests import Request
from starlette.responses import Response

from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass
from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass, httpbearer
from gateway.conf import gateway_settings
from gateway.core import route
from gateway.models.results import ResultsUploadResponse

results_router = APIRouter(
dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass)],
dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass), Security(httpbearer)],
tags=["Results"],
responses={404: {"description": "Not found"}},
)
Expand Down
19 changes: 13 additions & 6 deletions gateway/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import requests
import uvicorn
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import OAuth2PasswordRequestForm
from fastapi import FastAPI, HTTPException, Query
from starlette import status
from starlette.middleware.cors import CORSMiddleware

Expand Down Expand Up @@ -55,7 +54,12 @@ async def lifespan(app: FastAPI):
# Auth fill client ID for the docs with the below value
"clientId": realm_idp_settings.client_id, # default client-id is Keycloak
},
lifespan=lifespan
lifespan=lifespan,
license_info={
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
"identifier": "Apache-2.0",
},
)

app.add_middleware(
Expand Down Expand Up @@ -96,11 +100,14 @@ def get_health() -> HealthCheck:
status_code=status.HTTP_200_OK,
response_model=Token,
)
def get_token(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]) -> Token:
def get_token(
username: Annotated[str, Query()],
password: Annotated[str, Query()],
) -> Token:
"""Get a token from the IDP."""
payload = {
"username": form_data.username,
"password": form_data.password,
"username": username,
"password": password,
"client_id": realm_idp_settings.client_id,
"client_secret": realm_idp_settings.client_secret,
"grant_type": "password",
Expand Down

0 comments on commit 5e963f8

Please sign in to comment.