Skip to content

Commit

Permalink
made different header called Absent-Auth to replace Authorization hea…
Browse files Browse the repository at this point in the history
…der b/c of Apple and its quirks :)
  • Loading branch information
kvnyng committed Feb 27, 2022
1 parent b199fe1 commit 823ab6c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
38 changes: 30 additions & 8 deletions src/dataTypes/headers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

from curses.ascii import HT
from fastapi import HTTPException
from fastapi.security.http import HTTPBase
from fastapi.security.base import SecurityBase
from fastapi.openapi.models import HTTPBearer as HTTPBearerModel
from pydantic import BaseModel, validator
from typing import Optional
Expand All @@ -13,12 +15,32 @@ class HTTPAuthCreds(BaseModel):
scheme: str
credentials: str

# Basic checking for the creds
# @validator('credentials')
# def checkCredentials(cls, v):
# if len(v) != 64:
# raise ValueError('Invalid Credentials from validator.')
# return v
class HTTPBase(SecurityBase):
def __init__(
self,
*,
scheme: str,
scheme_name: Optional[str] = None,
description: Optional[str] = None,
auto_error: bool = True,
):
self.model = HTTPAuthCreds(scheme=scheme, description=description)
self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error

async def __call__(
self, request: Request
) -> Optional[HTTPAuthCreds]:
authorization: str = request.headers.get("Absent-Auth")
scheme, credentials = get_authorization_scheme_param(authorization)
if not (authorization and scheme and credentials):
if self.auto_error:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
)
else:
return None
return HTTPAuthCreds(scheme=scheme, credentials=credentials)

class HTTPAuth(HTTPBase):
def __init__(
Expand All @@ -36,7 +58,7 @@ def __init__(
async def __call__(
self, request: Request
) -> Optional[HTTPAuthCreds]:
authorization: str = request.headers.get("Authorization")
authorization: str = request.headers.get("Absent-Auth")
scheme, credentials = get_authorization_scheme_param(authorization)
if not (authorization and scheme and credentials):
if self.auto_error:
Expand All @@ -45,7 +67,7 @@ async def __call__(
)
else:
return None
if scheme.lower() != "absent-auth":
if scheme.lower() != "bearer":
if self.auto_error:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN,
Expand Down
2 changes: 1 addition & 1 deletion static/swagger-ui-bundle.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/unit/api/users/test_api_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class GetInfo(unittest.TestCase):

def test_get_users(self):
response = client.get("v1/users/me/info",
headers= { "Authorization": f"absent-auth {self.TROJANHORSE.token}" }
headers= { "Absent-Auth": f"Bearer {self.TROJANHORSE.token}" }
)
print(response.json())
print(self.TROJANHORSE.pseudo_info)
Expand All @@ -66,7 +66,7 @@ class Sessions(unittest.TestCase):

def test_get_sessions(self):
response = client.get("v1/users/me/sessions",
headers= { "Authorization": f"absent-auth {self.TROJANHORSE.token}" }
headers= { "Absent-Auth": f"Bearer {self.TROJANHORSE.token}" }
)
self.assertEqual(response.status_code, 200), "Failed to return 200 when Authorization header is present"

Expand Down

0 comments on commit 823ab6c

Please sign in to comment.