-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from bcgov/feature/connections
Refactor tenant api's into sub-app
- Loading branch information
Showing
5 changed files
with
74 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
from fastapi import APIRouter | ||
|
||
from api.endpoints.routes import innkeeper, tenants, connections, ledger | ||
from api.endpoints.routes import innkeeper, tenants | ||
|
||
api_router = APIRouter() | ||
api_router.include_router( | ||
connections.router, prefix="/connections", tags=["connections"] | ||
) | ||
api_router.include_router(ledger.router, prefix="/ledger", tags=["ledger"]) | ||
api_router.include_router(innkeeper.router, prefix="/innkeeper", tags=["innkeeper"]) | ||
api_router.include_router(tenants.router, prefix="/tenants", tags=["tenants"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from fastapi import APIRouter | ||
|
||
from api.endpoints.routes import connections, ledger | ||
|
||
tenant_router = APIRouter() | ||
tenant_router.include_router( | ||
connections.router, prefix="/connections", tags=["connections"] | ||
) | ||
tenant_router.include_router(ledger.router, prefix="/ledger", tags=["ledger"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from datetime import timedelta | ||
|
||
from fastapi import APIRouter, Depends, FastAPI, HTTPException, status | ||
from fastapi.security import OAuth2PasswordRequestForm | ||
from starlette.middleware import Middleware | ||
from starlette_context import plugins | ||
from starlette_context.middleware import RawContextMiddleware | ||
|
||
from api.endpoints.routes.tenant_api import tenant_router | ||
from api.tenant_security import ( | ||
TenantToken, | ||
authenticate_tenant, | ||
create_access_token, | ||
JWTTFetchingMiddleware, | ||
) | ||
from api.core.config import settings | ||
|
||
|
||
middleware = [ | ||
Middleware( | ||
RawContextMiddleware, | ||
plugins=(plugins.RequestIdPlugin(), plugins.CorrelationIdPlugin()), | ||
), | ||
Middleware(JWTTFetchingMiddleware), | ||
] | ||
|
||
router = APIRouter() | ||
|
||
|
||
def get_tenantapp() -> FastAPI: | ||
application = FastAPI( | ||
title=settings.TENANT_TITLE, | ||
description=settings.TENANT_DESCRIPTION, | ||
debug=settings.DEBUG, | ||
middleware=middleware, | ||
) | ||
application.include_router(tenant_router, prefix=settings.API_V1_STR) | ||
application.include_router(router, prefix="") | ||
return application | ||
|
||
|
||
@router.post("/token", response_model=TenantToken) | ||
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()): | ||
tenant = await authenticate_tenant(form_data.username, form_data.password) | ||
if not tenant: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Incorrect wallet_id or wallet_key", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
access_token_expires = timedelta(minutes=settings.JWT_ACCESS_TOKEN_EXPIRE_MINUTES) | ||
access_token = create_access_token( | ||
data={"sub": tenant["wallet_id"], "key": tenant["wallet_token"]}, | ||
expires_delta=access_token_expires, | ||
) | ||
return {"access_token": access_token, "token_type": "bearer"} |