Skip to content

Commit

Permalink
Merge pull request #26 from GDATASoftwareAG/add-oauth-in-python
Browse files Browse the repository at this point in the history
add connect_with_client_credentials
  • Loading branch information
doxthree authored May 24, 2022
2 parents 2e407ea + 9b14c2e commit db7686c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ java/.project
java/.settings
java/bin
lib/
*.crt

bin/
obj/
Expand Down
3 changes: 2 additions & 1 deletion python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ websockets~=10.3
python-dotenv==0.20.0
httpx[http2]==0.22.0
build==0.7.0
jwt==1.3.1
jwt==1.3.1
authlib==1.0.1
1 change: 1 addition & 0 deletions python/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ install_requires =
websockets == 10.3
httpx[http2] == 0.22.0
jwt == 1.3.1
authlib == 1.0.1

[options.packages.find]
where = src
21 changes: 20 additions & 1 deletion python/src/vaas/vaas.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
from jwt import JWT
import httpx
import websockets.client
from authlib.integrations.httpx_client import AsyncOAuth2Client


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


class VaasTracing:
"""Tracing interface for Vaas"""

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

async def connect_with_client_credentials(
self, client_id, client_secret, token_endpoint, url=URL, verify=True
):
"""Connect to VaaS with client credentials grant
:param str client_id: Client ID provided by G DATA
:param str client_secret: Client secret provided by G DATA
:param str token_endpoint: Token endpoint of identity provider
:param str url: Websocket endpoint for verdict requests
:param bool verify: This switch turns off SSL validation when set to False; default: True
"""
async with AsyncOAuth2Client(client_id, client_secret, verify=verify) as client:
token = (await client.fetch_token(token_endpoint))["access_token"]
await self.connect(token, url)

async def close(self):
"""Close the connection"""
if self.websocket is not None:
Expand Down
2 changes: 1 addition & 1 deletion python/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import xmlrunner

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

if __name__ == "__main__":
unittest.main(testRunner=xmlrunner.XMLTestRunner(output="test-reports"))
1 change: 1 addition & 0 deletions python/tests/test_vaas.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ async def test_for_buffer_traces(self):
tracing.trace_hash_request.assert_called_with(ANY)
tracing.trace_upload_request.assert_called_with(ANY, 1024)


if __name__ == "__main__":
unittest.main()

0 comments on commit db7686c

Please sign in to comment.