-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat(keycloak authentication) #4162
Draft
ChrOertlin
wants to merge
8
commits into
master
Choose a base branch
from
feat-keycloak
base: master
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.
+377
−48
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
49f1254
chore: remove google auth verification
ChrOertlin b8d6103
Merge branch 'master' into feat-keycloak
ChrOertlin 5d348f7
feat: add python-keycloak dependency
ChrOertlin d6c2eeb
Add keycloak client
ahdamin 2ae86a3
chore: test
ChrOertlin 4f617a4
feat: implement auth service
ChrOertlin ac275da
feat: fix before request
ChrOertlin b2f3397
chore: add more user util
ChrOertlin 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ADMIN: str = "admin" | ||
CUSTOMER: str = "customer" |
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,8 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class AuthenticatedUser(BaseModel): | ||
id: int | ||
username: str | ||
email: str | ||
role: str |
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,110 @@ | ||
from cycler import V | ||
from cg.services.authentication.constants import ADMIN, CUSTOMER | ||
from cg.store.models import User | ||
from cg.store.store import Store | ||
from keycloak import KeycloakError, KeycloakOpenID | ||
from cg.services.authentication.models import AuthenticatedUser | ||
|
||
|
||
class AuthenticationService: | ||
"""Authentication service user to verify tokens against keycloak and return user information.""" | ||
|
||
def __init__( | ||
self, store: Store, server_url: str, client_id: str, client_secret: str, realm_name: str | ||
): | ||
"""_summary_ | ||
|
||
Args: | ||
store (Store): Connection to statusDB | ||
server_url (str): server url to the keycloak server or container | ||
realm_name (str): the keycloak realm to connect to (can be found in keycloak) | ||
client_id (str): the client id to use in keycloak realm (can be found in keycloak) | ||
client_secret (str): the client secret to use in keycloak realm (can be found in keycloak) | ||
""" | ||
self.store: Store = store | ||
self.server_url: str = server_url | ||
self.client_id: str = client_id | ||
self.client_secret: str = client_secret | ||
self.realm_name: str = realm_name | ||
self.client: KeycloakOpenID = self._get_client() | ||
|
||
def _get_client(self): | ||
"""Set the KeycloakOpenID client. | ||
""" | ||
keycloak_openid_client = KeycloakOpenID( | ||
server_url=self.server_url, | ||
client_id=self.client_id, | ||
realm_name=self.realm_name, | ||
client_secret_key=self.client_secret, | ||
) | ||
return keycloak_openid_client | ||
|
||
def verify_token(self, token: str) -> AuthenticatedUser: | ||
"""Verify the token and return the user. | ||
args: | ||
token: str | ||
returns: | ||
AuthenticatedUser | ||
raises: | ||
ValueError: if the token is not active | ||
""" | ||
token_info = self.client.introspect(token) | ||
|
||
if not token_info['active']: | ||
raise ValueError('Token is not active') | ||
verified_token = self.client.decode_token(token) | ||
|
||
user_email = verified_token["email"] | ||
return self._get_user_reponse(user_email) | ||
|
||
|
||
|
||
def _get_user_by_email(self, email: str): | ||
""" | ||
Get user by email. | ||
args: | ||
email: str | ||
returns: | ||
User | ||
raises: | ||
ValueError: if the user is not found | ||
""" | ||
user: User | None = self.store.get_user_by_email(email) | ||
if not user: | ||
raise ValueError(f"User with email {email} not found") | ||
return user | ||
|
||
def _check_role(self, user: User) -> str: | ||
""" | ||
Check if user has the role. | ||
args: | ||
user: User | ||
returns: | ||
str | ||
raises: | ||
ValueError: if the user does not have access | ||
""" | ||
if user.is_admin: | ||
return ADMIN | ||
if user.order_portal_login: | ||
return CUSTOMER | ||
raise ValueError('User does not have access') | ||
|
||
def _get_user_reponse(self, email: str) -> AuthenticatedUser: | ||
""" | ||
Get user response. | ||
args: | ||
email: str | ||
returns: | ||
AuthenticatedUser | ||
""" | ||
user = self._get_user_by_email(email) | ||
role: str = self._check_role(user) | ||
return AuthenticatedUser( | ||
id=user.id, | ||
username=user.name, | ||
email=user.email, | ||
role=role, | ||
) | ||
Comment on lines
+103
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently we set the DB_USER as flask user. We should likely avoid that it couples Database models to the front end directly. |
||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In future this role checking should be just done by keycloak. Currently, we have do this by checking a bool in the user table.
Not sure if we want this kind of mapping right now or just straight up set the same bools, we can discuss this.
Introducing this kind of mapping would require some refactoring.
for example we have checks where: