summaryrefslogtreecommitdiff
path: root/wrappers/python
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2023-03-29 11:58:46 +0200
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2023-04-18 14:05:19 +0200
commit537a09d4334f1555b80d87b7d935328963a21739 (patch)
tree071eeb52a4ed79a1e1b49831c80d6a2336cee7cf /wrappers/python
parent61871cb9ea7605e5350e9612edf8c9d603da2883 (diff)
Client + Server: Implement a token updater callback
Diffstat (limited to 'wrappers/python')
-rw-r--r--wrappers/python/eduvpn_common/loader.py6
-rw-r--r--wrappers/python/eduvpn_common/main.py34
-rw-r--r--wrappers/python/eduvpn_common/server.py14
-rw-r--r--wrappers/python/eduvpn_common/types.py1
4 files changed, 52 insertions, 3 deletions
diff --git a/wrappers/python/eduvpn_common/loader.py b/wrappers/python/eduvpn_common/loader.py
index 036c201..673d180 100644
--- a/wrappers/python/eduvpn_common/loader.py
+++ b/wrappers/python/eduvpn_common/loader.py
@@ -8,6 +8,7 @@ from eduvpn_common.types import (
cToken,
DataError,
ReadRxBytes,
+ UpdateToken,
VPNStateChange,
)
@@ -68,6 +69,7 @@ def initialize_functions(lib: CDLL) -> None:
], c_void_p
lib.Deregister.argtypes, lib.Deregister.restype = [c_char_p], None
lib.FreeConfig.argtypes, lib.FreeConfig.restype = [c_void_p], None
+ lib.FreeTokens.argtypes, lib.FreeTokens.restype = [c_void_p], None
lib.FreeDiscoOrganizations.argtypes, lib.FreeDiscoOrganizations.restype = [
c_void_p
], None
@@ -112,6 +114,10 @@ def initialize_functions(lib: CDLL) -> None:
VPNStateChange,
c_int,
], c_void_p
+ lib.SetTokenUpdater.argtypes, lib.SetTokenUpdater.restype = [
+ c_char_p,
+ UpdateToken,
+ ], c_void_p
lib.RemoveCustomServer.argtypes, lib.RemoveCustomServer.restype = [
c_char_p,
c_char_p,
diff --git a/wrappers/python/eduvpn_common/main.py b/wrappers/python/eduvpn_common/main.py
index 3ca26fe..74ff52d 100644
--- a/wrappers/python/eduvpn_common/main.py
+++ b/wrappers/python/eduvpn_common/main.py
@@ -1,5 +1,5 @@
import threading
-from ctypes import cast, c_void_p, c_int, pointer
+from ctypes import POINTER, cast, c_void_p, c_int, pointer
from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple
from eduvpn_common.discovery import (
@@ -17,6 +17,7 @@ from eduvpn_common.server import (
encode_tokens,
get_config,
Server,
+ get_tokens,
get_transition_server,
get_servers,
)
@@ -24,7 +25,7 @@ from eduvpn_common.state import State, StateType
from eduvpn_common.types import (
VPNStateChange,
ReadRxBytes,
- cToken,
+ UpdateToken,
decode_res,
encode_args,
get_data_error,
@@ -54,6 +55,7 @@ class EduVPN(object):
initialize_functions(self.lib)
self.event_handler = EventHandler(self.lib)
+ self.token_callback = None
# Callbacks that need to wait for specific events
@@ -140,6 +142,16 @@ class EduVPN(object):
if register_err:
raise register_err
+ def set_token_updater(self, updater: Callable):
+ self.token_callback = updater
+ updater_err = self.go_function(
+ self.lib.SetTokenUpdater,
+ token_callback,
+ )
+
+ if updater_err:
+ raise updater_err
+
def get_disco_servers(self) -> Optional[DiscoServers]:
"""Get the discovery servers
@@ -442,6 +454,11 @@ class EduVPN(object):
"""
return self.event.run(old_state, new_state, data)
+ def token_calback(self, srv: Server, tok: Token):
+ if self.token_callback is None:
+ return
+ self.token_callback(srv, tok)
+
def set_profile(self, profile_id: str) -> None:
"""Set the profile of the current server
@@ -585,6 +602,19 @@ class EduVPN(object):
eduvpn_objects: Dict[str, EduVPN] = {}
+@UpdateToken
+def token_callback(name: bytes, srv, tok):
+ name_decoded = name.decode()
+ if name_decoded not in eduvpn_objects:
+ return 0
+ obj = eduvpn_objects[name_decoded]
+ srv_conv = get_transition_server(obj.lib, srv)
+ tok_conv = get_tokens(obj.lib, tok)
+ obj.token_callback(
+ srv_conv, tok_conv
+ )
+
+
@VPNStateChange
def state_callback(name: bytes, old_state: int, new_state: int, data: Any) -> int:
"""The internal callback that is passed to the Go library
diff --git a/wrappers/python/eduvpn_common/server.py b/wrappers/python/eduvpn_common/server.py
index 068dc61..55eadcd 100644
--- a/wrappers/python/eduvpn_common/server.py
+++ b/wrappers/python/eduvpn_common/server.py
@@ -392,6 +392,19 @@ def get_locations(lib: CDLL, ptr: c_void_p) -> Optional[List[str]]:
return None
+def get_tokens(lib: CDLL, ptr: c_void_p) -> Optional[Token]:
+ if ptr:
+ toks = cast(ptr, POINTER(cToken)).contents
+ access = toks.access.decode("utf-8")
+ refresh = toks.refresh.decode("utf-8")
+ expired = toks.expired
+ lib.FreeTokens(ptr)
+ return Token(
+ access, refresh, expired
+ )
+ return None
+
+
def get_config(lib: CDLL, ptr: c_void_p) -> Optional[Config]:
"""Get the config from the Go library as a C structure and return a Python usable structure
@@ -403,7 +416,6 @@ def get_config(lib: CDLL, ptr: c_void_p) -> Optional[Config]:
:return: The configuration if there is any
:rtype: Optional[Config]
"""
- # TODO: FREE
if ptr:
config = cast(ptr, POINTER(cConfig)).contents
cfg = config.config.decode("utf-8")
diff --git a/wrappers/python/eduvpn_common/types.py b/wrappers/python/eduvpn_common/types.py
index 4bc5a85..32a7a00 100644
--- a/wrappers/python/eduvpn_common/types.py
+++ b/wrappers/python/eduvpn_common/types.py
@@ -195,6 +195,7 @@ class DataError(Structure):
# The type for a Go state change callback
VPNStateChange = CFUNCTYPE(c_int, c_char_p, c_int, c_int, c_void_p)
ReadRxBytes = CFUNCTYPE(c_ulonglong)
+UpdateToken = CFUNCTYPE(None, c_char_p, c_void_p, c_void_p)
def encode_args(args: List[Any], types: List[Any]) -> Iterator[Any]: