-
Notifications
You must be signed in to change notification settings - Fork 5
Enabling Entra Auth #3251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
itsthatianguy
wants to merge
16
commits into
main
Choose a base branch
from
feature/CDD-3437-entra-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Enabling Entra Auth #3251
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
01eaf15
Enabling Entra auth
itsthatianguy debed12
formatting
itsthatianguy 5a43fcc
Ruff formatting fixes
itsthatianguy cadcb4b
Removed redundant check
itsthatianguy 48b35b8
Updating role capitalisation
itsthatianguy 89c0adb
capitalisation in tests
itsthatianguy 3177948
Requested cryptography package upgrade
itsthatianguy b2a2ecc
Merge branch 'main' into feature/CDD-3437-entra-auth
itsthatianguy f67df3e
Fixing header bug
itsthatianguy 454116d
Linting
itsthatianguy 73e2184
Merge branch 'main' into feature/CDD-3437-entra-auth
itsthatianguy 17cdbcb
Unified jwt header env var
itsthatianguy 8d75001
Merge branch 'main' into feature/CDD-3437-entra-auth
sahmed06 9870815
Multiple app ids allowed
itsthatianguy f9d0c71
Linting and test fixes
itsthatianguy 278e2ea
Env var update in readme
itsthatianguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains hidden or 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 hidden or 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,74 @@ | ||
| import logging | ||
|
|
||
| from django.contrib.auth import get_user_model | ||
| from django.contrib.auth.models import BaseUserManager | ||
| from rest_framework import exceptions | ||
|
|
||
| from cms.auth_content.models.users import User | ||
| from metrics.data.managers.rbac_models.user import UserManager | ||
| from metrics.utils.permission_hierarchy import build_permission_hierarchy | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def get_user_permission_set(user_id: str): | ||
| permissions = UserManager.get_permission_sets_for_user(user_id) | ||
| return build_permission_hierarchy(permissions) | ||
|
|
||
|
|
||
| class CognitoManager(BaseUserManager): | ||
|
|
||
| @staticmethod | ||
| def get_or_create(jwt_payload): | ||
| """Create an ephemeral user instance for this request. | ||
| If the permissions aren't present in the JWT, queries for them in | ||
| the database based on the entraObjectId in the token | ||
| """ | ||
| try: | ||
| username = jwt_payload["entraObjectId"] | ||
| # Check if the JWT already includes permissionSets | ||
| # Use if found, if not grab user permissions from the database | ||
| if "permissionSets" in jwt_payload: | ||
| permission_sets = jwt_payload["permissionSets"] | ||
| else: | ||
| permission_sets = get_user_permission_set(username) | ||
| except KeyError: | ||
| logger.debug( | ||
| "Error getting entraObjectId and/or permissionSets field(s)" | ||
| " from jwt payload: '%s'", | ||
| jwt_payload, | ||
| ) | ||
| return None | ||
|
|
||
| user_class = get_user_model() | ||
| user = user_class(username=username) | ||
| user.permission_sets = permission_sets | ||
| return user | ||
|
|
||
|
|
||
| class EntraManager(BaseUserManager): | ||
|
itsthatianguy marked this conversation as resolved.
|
||
|
|
||
| @staticmethod | ||
| def get_or_create(jwt_payload): | ||
| """Create an ephemeral user instance for this request. | ||
| If the provided appid isn't present in the database, raises | ||
| AuthenticationFailed exception | ||
| """ | ||
| try: | ||
| username = jwt_payload["appid"] | ||
| if not User.objects.filter(user_id=username).exists(): | ||
| msg = "Application not found." | ||
| raise exceptions.AuthenticationFailed(msg) | ||
| permission_sets = get_user_permission_set(username) | ||
| except KeyError: | ||
| logger.info( | ||
| "Error getting entraObjectId and/or permissionSets field(s)" | ||
| " from jwt payload: '%s'", | ||
| jwt_payload, | ||
| ) | ||
| return None | ||
|
|
||
| user_class = get_user_model() | ||
| user = user_class(username=username) | ||
| user.permission_sets = permission_sets | ||
| return user | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.