-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #12 --------- Co-authored-by: Hans Keeler <[email protected]>
- Loading branch information
1 parent
a656c63
commit 98635a0
Showing
12 changed files
with
218 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
KC_URL= | ||
KC_REALM= | ||
KC_ADMIN_CLIENT_ID= | ||
KC_ADMIN_CLIENT_SECRET= | ||
KC_REALM_URL=${KC_URL}/realms/${KC_REALM} | ||
KC_REALM_ADMIN_URL=${KC_URL}/admin/realms/${KC_REALM} | ||
AUTH_URL=${KC_REALM_URL}/protocol/openid-connect/auth | ||
TOKEN_URL=${KC_REALM_URL}/protocol/openid-connect/token | ||
CERTS_URL=${KC_REALM_URL}/protocol/openid-connect/certs | ||
AUTH_CLIENT= | ||
INST_DB_NAME= | ||
INST_DB_USER= | ||
INST_DB_PWD= | ||
INST_DB_HOST= | ||
INST_DB_SCHEMA= | ||
INST_CONN=postgresql+asyncpg://${INST_DB_USER}:${INST_DB_PWD}@${INST_DB_HOST}/${INST_DB_NAME} |
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,37 @@ | ||
from http import HTTPStatus | ||
from typing import Annotated | ||
from fastapi import Depends, HTTPException, Request | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from entities.engine import get_session | ||
from entities.repos import institutions_repo as repo | ||
|
||
OPEN_DOMAIN_REQUESTS = { | ||
"/v1/admin/me": {"GET"}, | ||
"/v1/institutions": {"GET"}, | ||
"/v1/institutions/domains/allowed": {"GET"}, | ||
} | ||
|
||
|
||
async def check_domain( | ||
request: Request, session: Annotated[AsyncSession, Depends(get_session)] | ||
) -> None: | ||
if request_needs_domain_check(request): | ||
if not request.user.is_authenticated: | ||
raise HTTPException(status_code=HTTPStatus.FORBIDDEN) | ||
if await email_domain_denied(session, request.user.email): | ||
raise HTTPException( | ||
status_code=HTTPStatus.FORBIDDEN, detail="email domain denied" | ||
) | ||
|
||
|
||
def request_needs_domain_check(request: Request) -> bool: | ||
path = request.scope["path"].rstrip("/") | ||
return not ( | ||
path in OPEN_DOMAIN_REQUESTS | ||
and request.scope["method"] in OPEN_DOMAIN_REQUESTS[path] | ||
) | ||
|
||
|
||
async def email_domain_denied(session: AsyncSession, email: str) -> bool: | ||
return not await repo.is_email_domain_allowed(session, email) |
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
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
Oops, something went wrong.