beremiz

WAMP: IDE generates its own PSK to connect to server.
py2compat
15 months ago, Edouard Tisserant
e458d4208bf7
Parents 67eead640d01
Children 2e01ad1cd325
WAMP: IDE generates its own PSK to connect to server.
--- a/PSKManagement.py Tue Mar 04 14:19:29 2025 +0100
+++ b/PSKManagement.py Tue Mar 04 17:20:26 2025 +0100
@@ -5,9 +5,13 @@
from __future__ import absolute_import
import os
+from os.path import join, exists
import time
import json
from zipfile import ZipFile
+from binascii import b2a_base64
+
+from util.paths import AppDataPath
# PSK Management Data model :
# [[ID,Desc, LastKnownURI, LastConnect]]
@@ -166,3 +170,25 @@
SaveData(project_path, data)
return data
+
+def GetIDEIdentity():
+ own_keystore = AppDataPath("keystore", "own")
+ if not exists(own_keystore):
+ os.makedirs(own_keystore)
+
+ own_identity = join(own_keystore, "default.psk")
+ if exists(own_identity):
+ ID, _sep, PSK = open(own_identity).read().partition(':')
+ secretstring = PSK.rstrip('\n\r')
+ else:
+ ID = os.urandom(8).hex()
+ # secret string length is 256
+ # b2a_base64 output len is 4/3 input len
+ secret = os.urandom(192) # int(256/1.3333)
+ secretstring = b2a_base64(secret).decode()
+
+ PSKstring = ID+":"+secretstring
+ with open(own_identity, 'w') as f:
+ f.write(PSKstring)
+
+ return ID, secretstring
--- a/connectors/WAMP/__init__.py Tue Mar 04 14:19:29 2025 +0100
+++ b/connectors/WAMP/__init__.py Tue Mar 04 17:20:26 2025 +0100
@@ -53,7 +53,7 @@
class WampSession(wamp.ApplicationSession):
def onConnect(self):
- user = self.config.extra["ID"]
+ user = self.config.extra["IDE_ID"]
self.join(self.config.realm, ["wampcra"], user)
def onChallenge(self, challenge):
@@ -78,7 +78,7 @@
global _WampSession
_WampSession = self
_WampSessionEvent.set()
- print('WAMP session joined for :', self.config.extra["ID"])
+ print('WAMP session joined for :', self.config.extra["IDE_ID"])
def onLeave(self, details):
global _WampSession
@@ -88,17 +88,18 @@
def _WAMP_connector_factory(cls, uri, confnodesroot):
"""
- WAMP://127.0.0.1:12345/path#realm#ID
- WAMPS://127.0.0.1:12345/path#realm#ID
+ WAMP://127.0.0.1:12345/path#realm#PLC_ID
+ WAMPS://127.0.0.1:12345/path#realm#PLC_ID
"""
scheme, location = uri.split("://")
- urlpath, realm, ID = location.split('#')
+ urlpath, realm, PLC_ID = location.split('#')
urlprefix = {"WAMP": "ws",
"WAMPS": "wss"}[scheme]
url = urlprefix+"://"+urlpath
CN = urlpath.split("/")[0].split(":")[0]
try:
- secret = PSK.GetSecret(confnodesroot.ProjectPath, ID)
+
+ IDE_ID, secret = PSK.GetIDEIdentity()
trust_store = Cert.GetCertPath(confnodesroot.ProjectPath, CN)
except Exception as e:
confnodesroot.logger.write_error(
@@ -115,7 +116,7 @@
component_config = types.ComponentConfig(
realm=text(realm),
extra={
- "ID": ID,
+ "IDE_ID": IDE_ID,
"secret": secret
})
session_factory = wamp.ApplicationSessionFactory(
--- a/util/paths.py Tue Mar 04 14:19:29 2025 +0100
+++ b/util/paths.py Tue Mar 04 17:20:26 2025 +0100
@@ -55,3 +55,17 @@
"""
return os.path.join(AbsParentDir(__file__, 2), name, *suffixes)
+def Bpath(*names):
+ """
+ Return path of files in Beremiz project
+ """
+ return os.path.join(AbsParentDir(__file__, 1), *names)
+
+def AppDataPath(*names):
+ """
+ Return path of files in Beremiz project
+ """
+ if os.name == "posix":
+ return os.path.join(os.environ["HOME"], ".local", "share", "beremiz", *names)
+
+ return os.path.join(os.environ["APPDATA"], "Beremiz", *names)