Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 77e6a60

Browse files
committed
Clean up username/password closures
1 parent caa241e commit 77e6a60

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

skpy/conn.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def __init__(self):
156156
self.tokens = {}
157157
self.tokenExpiry = {}
158158
self.tokenFile = None
159+
self.hasUserPwd = False
159160
self.msgsHost = self.API_MSGSHOST
160161
self.sess = requests.Session()
161162
self.endpoints = {"self": SkypeEndpoint(self, "SELF")}
@@ -170,6 +171,23 @@ def connected(self):
170171
def guest(self):
171172
return self.userId.startswith("guest:") if self.userId else None
172173

174+
def closure(self, method, *args, **kwargs):
175+
"""
176+
Create a generic closure to call a method with fixed arguments.
177+
178+
Args:
179+
method (MethodType): bound method of the class
180+
args (list): positional arguments for the method
181+
kwargs (dict): keyword arguments for the method
182+
183+
Returns:
184+
MethodType: bound method closure
185+
"""
186+
@functools.wraps(method)
187+
def inner(self):
188+
return method(*args, **kwargs)
189+
return MethodType(inner, self)
190+
173191
def __call__(self, method, url, codes=(200, 201, 202, 204, 207), auth=None, headers=None, **kwargs):
174192
"""
175193
Make an API call. Most parameters are passed directly to :mod:`requests`.
@@ -253,19 +271,6 @@ def syncStateCall(self, method, url, params={}, **kwargs):
253271
states.append(state)
254272
return resp
255273

256-
def setUserPwd(self, user, pwd):
257-
"""
258-
Replace the stub :meth:`getSkypeToken` method with one that connects via the Microsoft account flow using the
259-
given credentials. Avoids storing the account password in an accessible way.
260-
261-
Args:
262-
user (str): username or email address of the connecting account
263-
pwd (str): password of the connecting account
264-
"""
265-
def getSkypeToken(self):
266-
self.soapLogin(user, pwd)
267-
self.getSkypeToken = MethodType(getSkypeToken, self)
268-
269274
def setTokenFile(self, path):
270275
"""
271276
Enable reading and writing session tokens to a file at the given location.
@@ -345,6 +350,25 @@ def verifyToken(self, auth):
345350
if "reg" not in self.tokenExpiry or datetime.now() >= self.tokenExpiry["reg"]:
346351
self.getRegToken()
347352

353+
def skypeTokenClosure(self, method, *args, **kwargs):
354+
"""
355+
Replace the stub :meth:`getSkypeToken` method with one that connects using the given credentials. Avoids
356+
storing the account password in an accessible way.
357+
"""
358+
self.getSkypeToken = self.closure(method, *args, **kwargs)
359+
self.hasUserPwd = True
360+
361+
def setUserPwd(self, user, pwd):
362+
"""
363+
Replace the stub :meth:`getSkypeToken` method with one that connects via SOAP login using the given
364+
credentials. Avoids storing the account password in an accessible way.
365+
366+
Args:
367+
user (str): username or email address of the connecting account
368+
pwd (str): password of the connecting account
369+
"""
370+
self.skypeTokenClosure(self.soapLogin, user, pwd)
371+
348372
def liveLogin(self, user, pwd):
349373
"""
350374
Obtain connection parameters from the Microsoft account login page, and perform a login with the given email
@@ -365,6 +389,8 @@ def liveLogin(self, user, pwd):
365389
.SkypeAuthException: if the login request is rejected
366390
.SkypeApiException: if the login form can't be processed
367391
"""
392+
if not self.hasUserPwd:
393+
self.skypeTokenClosure(self.liveLogin, user, pwd)
368394
self.tokens["skype"], self.tokenExpiry["skype"] = SkypeLiveAuthProvider(self).auth(user, pwd)
369395
self.getUserId()
370396
self.getRegToken()
@@ -390,6 +416,8 @@ def soapLogin(self, user, pwd):
390416
.SkypeAuthException: if the login request is rejected
391417
.SkypeApiException: if the login form can't be processed
392418
"""
419+
if not self.hasUserPwd:
420+
self.skypeTokenClosure(self.soapLogin, user, pwd)
393421
self.tokens["skype"], self.tokenExpiry["skype"] = SkypeSOAPAuthProvider(self).auth(user, pwd)
394422
self.getUserId()
395423
self.getRegToken()

0 commit comments

Comments
 (0)