@@ -292,22 +292,19 @@ def setTokenFile(self, path):
292292 """
293293 self .tokenFile = path
294294
295- def readToken (self ):
295+ def readTokenFromStr (self , tokens ):
296296 """
297- Attempt to re-establish a connection using previously acquired tokens.
297+ Attempt to re-establish a connection using previously acquired tokens from a string .
298298
299299 If the Skype token is valid but the registration token is invalid, a new endpoint will be registered.
300300
301+ Args:
302+ tokens (str): string containing tokens
303+
301304 Raises:
302- .SkypeAuthException: if the token file cannot be used to authenticate
305+ .SkypeAuthException: if the token string cannot be used to authenticate
303306 """
304- if not self .tokenFile :
305- raise SkypeAuthException ("No token file specified" )
306- try :
307- with open (self .tokenFile , "r" ) as f :
308- lines = f .read ().splitlines ()
309- except OSError :
310- raise SkypeAuthException ("Token file doesn't exist or not readable" )
307+ lines = tokens .splitlines ()
311308 try :
312309 user , skypeToken , skypeExpiry , regToken , regExpiry , msgsHost = lines
313310 skypeExpiry = datetime .fromtimestamp (int (skypeExpiry ))
@@ -326,6 +323,42 @@ def readToken(self):
326323 else :
327324 self .getRegToken ()
328325
326+ def readToken (self ):
327+ """
328+ Attempt to re-establish a connection using previously acquired tokens.
329+
330+ If the Skype token is valid but the registration token is invalid, a new endpoint will be registered.
331+
332+ Raises:
333+ .SkypeAuthException: if the token file cannot be used to authenticate
334+ """
335+ if not self .tokenFile :
336+ raise SkypeAuthException ("No token file specified" )
337+ try :
338+ with open (self .tokenFile , "r" ) as f :
339+ tokens = f .read ()
340+ except OSError :
341+ raise SkypeAuthException ("Token file doesn't exist or not readable" )
342+ self .readTokenFromStr (tokens )
343+
344+ def writeTokenToStr (self ):
345+ """
346+ Return details of the current connection into a string.
347+
348+ This can be used by :meth:`readTokenFromStr` to re-authenticate at a later time.
349+
350+ Returns:
351+ str: A token string that can be used by :meth:`readTokenFromStr` to re-authenticate.
352+ """
353+ return "\n " .join ([
354+ self .userId ,
355+ self .tokens ["skype" ],
356+ str (int (time .mktime (self .tokenExpiry ["skype" ].timetuple ()))),
357+ self .tokens ["reg" ],
358+ str (int (time .mktime (self .tokenExpiry ["reg" ].timetuple ()))),
359+ self .msgsHost
360+ ]) + "\n "
361+
329362 def writeToken (self ):
330363 """
331364 Store details of the current connection in the named file.
@@ -336,12 +369,7 @@ def writeToken(self):
336369 with os .fdopen (os .open (self .tokenFile , os .O_WRONLY | os .O_CREAT , 0o600 ), "w" ) as f :
337370 # When opening files via os, truncation must be done manually.
338371 f .truncate ()
339- f .write (self .userId + "\n " )
340- f .write (self .tokens ["skype" ] + "\n " )
341- f .write (str (int (time .mktime (self .tokenExpiry ["skype" ].timetuple ()))) + "\n " )
342- f .write (self .tokens ["reg" ] + "\n " )
343- f .write (str (int (time .mktime (self .tokenExpiry ["reg" ].timetuple ()))) + "\n " )
344- f .write (self .msgsHost + "\n " )
372+ f .write (self .writeTokenToStr ())
345373
346374 def verifyToken (self , auth ):
347375 """
0 commit comments