1- from yowsup .layers import YowLayer , YowLayerEvent , YowProtocolLayer
1+ from yowsup .layers import YowLayerEvent , YowProtocolLayer
22from .keystream import KeyStream
33from yowsup .common .tools import TimeTools
44from .layer_crypt import YowCryptLayer
55from yowsup .layers .network import YowNetworkLayer
66from .autherror import AuthError
77from .protocolentities import *
88from yowsup .common .tools import StorageTools
9+ from .layer_interface_authentication import YowAuthenticationProtocolLayerInterface
910import base64
1011class YowAuthenticationProtocolLayer (YowProtocolLayer ):
1112 EVENT_LOGIN = "org.openwhatsapp.yowsup.event.auth.login"
@@ -22,29 +23,38 @@ def __init__(self):
2223 "stream:error" : (self .handleStreamError , None ),
2324 }
2425 super (YowAuthenticationProtocolLayer , self ).__init__ (handleMap )
25- self .credentials = None
26+ self .interface = YowAuthenticationProtocolLayerInterface (self )
27+ self .credentials = None #left for backwards-compat
28+ self ._credentials = None #new style set
2629
2730 def __str__ (self ):
2831 return "Authentication Layer"
2932
30- def __getCredentials (self ):
31- u , pb64 = self .getProp (YowAuthenticationProtocolLayer .PROP_CREDENTIALS )
33+ def __getCredentials (self , credentials = None ):
34+ u , pb64 = credentials or self .getProp (YowAuthenticationProtocolLayer .PROP_CREDENTIALS )
3235 if type (pb64 ) is str :
3336 pb64 = pb64 .encode ()
3437 password = base64 .b64decode (pb64 )
3538 return (u , bytearray (password ))
3639
40+ def setCredentials (self , credentials ):
41+ self .setProp (YowAuthenticationProtocolLayer .PROP_CREDENTIALS , credentials ) #keep for now
42+ self ._credentials = self .__getCredentials (credentials )
43+
44+ def getUsername (self , full = False ):
45+ if self ._credentials :
46+ return self ._credentials [0 ] if not full else ("%s@s.whatsapp.net" % self ._credentials [0 ])
47+ else :
48+ prop = self .getProp (YowAuthenticationProtocolLayer .PROP_CREDENTIALS )
49+ return prop [0 ] if prop else None
50+
3751 def onEvent (self , event ):
3852 if event .getName () == YowNetworkLayer .EVENT_STATE_CONNECTED :
3953 self .login ()
40- elif event .getName () == YowNetworkLayer .EVENT_STATE_CONNECT :
41- self .credentials = self .__getCredentials ()
42- if not self .credentials :
43- raise AuthError ("Auth stopped connection signal as no credentials have been set" )
4454
4555 ## general methods
4656 def login (self ):
47-
57+ self . credentials = self . _credentials or self . __getCredentials ()
4858 self ._sendFeatures ()
4959 self ._sendAuth ()
5060
@@ -102,7 +112,7 @@ def _sendResponse(self,nonce):
102112 responseEntity = ResponseProtocolEntity (authBlob )
103113
104114 #to prevent enr whole response
105- self .broadcastEvent (YowLayerEvent (YowCryptLayer .EVENT_KEYS_READY , keys = (inputKey , None )))
115+ self .broadcastEvent (YowLayerEvent (YowCryptLayer .EVENT_KEYS_READY , keys = (inputKey , None )))
106116 self .entityToLower (responseEntity )
107117 self .broadcastEvent (YowLayerEvent (YowCryptLayer .EVENT_KEYS_READY , keys = (inputKey , outputKey )))
108118 #YowCryptLayer.setProp("outputKey", outputKey)
0 commit comments