Skip to content

Commit 1efc7c0

Browse files
CH-45 Update main template to support bearer and cookie access tokens
1 parent 3def2a6 commit 1efc7c0

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

  • application-templates/django-app/api/templates

application-templates/django-app/api/templates/main.jinja2

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ app.add_middleware(
4949
allow_headers=["*"],
5050
)
5151

52-
from cloudharness.middleware import set_authentication_token
52+
from cloudharness.middleware import set_authentication_token, get_authentication_token
5353
@app.middleware("http")
5454
async def add_process_time_header(request: Request, call_next):
5555
# retrieve the bearer token from the header
5656
# and save it for use in the AuthClient
57-
authorization = request.headers.get('Authorization')
57+
authorization = request.headers.get('Authorization') or request.cookies.get('kc-access')
58+
5859
if authorization:
60+
if 'Bearer ' in authorization:
61+
authorization = authorization.split('Bearer ')[1]
62+
5963
set_authentication_token(authorization)
6064

6165
return await call_next(request)
@@ -67,16 +71,17 @@ if os.environ.get('KUBERNETES_SERVICE_HOST', None):
6771
# start the kafka event listener when running in/for k8s
6872
import cloudharness_django.services.events
6973

70-
# enable the Bearer Authentication
71-
security = HTTPBearer()
72-
73-
async def has_access(credentials: HTTPBasicCredentials = Depends(security)):
74+
async def has_access():
7475
"""
7576
Function that is used to validate the token in the case that it requires it
7677
"""
7778
if not os.environ.get('KUBERNETES_SERVICE_HOST', None):
7879
return {}
79-
token = credentials.credentials
80+
81+
token = get_authentication_token()
82+
83+
if not token:
84+
raise HTTPException(status_code=401)
8085

8186
try:
8287
payload = get_auth_service().get_auth_client().decode_token(token)

0 commit comments

Comments
 (0)