Skip to content
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

Fixed OIDC authentication for SCIM endpoints #43

Merged
merged 2 commits into from
Oct 2, 2024

Conversation

QuanMPhm
Copy link
Contributor

Previously, the Coldfront SCIM endpoints could not perform OIDC authentication, preventing usage of the client credentials and device authorization flows.

A authentication middleware which subclasses from the one provided by django_scim has been added, which will perform OIDC authentication given an access token in the "Authorization" HTTP request header.

The SCIM endpoint is now available to Coldfront users with "staff" or "superuser" status.

Copy link
Collaborator

@knikolla knikolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are in no urgency to merge this now, I think it is important to use this opportunity to make our testing infrastructure more robust by including authentication for testing that OAuth authentication works correctly for the API interface.

Please introduce a new functional CI job that starts up and configures a Keycloak container and checks that authentication proceeds correctly for staff users using an access token and denies correctly unauthenticated users.

Feel free to use this class to help you create a Keycloak client and users as necessary. https://github.com/CCI-MOC/onboarding-tools/blob/master/onboarding_tools/keycloak.py

Previously, the Coldfront SCIM endpoints could not perform OIDC authentication,
preventing usage of the client credentials and device authorization flows.

A authentication middleware which subclasses from the one provided by
`django_scim` has been added, which will perform OIDC authentication
given an access token in the "Authorization" HTTP request header.

The SCIM endpoint is now available to Coldfront users with
"staff" or "superuser" status. It will perform OIDC authentication
if the `PLUGIN_AUTH_OIDC` env var is set to True.
@QuanMPhm QuanMPhm requested a review from knikolla August 29, 2024 19:46
@knikolla knikolla requested a review from larsks August 29, 2024 20:04
Copy link
Collaborator

@knikolla knikolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me. A minor comment on the comment (lol) that can be fixed in a later PR.

class SCIMColdfrontAuthCheckMiddleware(SCIMAuthCheckMiddleware):
def process_request(self, request):
if not request.user or not request.user.is_authenticated:
# Check if OIDC authentication is enabled to see if we should authenticate user using OIDC
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're still explaining the what. A better comment would be

# django-scim2 does not use by default the DRF backend of mozilla-django-oidc,
# and therefore does not support authentication with bearer tokens, only
# session cookies. We manually call authenticate on the DRF backend in case
# of failed authentication, to try again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QuanMPhm I think you should update the comment and then it's good to merge.

Copy link
Contributor

@naved001 naved001 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll need to read more about this before I can confidently approve it as this is not very familiar to me.

class SCIMColdfrontAuthCheckMiddleware(SCIMAuthCheckMiddleware):
def process_request(self, request):
if not request.user or not request.user.is_authenticated:
# Check if OIDC authentication is enabled to see if we should authenticate user using OIDC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QuanMPhm I think you should update the comment and then it's good to merge.

Copy link
Collaborator

@knikolla knikolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With time pressure being removed, I would also like to see the updated comment be part of this PR now.

A new CI job has been added to test the OIDC authentication for the
API endpoints. This CI starts Keycloak in a container, installs coldfront
then runs the API functional tests.

A few code decisions to note:
- The functional tests subclassed the keycloak client provided by `CCI-MOC/onboarding-tools`.
Subclassing was needed because the original client class contained code which is out-of-date
with the current version of Keycloak
- The `is_user_superuser()` function in `utils.py` now also checks if `user.is_superuser`,
to account for edge case where a superuser is created but their `is_staff` attribute is
not set.
@QuanMPhm
Copy link
Contributor Author

QuanMPhm commented Oct 1, 2024

I have updated the comment

@QuanMPhm QuanMPhm requested a review from knikolla October 1, 2024 14:19
@knikolla knikolla merged commit 45a880b into nerc-project:main Oct 2, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants