2020-11-28 14:47:10 +01:00
|
|
|
# w.c.s. - web application for online forms
|
|
|
|
# Copyright (C) 2005-2020 Entr'ouvert
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-08-29 14:22:08 +02:00
|
|
|
import json
|
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
from quixote import get_publisher
|
|
|
|
|
2020-11-28 14:47:10 +01:00
|
|
|
from wcs.qommon.xml_storage import XmlStorableObject
|
|
|
|
|
2022-08-29 14:22:08 +02:00
|
|
|
from .qommon.errors import ConnectionError
|
|
|
|
from .qommon.misc import http_post_request
|
|
|
|
|
2020-11-28 14:47:10 +01:00
|
|
|
|
|
|
|
class ApiAccess(XmlStorableObject):
|
|
|
|
_names = 'apiaccess'
|
|
|
|
xml_root_node = 'apiaccess'
|
|
|
|
name = None
|
|
|
|
access_identifier = None
|
|
|
|
access_key = None
|
|
|
|
description = None
|
2021-04-17 10:23:48 +02:00
|
|
|
restrict_to_anonymised_data = False
|
2024-03-27 07:55:42 +01:00
|
|
|
idp_api_client = False
|
2021-12-18 13:07:09 +01:00
|
|
|
_roles = None
|
2022-08-29 14:22:08 +02:00
|
|
|
_role_ids = Ellipsis
|
2020-11-28 14:47:10 +01:00
|
|
|
|
|
|
|
# declarations for serialization
|
|
|
|
XML_NODES = [
|
|
|
|
('name', 'str'),
|
|
|
|
('description', 'str'),
|
|
|
|
('access_identifier', 'str'),
|
|
|
|
('access_key', 'str'),
|
2021-04-17 10:23:48 +02:00
|
|
|
('restrict_to_anonymised_data', 'bool'),
|
2021-04-17 13:08:16 +02:00
|
|
|
('roles', 'roles'),
|
2024-03-27 07:55:42 +01:00
|
|
|
('idp_api_client', 'bool'),
|
2020-11-28 14:47:10 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
@classmethod
|
2021-04-17 10:23:48 +02:00
|
|
|
def get_by_identifier(cls, access_identifier):
|
2020-11-28 14:47:10 +01:00
|
|
|
for api_access in cls.select():
|
|
|
|
if api_access.access_identifier == access_identifier:
|
2021-04-17 10:23:48 +02:00
|
|
|
return api_access
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_access_key(cls, access_identifier):
|
|
|
|
api_access = cls.get_by_identifier(access_identifier)
|
|
|
|
if api_access:
|
|
|
|
return api_access.access_key
|
2020-11-28 14:47:10 +01:00
|
|
|
return None
|
2021-04-17 13:08:16 +02:00
|
|
|
|
|
|
|
def get_roles(self):
|
|
|
|
return self.roles or []
|
|
|
|
|
2022-08-29 14:22:08 +02:00
|
|
|
def get_role_ids(self):
|
|
|
|
if self._role_ids is not Ellipsis:
|
|
|
|
return self._role_ids
|
|
|
|
self._role_ids = [x.id for x in self.get_roles()]
|
|
|
|
return self._role_ids
|
|
|
|
|
2021-04-17 14:40:28 +02:00
|
|
|
def get_as_api_user(self):
|
|
|
|
class RestrictedApiUser:
|
|
|
|
# kept as inner class so cannot be pickled
|
|
|
|
id = Ellipsis # make sure it fails all over the place if used
|
|
|
|
is_admin = False
|
|
|
|
is_api_user = True
|
|
|
|
|
2021-05-10 13:47:51 +02:00
|
|
|
def __init__(self, api_access):
|
|
|
|
self.api_access = api_access
|
|
|
|
|
2021-05-07 19:37:27 +02:00
|
|
|
def can_go_in_admin(self):
|
|
|
|
return False
|
|
|
|
|
2021-05-02 17:28:09 +02:00
|
|
|
def can_go_in_backoffice(self):
|
|
|
|
return False
|
|
|
|
|
2021-04-17 14:40:28 +02:00
|
|
|
def get_roles(self):
|
|
|
|
return self.roles
|
|
|
|
|
2022-11-04 15:34:03 +01:00
|
|
|
def get_substitution_variables(self):
|
|
|
|
return {}
|
|
|
|
|
2021-05-10 13:47:51 +02:00
|
|
|
user = RestrictedApiUser(self)
|
2022-08-29 14:22:08 +02:00
|
|
|
user.roles = self.get_role_ids()
|
2021-04-17 14:40:28 +02:00
|
|
|
return user
|
2021-05-02 17:28:09 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_with_credentials(cls, username, password):
|
|
|
|
api_access = cls.get_by_identifier(username)
|
2024-03-27 07:55:42 +01:00
|
|
|
if not api_access or api_access.access_key != password or api_access.idp_api_client:
|
2022-08-29 14:22:08 +02:00
|
|
|
api_access = cls.get_from_idp(username, password)
|
|
|
|
if not api_access:
|
|
|
|
raise KeyError
|
2021-05-02 17:28:09 +02:00
|
|
|
return api_access.get_as_api_user()
|
2021-12-18 13:07:09 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def roles(self):
|
|
|
|
return self._roles() if callable(self._roles) else self._roles
|
|
|
|
|
|
|
|
@roles.setter
|
|
|
|
def roles(self, value):
|
|
|
|
self._roles = value
|
2022-08-29 14:22:08 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_from_idp(cls, username, password):
|
|
|
|
from wcs.api_utils import get_secret_and_orig, sign_url
|
|
|
|
|
|
|
|
idp_api_url = get_publisher().get_site_option('idp_api_url', 'variables') or ''
|
|
|
|
if not idp_api_url:
|
|
|
|
return None
|
|
|
|
|
|
|
|
url = urllib.parse.urljoin(idp_api_url, 'check-api-client/')
|
|
|
|
secret, orig = get_secret_and_orig(url)
|
|
|
|
url += '?orig=%s' % orig
|
|
|
|
headers = {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-type': 'application/json',
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
response, status, _, _ = http_post_request(
|
|
|
|
sign_url(url, secret),
|
|
|
|
body=json.dumps({'identifier': username, 'password': password}),
|
|
|
|
headers=headers,
|
|
|
|
)
|
|
|
|
except ConnectionError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if status != 200:
|
|
|
|
return None
|
|
|
|
|
|
|
|
data = response.json()
|
|
|
|
if data.get('err', 1) != 0:
|
|
|
|
return None
|
|
|
|
|
2024-03-27 07:55:42 +01:00
|
|
|
# cache api client locally, it is necessary for serialization for afterjobs
|
|
|
|
# in uwsgi spooler.
|
|
|
|
access_identifier = f'_idp_{username}'
|
|
|
|
api_access = cls.get_by_identifier(access_identifier) or cls()
|
|
|
|
api_access.idp_api_client = True
|
|
|
|
api_access.access_identifier = access_identifier
|
|
|
|
role_class = get_publisher().role_class
|
2022-08-29 14:22:08 +02:00
|
|
|
try:
|
|
|
|
api_access.restrict_to_anonymised_data = data['data']['restrict_to_anonymised_data']
|
2024-03-27 07:55:42 +01:00
|
|
|
api_access.roles = [role_class.get(x, ignore_errors=True) for x in data['data']['roles']]
|
|
|
|
api_access.roles = [x for x in api_access.roles if x is not None]
|
2022-08-29 14:22:08 +02:00
|
|
|
except KeyError:
|
|
|
|
return None
|
2024-03-27 07:55:42 +01:00
|
|
|
api_access.store()
|
2022-08-29 14:22:08 +02:00
|
|
|
return api_access
|