Created
May 10, 2019 21:22
-
-
Save nilsdebruin/d2dbc365eaf531cf946f59e86d2aa140 to your computer and use it in GitHub Desktop.
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
class Token(BaseModel): | |
access_token: str | |
token_type: str | |
class TokenData(BaseModel): | |
username: str = None | |
email: str = None | |
class User(BaseModel): | |
username: str | |
email: str = None | |
full_name: str = None | |
disabled: bool = None | |
class OAuth2PasswordBearerCookie(OAuth2): | |
def __init__( | |
self, | |
tokenUrl: str, | |
scheme_name: str = None, | |
scopes: dict = None, | |
auto_error: bool = True, | |
): | |
if not scopes: | |
scopes = {} | |
flows = OAuthFlowsModel(password={"tokenUrl": tokenUrl, "scopes": scopes}) | |
super().__init__(flows=flows, scheme_name=scheme_name, auto_error=auto_error) | |
async def __call__(self, request: Request) -> Optional[str]: | |
header_authorization: str = request.headers.get("Authorization") | |
cookie_authorization: str = request.cookies.get("Authorization") | |
header_scheme, header_param = get_authorization_scheme_param( | |
header_authorization | |
) | |
cookie_scheme, cookie_param = get_authorization_scheme_param( | |
cookie_authorization | |
) | |
if header_scheme.lower() == "bearer": | |
authorization = True | |
scheme = header_scheme | |
param = header_param | |
elif cookie_scheme.lower() == "bearer": | |
authorization = True | |
scheme = cookie_scheme | |
param = cookie_param | |
else: | |
authorization = False | |
if not authorization or scheme.lower() != "bearer": | |
if self.auto_error: | |
raise HTTPException( | |
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated" | |
) | |
else: | |
return None | |
return param | |
oauth2_scheme = OAuth2PasswordBearerCookie(tokenUrl="/token") | |
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment