Added 2FA for kate's tokens, finally. Closes #13

This commit is contained in:
Manuel Cortez 2018-12-25 18:55:50 -06:00
parent f191ed42da
commit 1cff350fed
4 changed files with 10 additions and 20 deletions

View File

@ -2,7 +2,7 @@
## changes in this version ## changes in this version
* Added support for Two factor authentication (2FA). When detecting 2FA in an user account, Socializer will switch automatically to alternative tokens. There is some work to authorize kate's tokens with 2FA, but currently it doesn't work fully. ([#13,](https://code.manuelcortez.net/manuelcortez/socializer/issues/13)) * Added support for Two factor authentication (2FA). ([#13,](https://code.manuelcortez.net/manuelcortez/socializer/issues/13))
* Now it is possible to send voice messages from socializer. Voice messages are available from the "add" button in any conversation buffer. * Now it is possible to send voice messages from socializer. Voice messages are available from the "add" button in any conversation buffer.
* tokens generated by socializer will never expire. ([#3,](https://code.manuelcortez.net/manuelcortez/socializer/issues/3)) * tokens generated by socializer will never expire. ([#3,](https://code.manuelcortez.net/manuelcortez/socializer/issues/3))
* In order to use all methods available in VK, socializer will use tokens of kate mobile for Android. It means you may receive an email by saying that you've authorised Kate for accessing your account from an Android device. * In order to use all methods available in VK, socializer will use tokens of kate mobile for Android. It means you may receive an email by saying that you've authorised Kate for accessing your account from an Android device.

View File

@ -15,7 +15,7 @@ class C2DMError(Exception):
client_id = '2685278' client_id = '2685278'
client_secret = 'lxhD8OD7dMsqtXIm5IUY' client_secret = 'lxhD8OD7dMsqtXIm5IUY'
api_ver='5.70' api_ver='5.92'
scope = 'all' scope = 'all'
user_agent = 'KateMobileAndroid/47-427 (Android 6.0.1; SDK 23; armeabi-v7a; samsung SM-G900F; ru)' user_agent = 'KateMobileAndroid/47-427 (Android 6.0.1; SDK 23; armeabi-v7a; samsung SM-G900F; ru)'
android_id = '4119748609680577006' android_id = '4119748609680577006'
@ -25,27 +25,24 @@ api_url = 'https://api.vk.com/method/'
def requestAuth(login, password, scope=scope): def requestAuth(login, password, scope=scope):
if not (login or password): if not (login or password):
raise ValueError raise ValueError
url = 'https://oauth.vk.com/token?grant_type=password&2fa_supported=1&client_id='+client_id+'&client_secret='+client_secret+'&username='+login+'&password='+password+'&v='+api_ver+'&scope='+scope url = 'https://oauth.vk.com/token?grant_type=password&2fa_supported=1&force_sms=1&client_id='+client_id+'&client_secret='+client_secret+'&username='+login+'&password='+password+'&v='+api_ver+'&scope='+scope
headers = { headers = {
'User-Agent': user_agent 'User-Agent': user_agent
} }
r = requests.get(url, headers=headers) r = requests.get(url, headers=headers)
# If a 401 error is raised, we need to use 2FA here.
# see https://vk.com/dev/auth_direct (switch lang to russian, english docs are very incomplete in the matter)
if r.status_code == 401 and "phone_mask" in r.text:
t = r.json()
code, remember = two_factor_auth()
url = 'https://oauth.vk.com/token?grant_type=password&client_id='+client_id+'&client_secret='+client_secret+'&username='+login+'&password='+password+'&v='+api_ver+'&scope='+scope+'&code='+code
r = requests.get(url, headers=headers)
if r.status_code == 200 and 'access_token' in r.text: if r.status_code == 200 and 'access_token' in r.text:
res = r.json() res = r.json()
access_token = res['access_token'] access_token = res['access_token']
user_id = str(res['user_id']) user_id = str(res['user_id'])
return access_token, user_id return access_token, user_id
else: else:
# Two factor auth is not supported in this method as it returns invalid code all the time.
# t = r.json()
# print t
# q = requests.get(t["redirect_uri"], headers=headers)
# print q.text
# code, remember = two_factor_auth()
# url = 'https://oauth.vk.com/token?grant_type=password&client_id='+client_id+'&client_secret='+client_secret+'&username='+login+'&password='+password+'&v='+api_ver+'&scope='+scope+'&code='+code+'&remember_device='+str(int(remember))
# print url
# r = requests.get(url, headers=headers)
# print r.text
raise AuthenticationError(r.text) raise AuthenticationError(r.text)
def getReceipt(user_id): def getReceipt(user_id):

View File

@ -42,9 +42,6 @@ class sessionManagerController(object):
if not os.path.exists(path): if not os.path.exists(path):
os.mkdir(path) os.mkdir(path)
s.get_configuration() s.get_configuration()
if view.two_factor_question() == widgetUtils.YES:
s.settings["vk"]["use_alternative_tokens"] = True
s.settings.write()
self.get_authorisation(s) self.get_authorisation(s)
session.sessions[location] = s session.sessions[location] = s
else: else:

View File

@ -23,12 +23,8 @@ def get_code():
if response == widgetUtils.OK: if response == widgetUtils.OK:
code = dlg.GetValue() code = dlg.GetValue()
dlg.Destroy() dlg.Destroy()
dlg.Destroy() dlg.Destroy()
def two_factor_question():
return wx.MessageDialog(None, _(u"Do you have two factor authentication enabled in your account?"), _(u"Authentication method"), wx.YES_NO).ShowModal()
class newSessionDialog(widgetUtils.BaseDialog): class newSessionDialog(widgetUtils.BaseDialog):
def __init__(self): def __init__(self):
super(newSessionDialog, self).__init__(parent=None, id=wx.NewId(), title=_(u"Authorise VK")) super(newSessionDialog, self).__init__(parent=None, id=wx.NewId(), title=_(u"Authorise VK"))