From 179cc737b2c8e13afa2b58f471204785dab0729f Mon Sep 17 00:00:00 2001 From: Dean Sleick Date: Sun, 21 Apr 2024 17:46:23 -0300 Subject: [PATCH 1/2] Update conn.py, adding readTokenFromStr and writeTokenToStr --- skpy/conn.py | 57 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/skpy/conn.py b/skpy/conn.py index 19da8cc..818afb0 100644 --- a/skpy/conn.py +++ b/skpy/conn.py @@ -292,22 +292,19 @@ def setTokenFile(self, path): """ self.tokenFile = path - def readToken(self): + def readTokenFromStr(self, tokens): """ - Attempt to re-establish a connection using previously acquired tokens. + Attempt to re-establish a connection using previously acquired tokens from a string. If the Skype token is valid but the registration token is invalid, a new endpoint will be registered. + Args: + tokens (str): string containing tokens + Raises: - .SkypeAuthException: if the token file cannot be used to authenticate + .SkypeAuthException: if the token string cannot be used to authenticate """ - if not self.tokenFile: - raise SkypeAuthException("No token file specified") - try: - with open(self.tokenFile, "r") as f: - lines = f.read().splitlines() - except OSError: - raise SkypeAuthException("Token file doesn't exist or not readable") + lines = tokens.splitlines() try: user, skypeToken, skypeExpiry, regToken, regExpiry, msgsHost = lines skypeExpiry = datetime.fromtimestamp(int(skypeExpiry)) @@ -325,7 +322,40 @@ def readToken(self): self.msgsHost = msgsHost else: self.getRegToken() + + def readToken(self): + """ + Attempt to re-establish a connection using previously acquired tokens. + + If the Skype token is valid but the registration token is invalid, a new endpoint will be registered. + + Raises: + .SkypeAuthException: if the token file cannot be used to authenticate + """ + if not self.tokenFile: + raise SkypeAuthException("No token file specified") + try: + with open(self.tokenFile, "r") as f: + tokens = f.read() + except OSError: + raise SkypeAuthException("Token file doesn't exist or not readable") + self.readTokenFromStr(tokens) + def writeTokenToStr(self): + """ + Return details of the current connection into a string. + + This can be used by :meth:`readTokenFromStr` to re-authenticate at a later time. + """ + return "\n".join([ + self.userId, + self.tokens["skype"], + str(int(time.mktime(self.tokenExpiry["skype"].timetuple()))), + self.tokens["reg"], + str(int(time.mktime(self.tokenExpiry["reg"].timetuple()))), + self.msgsHost + ]) + "\n" + def writeToken(self): """ Store details of the current connection in the named file. @@ -336,12 +366,7 @@ def writeToken(self): with os.fdopen(os.open(self.tokenFile, os.O_WRONLY | os.O_CREAT, 0o600), "w") as f: # When opening files via os, truncation must be done manually. f.truncate() - f.write(self.userId + "\n") - f.write(self.tokens["skype"] + "\n") - f.write(str(int(time.mktime(self.tokenExpiry["skype"].timetuple()))) + "\n") - f.write(self.tokens["reg"] + "\n") - f.write(str(int(time.mktime(self.tokenExpiry["reg"].timetuple()))) + "\n") - f.write(self.msgsHost + "\n") + f.write(self.writeTokenToStr()) def verifyToken(self, auth): """ From 1a0a1d1ef9803babe9027b02709b8cadb57018b1 Mon Sep 17 00:00:00 2001 From: Dean Sleick Date: Sun, 21 Apr 2024 18:04:40 -0300 Subject: [PATCH 2/2] Update conn.py --- skpy/conn.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/skpy/conn.py b/skpy/conn.py index 818afb0..6ba15fd 100644 --- a/skpy/conn.py +++ b/skpy/conn.py @@ -346,6 +346,9 @@ def writeTokenToStr(self): Return details of the current connection into a string. This can be used by :meth:`readTokenFromStr` to re-authenticate at a later time. + + Returns: + str: A token string that can be used by :meth:`readTokenFromStr` to re-authenticate. """ return "\n".join([ self.userId,