Skip to content

Commit 014e144

Browse files
committed
Add request validation function as dependency
1 parent 9874e8f commit 014e144

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

code/function/fastapp/health/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Annotated
2+
from fastapi import Header, HTTPException
3+
from hashlib import sha256
4+
import base64
5+
from fastapp.core.config import settings
6+
7+
async def verify_health_auth_header(x_ms_auth_internal_token: Annotated[str, Header()]) -> bool:
8+
"""Returns true if SHA256 of header_value matches WEBSITE_AUTH_ENCRYPTION_KEY.
9+
10+
x_ms_auth_internal_token: Value of the x-ms-auth-internal-token header.
11+
RETURNS (bool): Specifies whether the header matches.
12+
"""
13+
website_auth_encryption_key = settings.WEBSITE_AUTH_ENCRYPTION_KEY
14+
hash = base64.b64encode(sha256(website_auth_encryption_key.encode('utf-8')).digest()).decode('utf-8')
15+
if hash != x_ms_auth_internal_token:
16+
raise HTTPException(status_code=400, detail="x-ms-auth-internal-token is invalid")
17+
else:
18+
return True

0 commit comments

Comments
 (0)