oldaplib 0.2.6__py3-none-any.whl → 0.2.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- oldaplib/src/connection.py +32 -6
- oldaplib/src/version.py +1 -0
- oldaplib/test/test_connection.py +9 -2
- {oldaplib-0.2.6.dist-info → oldaplib-0.2.8.dist-info}/METADATA +1 -1
- {oldaplib-0.2.6.dist-info → oldaplib-0.2.8.dist-info}/RECORD +6 -6
- oldaplib/version.py +0 -1
- {oldaplib-0.2.6.dist-info → oldaplib-0.2.8.dist-info}/WHEEL +0 -0
oldaplib/src/connection.py
CHANGED
|
@@ -14,6 +14,9 @@ from pathlib import Path
|
|
|
14
14
|
|
|
15
15
|
from requests.auth import HTTPBasicAuth
|
|
16
16
|
|
|
17
|
+
from oldaplib.src.oldaplogging import get_logger
|
|
18
|
+
from oldaplib.src.version import __version__
|
|
19
|
+
|
|
17
20
|
from oldaplib.src.cachesingleton import CacheSingleton, CacheSingletonRedis
|
|
18
21
|
from oldaplib.src.enums.adminpermissions import AdminPermission
|
|
19
22
|
from oldaplib.src.userdataclass import UserData
|
|
@@ -144,11 +147,15 @@ class Connection(IConnection):
|
|
|
144
147
|
self._query_url = f'{self._server}/repositories/{self._repo}'
|
|
145
148
|
self._update_url = f'{self._server}/repositories/{self._repo}/statements'
|
|
146
149
|
self._store = SPARQLUpdateStore(self._query_url, self._update_url)
|
|
150
|
+
|
|
151
|
+
logger = get_logger()
|
|
152
|
+
|
|
147
153
|
context = Context(name=context_name)
|
|
148
154
|
if token is not None:
|
|
149
155
|
try:
|
|
150
156
|
payload = jwt.decode(jwt=token, key=Connection.__jwtkey, algorithms="HS256")
|
|
151
157
|
except InvalidTokenError:
|
|
158
|
+
logger.error("Connection with invalid token")
|
|
152
159
|
raise OldapError("Wrong credentials")
|
|
153
160
|
self._userdata = json.loads(payload['userdata'], object_hook=serializer.decoder_hook)
|
|
154
161
|
self._token = token
|
|
@@ -158,6 +165,7 @@ class Connection(IConnection):
|
|
|
158
165
|
if not isinstance(userId, Xsd_NCName):
|
|
159
166
|
userId = Xsd_NCName(userId)
|
|
160
167
|
if userId is None:
|
|
168
|
+
logger.error("Connection with wrong credentials")
|
|
161
169
|
raise OldapError("Wrong credentials")
|
|
162
170
|
sparql = UserData.sparql_query(context=context, userId=userId)
|
|
163
171
|
headers = {
|
|
@@ -168,22 +176,25 @@ class Connection(IConnection):
|
|
|
168
176
|
'query': sparql,
|
|
169
177
|
}
|
|
170
178
|
#
|
|
171
|
-
# if we have protected the triplestore by a user/password, add it
|
|
179
|
+
# if we have protected the triplestore by a user/password, add it to the request
|
|
172
180
|
#
|
|
173
181
|
auth = HTTPBasicAuth(self._dbuser, self._dbpassword) if self._dbuser and self._dbpassword else None
|
|
174
182
|
res = requests.post(url=self._query_url, headers=headers, data=data, auth=auth)
|
|
175
183
|
if res.status_code == 200:
|
|
176
184
|
jsonobj = res.json()
|
|
177
185
|
else:
|
|
186
|
+
logger.error(f"Could not connect to triplestore: {res.text}")
|
|
178
187
|
raise OldapError(res.status_code, res.text)
|
|
179
188
|
res = QueryProcessor(context=context, query_result=jsonobj)
|
|
180
189
|
|
|
181
190
|
self._userdata = UserData.from_query(res)
|
|
182
191
|
if not self._userdata.isActive:
|
|
192
|
+
logger.error("Connection with wrong credentials")
|
|
183
193
|
raise OldapError("Wrong credentials") # On purpose, we are not providing too much information why the login failed
|
|
184
194
|
if userId != "unknown":
|
|
185
195
|
hashed = str(self._userdata.credentials).encode('utf-8')
|
|
186
196
|
if not bcrypt.checkpw(credentials.encode('utf-8'), hashed):
|
|
197
|
+
logger.error("Connection with wrong credentials")
|
|
187
198
|
raise OldapError("Wrong credentials") # On purpose, we are not providing too much information why the login failed
|
|
188
199
|
|
|
189
200
|
expiration = datetime.now().astimezone() + timedelta(days=1)
|
|
@@ -225,19 +236,21 @@ class Connection(IConnection):
|
|
|
225
236
|
if res.status_code == 200:
|
|
226
237
|
jsonobj = res.json()
|
|
227
238
|
else:
|
|
239
|
+
logger.error(f"Could not connect to triplestore: {res.text}")
|
|
228
240
|
raise OldapError(res.status_code, res.text)
|
|
229
241
|
res = QueryProcessor(context=context, query_result=jsonobj)
|
|
230
242
|
for r in res:
|
|
231
243
|
context[r['sname']] = r['ns']
|
|
244
|
+
logger.info(f'Connection established. User "{str(self._userdata.userId)}".')
|
|
245
|
+
|
|
246
|
+
@staticmethod
|
|
247
|
+
def version(self) -> str:
|
|
248
|
+
return __version__
|
|
232
249
|
|
|
233
250
|
@property
|
|
234
251
|
def jwtkey(self) -> str:
|
|
235
252
|
"""Getter for the JWT token"""
|
|
236
|
-
return
|
|
237
|
-
|
|
238
|
-
@jwtkey.setter
|
|
239
|
-
def wtkey(self, value: str) -> None:
|
|
240
|
-
self.__jwtkey = value
|
|
253
|
+
return Connection.__jwtkey
|
|
241
254
|
|
|
242
255
|
@property
|
|
243
256
|
def server(self) -> str:
|
|
@@ -267,7 +280,9 @@ class Connection(IConnection):
|
|
|
267
280
|
permission to clear the graph.
|
|
268
281
|
:raises OldapError: If the SPARQL update operation fails.
|
|
269
282
|
"""
|
|
283
|
+
logger = get_logger()
|
|
270
284
|
if not self._userdata:
|
|
285
|
+
logger.error("Connection with no permission to clear graph.")
|
|
271
286
|
raise OldapErrorNoPermission("No permission")
|
|
272
287
|
actor = self._userdata
|
|
273
288
|
sysperms = actor.inProject.get(Xsd_QName('oldap:SystemProject'))
|
|
@@ -289,7 +304,9 @@ class Connection(IConnection):
|
|
|
289
304
|
data=data,
|
|
290
305
|
auth=auth)
|
|
291
306
|
if not req.ok:
|
|
307
|
+
logger.error(f'Clearing of graph "{graph_iri}" failed: {req.text}')
|
|
292
308
|
raise OldapError(req.text)
|
|
309
|
+
logger.info(f'Graph "{graph_iri}" cleared.')
|
|
293
310
|
|
|
294
311
|
def clear_repo(self) -> None:
|
|
295
312
|
"""
|
|
@@ -344,6 +361,7 @@ class Connection(IConnection):
|
|
|
344
361
|
# if not is_root:
|
|
345
362
|
# raise OldapErrorNoPermission("No permission")
|
|
346
363
|
|
|
364
|
+
logger = get_logger()
|
|
347
365
|
with open(filename, encoding="utf-8") as f:
|
|
348
366
|
content = f.read()
|
|
349
367
|
ext = Path(filename).suffix
|
|
@@ -390,7 +408,9 @@ class Connection(IConnection):
|
|
|
390
408
|
data=jsondata,
|
|
391
409
|
auth=auth)
|
|
392
410
|
if not req.ok:
|
|
411
|
+
logger.error(f'Upload of file "{filename}" failed: {req.text}')
|
|
393
412
|
raise OldapError(req.text)
|
|
413
|
+
logger.info(f'File "{filename}" uploaded.')
|
|
394
414
|
|
|
395
415
|
def query(self, query: str, format: SparqlResultFormat = SparqlResultFormat.JSON) -> Any:
|
|
396
416
|
"""
|
|
@@ -404,7 +424,9 @@ class Connection(IConnection):
|
|
|
404
424
|
:rtype: Any
|
|
405
425
|
:raises OldapError: Raised if not logged in or if there is an issue with the query execution.
|
|
406
426
|
"""
|
|
427
|
+
logger = get_logger()
|
|
407
428
|
if not self._userdata:
|
|
429
|
+
logger.error("Not a valid user session.")
|
|
408
430
|
raise OldapError("No login")
|
|
409
431
|
headers = {
|
|
410
432
|
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
@@ -421,6 +443,7 @@ class Connection(IConnection):
|
|
|
421
443
|
if res.status_code == 200:
|
|
422
444
|
return Connection._switcher[format](res)
|
|
423
445
|
else:
|
|
446
|
+
logger.error(f"SPARQL query failed: {res.text}")
|
|
424
447
|
raise OldapError(res.text)
|
|
425
448
|
|
|
426
449
|
def update_query(self, query: str) -> Dict[str,str]:
|
|
@@ -436,7 +459,9 @@ class Connection(IConnection):
|
|
|
436
459
|
:rtype: Dict[str, str
|
|
437
460
|
:raises OldapError: If user authentication is missing or the SPARQL UPDATE execution fails.
|
|
438
461
|
"""
|
|
462
|
+
logger = get_logger()
|
|
439
463
|
if not self._userdata:
|
|
464
|
+
logger.error("Not a valid user session.")
|
|
440
465
|
raise OldapError("No login")
|
|
441
466
|
headers = {
|
|
442
467
|
"Accept": "*/*"
|
|
@@ -445,6 +470,7 @@ class Connection(IConnection):
|
|
|
445
470
|
auth = HTTPBasicAuth(self._dbuser, self._dbpassword) if self._dbuser and self._dbpassword else None
|
|
446
471
|
res = requests.post(url, data={"update": query}, headers=headers, auth=auth)
|
|
447
472
|
if not res.ok:
|
|
473
|
+
logger.error(f"SPARQL update query failed: {res.text}")
|
|
448
474
|
raise OldapError(f'Update query failed. Reason: "{res.text}"')
|
|
449
475
|
|
|
450
476
|
def transaction_start(self) -> None:
|
oldaplib/src/version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.8"
|
oldaplib/test/test_connection.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
1
3
|
import unittest
|
|
2
4
|
from datetime import datetime, timezone
|
|
3
5
|
from pathlib import Path
|
|
@@ -11,6 +13,7 @@ from oldaplib.src.enums.sparql_result_format import SparqlResultFormat
|
|
|
11
13
|
from oldaplib.src.helpers.context import Context
|
|
12
14
|
from oldaplib.src.dtypes.bnode import BNode
|
|
13
15
|
from oldaplib.src.dtypes.namespaceiri import NamespaceIRI
|
|
16
|
+
from oldaplib.src.helpers.serializer import serializer
|
|
14
17
|
from oldaplib.src.xsd.iri import Iri
|
|
15
18
|
from oldaplib.src.xsd.xsd_anyuri import Xsd_anyURI
|
|
16
19
|
from oldaplib.src.xsd.xsd_qname import Xsd_QName
|
|
@@ -79,7 +82,7 @@ class TestBasicConnection(unittest.TestCase):
|
|
|
79
82
|
self.assertEqual(con.server, 'http://localhost:7200')
|
|
80
83
|
self.assertEqual(con.repo, 'oldap')
|
|
81
84
|
self.assertEqual(con.context_name, 'DEFAULT')
|
|
82
|
-
payload = jwt.decode(jwt=con.token, key=con.
|
|
85
|
+
payload = jwt.decode(jwt=con.token, key=con.jwtkey, algorithms="HS256")
|
|
83
86
|
self.assertEqual(payload['iss'], 'http://oldap.org')
|
|
84
87
|
|
|
85
88
|
|
|
@@ -113,11 +116,15 @@ class TestBasicConnection(unittest.TestCase):
|
|
|
113
116
|
|
|
114
117
|
#@unittest.skip('No longer used')
|
|
115
118
|
def test_token(self):
|
|
116
|
-
|
|
119
|
+
os.environ["OLDAP_JWT_SECRET"] = "This is a very special secret, yeah!"
|
|
117
120
|
con = Connection(userId="rosenth",
|
|
118
121
|
credentials="RioGrande",
|
|
119
122
|
context_name="DEFAULT")
|
|
120
123
|
token = con.token
|
|
124
|
+
tokendata = jwt.decode(jwt=token, key=con.jwtkey, algorithms="HS256")
|
|
125
|
+
userdata = json.loads(tokendata['userdata'], object_hook=serializer.decoder_hook)
|
|
126
|
+
self.assertEqual(userdata.userId, "rosenth")
|
|
127
|
+
|
|
121
128
|
con = Connection(token=token,
|
|
122
129
|
context_name="DEFAULT")
|
|
123
130
|
self.assertEqual(con.userid, Xsd_NCName("rosenth"))
|
|
@@ -7,7 +7,7 @@ oldaplib/ontologies/oldap.ttl,sha256=w4q5SX6UIeuYXI2nxNSxzsjZKfR5-mDoLaJXyODBUps
|
|
|
7
7
|
oldaplib/ontologies/shared.trig,sha256=Am3_Nrp1j6fr3W0CEns-RczBRX96mQD0T1GgSjdmvDY,17233
|
|
8
8
|
oldaplib/src/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
9
|
oldaplib/src/cachesingleton.py,sha256=RzQz5-rSAuqgF0TbkdHPwdOjoGxpHFqO8Fpips4AK54,3550
|
|
10
|
-
oldaplib/src/connection.py,sha256=
|
|
10
|
+
oldaplib/src/connection.py,sha256=pWYik6G6LZucG_kTiaJmSZFH39pGJKuXTyEfBgMkWj8,29012
|
|
11
11
|
oldaplib/src/datamodel.py,sha256=7jhqiFYT9fkn6cNTCn5Q7QfxSy29QyMrcVferPZLQrE,30309
|
|
12
12
|
oldaplib/src/dtypes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
13
|
oldaplib/src/dtypes/bnode.py,sha256=aPdm6FpqqFXCAtZJtOniLwIUFRcxK4XmwpCcg3PEnGc,1554
|
|
@@ -66,6 +66,7 @@ oldaplib/src/propertyclass.py,sha256=sOxK1S5SEGOoUqSKrV4hxWAluNMagnTnPFoo5JuuWEQ
|
|
|
66
66
|
oldaplib/src/resourceclass.py,sha256=DSuLMmgWutLUCbC1NuoBVwLHalDakeipMP3-NmnWx_g,98981
|
|
67
67
|
oldaplib/src/user.py,sha256=Z4GXPRkaHXx3glUpPXQdFqYMxQPOuqayDwkTAE5RGjU,48820
|
|
68
68
|
oldaplib/src/userdataclass.py,sha256=FbZkcRt0pKbOeqsZ7HbpwoKE-XPWH2AqpHG1GcsrBPo,12364
|
|
69
|
+
oldaplib/src/version.py,sha256=o1__UKPJBP04AwK1eLp_8Kn-DBuf_dIX5-C7GgRlWNo,21
|
|
69
70
|
oldaplib/src/xsd/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
70
71
|
oldaplib/src/xsd/floatingpoint.py,sha256=rDReKqh0mXyc4F5wslgTUxbeGf3-PGERyughj5_62YI,8852
|
|
71
72
|
oldaplib/src/xsd/iri.py,sha256=tXOuDASdQ6k9KKp2wTO8H4K8yb-_o8jFPanua7wCHog,8269
|
|
@@ -112,7 +113,7 @@ oldaplib/src/xsd/xsd_unsignedlong.py,sha256=5fGH8_SAV1x6b0YGiIR44ZlJXjUiCUW6XHhC
|
|
|
112
113
|
oldaplib/src/xsd/xsd_unsignedshort.py,sha256=M1E8SDR33rPJ0jUpRkzcDCcJYg17YfVbaItTuJeEibc,1013
|
|
113
114
|
oldaplib/test/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
114
115
|
oldaplib/test/test_cache.py,sha256=6TGukPUruuj5BsZaPGZdIx9L7v39qB9jIKZkg_GkbW4,702
|
|
115
|
-
oldaplib/test/test_connection.py,sha256=
|
|
116
|
+
oldaplib/test/test_connection.py,sha256=TM9f17G5RCa2B5gGWVbVhoLL0CUW1JHoM12Brz4P73U,13390
|
|
116
117
|
oldaplib/test/test_context.py,sha256=cJJWzSiWpKq9UFhbScru_PdOAqaCWKB3f_lRzxcCm10,4939
|
|
117
118
|
oldaplib/test/test_datamodel.py,sha256=6hE50zzh6tGb2PKQ4sFk-C829OM5qWOdsJKR107dhgw,54377
|
|
118
119
|
oldaplib/test/test_dtypes.py,sha256=UVdlHNdJrJ5iujMWlwk9tOhRzouJYA8x0E-eLDXPmUo,8583
|
|
@@ -151,7 +152,6 @@ oldaplib/testdata/source_type.yaml,sha256=dSihKikw3O-IlGf6anj5KWMoBYLaweLVF1Zojm
|
|
|
151
152
|
oldaplib/testdata/test_move_left_of_toL.yaml,sha256=2m1OSQrQFlsCQxeJrjzBAO74LMprNDo_HuyrYGsOeXI,787
|
|
152
153
|
oldaplib/testdata/testlist.yaml,sha256=AT11nXEG81Sfyb-tr1gQV0H_dZBrOCcFuHf7YtL8P2g,1994
|
|
153
154
|
oldaplib/testit.http,sha256=qW7mnr6aNLXFG6lQdLgyhXILOPN6qc5iFVZclLyVvkY,303
|
|
154
|
-
oldaplib/
|
|
155
|
-
oldaplib-0.2.
|
|
156
|
-
oldaplib-0.2.
|
|
157
|
-
oldaplib-0.2.6.dist-info/RECORD,,
|
|
155
|
+
oldaplib-0.2.8.dist-info/METADATA,sha256=wNnchGmZcWFjEpxgCxWQtyemD8ESxLlfOx8aFvgCPQA,2803
|
|
156
|
+
oldaplib-0.2.8.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
157
|
+
oldaplib-0.2.8.dist-info/RECORD,,
|
oldaplib/version.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.6"
|
|
File without changes
|