-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
167 additions
and
62 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ SPDX-FileCopyrightText: 2022 - 2023 Orthanc Team SRL <[email protected]> | |
SPDX-License-Identifier: GPL-3.0-or-later | ||
--> | ||
|
||
- added support for Api-keys through a new configuration file `API_KEYS_FILE_PATH` | ||
|
||
v 24.1.1 | ||
======== | ||
|
||
|
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,6 @@ | ||
// "SPDX-FileCopyrightText: 2022 - 2023 Orthanc Team SRL <[email protected]>" | ||
// SPDX-License-Identifier: CC0-1.0 | ||
{ | ||
"123456" : "admin-role", | ||
"abcde-fghb": "doctor-role" | ||
} |
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,58 @@ | ||
# SPDX-FileCopyrightText: 2022 - 2023 Orthanc Team SRL <[email protected]> | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import os | ||
import logging | ||
import requests | ||
import jwt | ||
import jsonc | ||
from typing import Dict, Any, List, Tuple | ||
from .models import * | ||
from .utils.utils import get_secret_or_die, is_secret_defined | ||
from .roles_configuration import RolesConfiguration | ||
|
||
class ApiKeys: | ||
|
||
def __init__(self, role_per_key: Dict[str, str], roles_configuration: RolesConfiguration): | ||
self.role_per_key = role_per_key | ||
self.roles_configuration = roles_configuration | ||
|
||
def get_role_from_api_key(self, api_key: str) -> List[str]: | ||
if api_key in self.role_per_key: | ||
return self.role_per_key[api_key] | ||
else: | ||
logging.warning(f"No role found for api-key") | ||
return None | ||
|
||
|
||
def get_user_profile_from_api_key(self, api_key: str) -> UserProfileResponse: | ||
response = UserProfileResponse( | ||
name="API KEY", | ||
permissions=[], | ||
validity=60, | ||
authorized_labels=[]) | ||
|
||
role = self.get_role_from_api_key(api_key=api_key) | ||
|
||
response.permissions, response.authorized_labels = self.roles_configuration.get_role_configuration([role]) | ||
|
||
return response | ||
|
||
def create_api_keys_from_file(api_keys_file_path: str, roles_configuration: RolesConfiguration): | ||
try: | ||
with open(api_keys_file_path) as f: | ||
role_per_key = jsonc.load(f) | ||
|
||
for key, role in role_per_key.items(): | ||
if role not in roles_configuration.get_roles(): | ||
logging.error(f"Unknown role in Api-keys file: {role}") | ||
exit(-1) | ||
|
||
return ApiKeys(role_per_key=role_per_key, roles_configuration=roles_configuration) | ||
|
||
except Exception as ex: | ||
logging.exception(ex) | ||
logging.error(f"Unable to get api-keys from configuration file ({api_keys_file_path}), exiting...") | ||
exit(-1) | ||
|
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
76 changes: 76 additions & 0 deletions
76
sources/orthanc_auth_service/shares/roles_configuration.py
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,76 @@ | ||
import logging | ||
import jsonc | ||
import os | ||
from .models import * | ||
from typing import Dict, Any, List, Tuple | ||
|
||
|
||
def _get_config_from_file(file_path: str): | ||
with open(file_path) as f: | ||
data = jsonc.load(f) | ||
|
||
roles = data.get('roles') | ||
|
||
for key, role_def in roles.items(): | ||
if not role_def.get("authorized_labels"): | ||
msg = f'No "authorized_labels" defined for role "{key}". You should, e.g, include "authorized_labels" = ["*"] if you want to authorize all labels.")' | ||
logging.error(msg) | ||
raise ValueError(msg) | ||
|
||
if not role_def.get("permissions"): | ||
msg = f'No "permissions" defined for role "{key}". You should, e.g, include "permissions" = ["all"] if you want to authorize all permissions.")' | ||
logging.error(msg) | ||
raise ValueError(msg) | ||
|
||
return roles | ||
|
||
|
||
class RolesConfiguration: | ||
|
||
def __init__(self, roles: Dict[str, Any]): | ||
self.configured_roles = roles | ||
|
||
def get_roles(self) -> List[str]: | ||
return self.configured_roles.keys() | ||
|
||
def get_role_configuration(self, roles: List[str]) -> Tuple[List[UserPermissions], List[str], List[str]]: | ||
permissions = [] | ||
authorized_labels = [] | ||
configured_user_roles = [] | ||
|
||
for r in roles: | ||
if r in self.configured_roles: | ||
configured_user_roles.append(r) | ||
|
||
# complain if there are 2 roles for the same user ??? How should we combine the authorized and forbidden labels in this case ??? | ||
if len(configured_user_roles) > 1: | ||
raise ValueError("Unable to handle multiple roles for a single user") | ||
|
||
role = configured_user_roles[0] | ||
# search for it in the configured roles | ||
configured_role = self.configured_roles.get(role) | ||
# if it has been configured: | ||
if configured_role is None: | ||
raise ValueError(f"Role not found in configuration: {role}") | ||
|
||
for item in configured_role.get('permissions'): | ||
# (if not already there) | ||
if UserPermissions(item) not in permissions: | ||
permissions.append(UserPermissions(item)) | ||
|
||
if configured_role.get("authorized_labels"): | ||
authorized_labels = configured_role.get("authorized_labels") | ||
|
||
return permissions, authorized_labels | ||
|
||
def create_roles_configuration_from_file(permissions_file_path: str): | ||
try: | ||
roles = _get_config_from_file(permissions_file_path) | ||
logging.info(f"Got the roles and permissions from configuration file") | ||
|
||
except Exception as ex: | ||
logging.exception(ex) | ||
logging.error(f"Unable to get roles and permissions from configuration file ({permissions_file_path}), exiting...") | ||
exit(-1) | ||
|
||
return RolesConfiguration(roles) |