Skip to content

feat(keycloak authentication) #4162

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
wants to merge 72 commits into
base: master
Choose a base branch
from
Open

feat(keycloak authentication) #4162

wants to merge 72 commits into from

Conversation

ChrOertlin
Copy link
Contributor

@ChrOertlin ChrOertlin commented Jan 28, 2025

Description

Implements keycloak as authirsation and verification provider for CG

Added

  • AuthenticationService
    Handles logic pertaining to authentication and token verification of users

  • UserService
    handles fetching of users from the database - introduced to reduce coupling of database to endpoints

  • New endpoints:
    auth/login
    auth/callback
    auth/logout

  • New config settings in the Flask AppConfig to initiate the authentication service

Changed

  • Moved app.route("/") to its own blueprint to cleanup the _register_blueprint() function. Increased code clarity

  • Removed google oauth flow from front end

Fixed

closes: #4159

How to prepare for test

  • Ssh to relevant server (depending on type of change)
  • Use stage: us
  • Paxa the environment: paxa
  • Install on stage (example for Hasta):
    bash /home/proj/production/servers/resources/hasta.scilifelab.se/update-tool-stage.sh -e S_cg -t cg -b [THIS-BRANCH-NAME] -a

How to test

  • Do ...

Expected test outcome

  • Check that ...
  • Take a screenshot and attach or copy/paste the output.

Review

  • Tests executed by
  • "Merge and deploy" approved by
    Thanks for filling in who performed the code review and the test!

This version is a

  • MAJOR - when you make incompatible API changes
  • MINOR - when you add functionality in a backwards compatible manner
  • PATCH - when you make backwards compatible bug fixes or documentation/instructions

Implementation Plan

  • Document in ...
  • Deploy this branch on ...
  • Inform to ...

Copy link

sonarqubecloud bot commented May 7, 2025

@clingen-sthlm clingen-sthlm temporarily deployed to stage May 7, 2025 15:15 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 7, 2025 15:17 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 7, 2025 15:22 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 7, 2025 16:29 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 9, 2025 11:51 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 13, 2025 01:24 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 13, 2025 11:38 Inactive
Comment on lines +45 to +47
self.keycloak_client: KeycloakClient = keycloak_client
self.keycloak_backend_user = config["trailblazer"]["keycloak_backend_user"]
self.keycloak_backend_user_password = config["trailblazer"][
Copy link
Contributor

Choose a reason for hiding this comment

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

We should create a confidential client with a client secret. This should allow us to circumvent using username+password
Screenshot 2025-05-13 at 14 15 28
rd

Comment on lines +129 to +130
keycloak_backend_user: str
keycloak_backend_user_password: str
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove and use client_secret_key instead

Comment on lines +56 to +65
def check_role(roles: list[str]) -> None:
"""Check the user roles.
Currently set to a single permissable role, expand if needed.
Args:
roles (list[str]): The user roles received from the RealmAccess.
Raises:
UserRoleError: if required role not present
"""
if not "cg-employee" in roles:
raise UserRoleError("The user does not have the required role to access this service.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Good implementation. Will depend on how our user groups are set-up. Likely to change in some degree

if code:
token = keycloak_client.get_token_by_authorisation_code(code)
parsed_token = TokenResponseModel(**token)
LOG.info(f"access_token: {parsed_token.access_token}")
Copy link
Contributor

Choose a reason for hiding this comment

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

remove logging the access token

Comment on lines +25 to +26

LOG = logging.getLogger(__name__)
Copy link
Contributor

Choose a reason for hiding this comment

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

unsure why the logging is needed

Comment on lines +49 to +50
keycloak_backend_user: str = "user"
keycloak_backend_user_password: str = "password"
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure these are needed

@clingen-sthlm clingen-sthlm temporarily deployed to stage May 13, 2025 21:02 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 14, 2025 08:21 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 14, 2025 11:12 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 14, 2025 15:14 Inactive
Comment on lines -31 to -37
if not logged_in():
return redirect(url_for("admin.index"))


def logged_in():
user = db.get_user_by_email(email=session.get("user_email"))
return google.authorized and user and user.is_admin
Copy link
Contributor

Choose a reason for hiding this comment

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

For phase one we could keep the database check and start relying on roles in phase 2

@clingen-sthlm clingen-sthlm temporarily deployed to stage May 15, 2025 09:29 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 16, 2025 07:17 Inactive
@clingen-sthlm clingen-sthlm temporarily deployed to stage May 16, 2025 12:44 Inactive
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.

Enable keycloak authentication in CG
5 participants