-
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 #53 from bcgov/feature/innkeeper-subapp
Secure innkeeper. Make innkeeper sub-application
- Loading branch information
Showing
15 changed files
with
122 additions
and
100 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
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
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
22 changes: 22 additions & 0 deletions
22
services/traction/api/endpoints/dependencies/jwt_security.py
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,22 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from jose import jwt | ||
from pydantic import BaseModel | ||
|
||
from api.core.config import settings | ||
|
||
|
||
class AccessToken(BaseModel): | ||
access_token: str | ||
token_type: str | ||
|
||
|
||
def create_access_token(data: dict): | ||
expires_delta = timedelta(minutes=settings.JWT_ACCESS_TOKEN_EXPIRE_MINUTES) | ||
to_encode = data.copy() | ||
expire = datetime.utcnow() + expires_delta | ||
to_encode.update({"exp": expire}) | ||
encoded_jwt = jwt.encode( | ||
to_encode, settings.JWT_SECRET_KEY, algorithm=settings.JWT_ALGORITHM | ||
) | ||
return AccessToken(access_token=encoded_jwt, token_type="bearer") |
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 was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
from fastapi import APIRouter, Depends, FastAPI, HTTPException, status | ||
from fastapi.security import OAuth2PasswordRequestForm, OAuth2PasswordBearer | ||
from starlette.middleware import Middleware | ||
from starlette_context import plugins | ||
from starlette_context.middleware import RawContextMiddleware | ||
|
||
from api.endpoints.routes.innkeeper import router as innkeeper_router | ||
from api.endpoints.dependencies.jwt_security import AccessToken, create_access_token | ||
from api.core.config import settings as s | ||
|
||
|
||
middleware = [ | ||
Middleware( | ||
RawContextMiddleware, | ||
plugins=(plugins.RequestIdPlugin(), plugins.CorrelationIdPlugin()), | ||
), | ||
] | ||
|
||
router = APIRouter() | ||
|
||
|
||
def get_innkeeperapp() -> FastAPI: | ||
application = FastAPI( | ||
title=s.INNKEEPER_TITLE, | ||
description=s.INNKEEPER_DESCRIPTION, | ||
debug=s.DEBUG, | ||
middleware=middleware, | ||
) | ||
# mount the token endpoint | ||
application.include_router(router, prefix="") | ||
# mount other endpoints, these will be secured by the above token endpoint | ||
application.include_router( | ||
innkeeper_router, | ||
prefix=s.API_V1_STR, | ||
dependencies=[Depends(OAuth2PasswordBearer(tokenUrl="token"))], | ||
tags=["innkeeper"], | ||
) | ||
return application | ||
|
||
|
||
@router.post("/token", response_model=AccessToken) | ||
async def login_for_traction_api_admin( | ||
form_data: OAuth2PasswordRequestForm = Depends(), | ||
): | ||
authenticated = await authenticate_innkeeper(form_data.username, form_data.password) | ||
if not authenticated: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Incorrect Traction Api Admin User or Traction Api Admin Key", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
return create_access_token(data={"sub": form_data.username}) | ||
|
||
|
||
async def authenticate_innkeeper(username: str, password: str): | ||
if s.TRACTION_API_ADMIN_USER == username and s.TRACTION_API_ADMIN_KEY == password: | ||
return True | ||
return False |
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
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