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

Bugfix/Authentication #215

Merged
merged 6 commits into from
Dec 5, 2023
Merged

Bugfix/Authentication #215

merged 6 commits into from
Dec 5, 2023

Conversation

josvandervelde
Copy link

Use keycloak_openid.introspect(token) instead of keycloak_openid.userinfo(token).

The previous code used the bearer token to authenticate to Keycloak. In other words, it authenticated with Keycloak in the name of the user. (http://openid.net/specs/openid-connect-core-1_0.html#UserInfo) This meant that the client_id and client_secret were ignored. This is not directly unsafe, but it removes a layer of defense.

Instead, we want to authenticate as the AIoD API using the client_id and client_secret. We can do that using introspect: https://datatracker.ietf.org/doc/html/rfc7662.

Couple of other changes:

  1. Made sure that a User object is returned, with only the username and roles. This way, we're sure not to leak any other information to our own application, and by extension to endpoints such as /authentication-test (just as precaution). Moreover, it simplifies the rest of the code, making authorization-checks less error-prone.
  2. Checking the introspected information: is the user still active?
  3. Added unittests

@josvandervelde josvandervelde changed the title Bugfix/token introspection Bugfix/Authentication Nov 30, 2023
@josvandervelde josvandervelde self-assigned this Dec 4, 2023
Copy link
Member

@jsmatias jsmatias left a comment

Choose a reason for hiding this comment

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

All good!
I just left a few comments and maybe add one more test to ensure no more information is returned about the user apart from username and roles.

Regarding this: "2. Checking the introspected information: is the user still active?
". How does the checking if the user is still active done? Is it implicitly done as part of the keycloak_openid.introspect() or is it implemented in the code more explicitly?

Other than that, all good! :)

src/authentication.py Show resolved Hide resolved
src/tests/testutils/mock_keycloak.py Show resolved Hide resolved
src/authentication.py Show resolved Hide resolved
src/tests/test_authentication.py Show resolved Hide resolved
@josvandervelde
Copy link
Author

Regarding this: "2. Checking the introspected information: is the user still active? ". How does the checking if the user is still active done? Is it implicitly done as part of the keycloak_openid.introspect() or is it implemented in the code more explicitly?

This is done in authentication.py:

if not userinfo.get("active", False):
     logging.error("Invalid userinfo or inactive user.")
     raise RuntimeError("Invalid userinfo or inactive user.")  # caught below

Copy link
Member

@jsmatias jsmatias left a comment

Choose a reason for hiding this comment

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

I'm approving it.
Well done, nice job!

@josvandervelde josvandervelde merged commit de3d7ba into develop Dec 5, 2023
2 checks passed
@josvandervelde josvandervelde deleted the bugfix/token_introspection branch December 5, 2023 09:56
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.

2 participants