Skip to content

Commit

Permalink
Merge branch RCW679:master (PR #259)
Browse files Browse the repository at this point in the history
  • Loading branch information
Terrance committed May 3, 2024
2 parents 00797b1 + 1a0a1d1 commit 5b843a7
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions skpy/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -326,6 +323,42 @@ def readToken(self):
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.
Returns:
str: A token string that can be used by :meth:`readTokenFromStr` to re-authenticate.
"""
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.
Expand All @@ -336,12 +369,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):
"""
Expand Down

0 comments on commit 5b843a7

Please sign in to comment.