Skip to content

Commit db7686c

Browse files
authored
Merge pull request #26 from GDATASoftwareAG/add-oauth-in-python
add connect_with_client_credentials
2 parents 2e407ea + 9b14c2e commit db7686c

6 files changed

Lines changed: 26 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ java/.project
3939
java/.settings
4040
java/bin
4141
lib/
42+
*.crt
4243

4344
bin/
4445
obj/

python/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ websockets~=10.3
44
python-dotenv==0.20.0
55
httpx[http2]==0.22.0
66
build==0.7.0
7-
jwt==1.3.1
7+
jwt==1.3.1
8+
authlib==1.0.1

python/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ install_requires =
2020
websockets == 10.3
2121
httpx[http2] == 0.22.0
2222
jwt == 1.3.1
23+
authlib == 1.0.1
2324

2425
[options.packages.find]
2526
where = src

python/src/vaas/vaas.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
from jwt import JWT
1111
import httpx
1212
import websockets.client
13+
from authlib.integrations.httpx_client import AsyncOAuth2Client
14+
1315

1416
URL = "wss://gateway-vaas.gdatasecurity.de"
1517

18+
1619
class VaasTracing:
1720
"""Tracing interface for Vaas"""
1821

@@ -37,7 +40,7 @@ def __init__(self, tracing=VaasTracing()):
3740
async def connect(self, token, url=URL):
3841
"""Connect to VaaS
3942
40-
token -- a OpenID Connect token signed by a trusted identity provider
43+
token -- OpenID Connect token signed by a trusted identity provider
4144
"""
4245
self.websocket = await websockets.client.connect(url)
4346
authenticate_request = {"kind": "AuthRequest", "token": token}
@@ -53,6 +56,22 @@ async def connect(self, token, url=URL):
5356
self.__receive_loop()
5457
) # fire and forget async_foo()
5558

59+
async def connect_with_client_credentials(
60+
self, client_id, client_secret, token_endpoint, url=URL, verify=True
61+
):
62+
"""Connect to VaaS with client credentials grant
63+
64+
:param str client_id: Client ID provided by G DATA
65+
:param str client_secret: Client secret provided by G DATA
66+
:param str token_endpoint: Token endpoint of identity provider
67+
:param str url: Websocket endpoint for verdict requests
68+
:param bool verify: This switch turns off SSL validation when set to False; default: True
69+
70+
"""
71+
async with AsyncOAuth2Client(client_id, client_secret, verify=verify) as client:
72+
token = (await client.fetch_token(token_endpoint))["access_token"]
73+
await self.connect(token, url)
74+
5675
async def close(self):
5776
"""Close the connection"""
5877
if self.websocket is not None:

python/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
import xmlrunner
33

4-
from tests.test_vaas import VaasTest # pylint: disable=unused-import
4+
from tests.test_vaas import VaasTest # pylint: disable=unused-import
55

66
if __name__ == "__main__":
77
unittest.main(testRunner=xmlrunner.XMLTestRunner(output="test-reports"))

python/tests/test_vaas.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ async def test_for_buffer_traces(self):
7373
tracing.trace_hash_request.assert_called_with(ANY)
7474
tracing.trace_upload_request.assert_called_with(ANY, 1024)
7575

76+
7677
if __name__ == "__main__":
7778
unittest.main()

0 commit comments

Comments
 (0)