diff --git a/changelog.md b/changelog.md index af822e2..b46805e 100644 --- a/changelog.md +++ b/changelog.md @@ -2,7 +2,7 @@ ## 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. * 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. diff --git a/src/sessionmanager/core.py b/src/sessionmanager/core.py index c29e652..b50b6f8 100644 --- a/src/sessionmanager/core.py +++ b/src/sessionmanager/core.py @@ -15,7 +15,7 @@ class C2DMError(Exception): client_id = '2685278' client_secret = 'lxhD8OD7dMsqtXIm5IUY' -api_ver='5.70' +api_ver='5.92' scope = 'all' user_agent = 'KateMobileAndroid/47-427 (Android 6.0.1; SDK 23; armeabi-v7a; samsung SM-G900F; ru)' android_id = '4119748609680577006' @@ -25,27 +25,24 @@ api_url = 'https://api.vk.com/method/' def requestAuth(login, password, scope=scope): if not (login or password): 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 = { 'User-Agent': user_agent } 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: res = r.json() access_token = res['access_token'] user_id = str(res['user_id']) return access_token, user_id 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) def getReceipt(user_id): diff --git a/src/sessionmanager/sessionManager.py b/src/sessionmanager/sessionManager.py index 9e1b550..f2765f7 100644 --- a/src/sessionmanager/sessionManager.py +++ b/src/sessionmanager/sessionManager.py @@ -42,9 +42,6 @@ class sessionManagerController(object): if not os.path.exists(path): os.mkdir(path) s.get_configuration() - if view.two_factor_question() == widgetUtils.YES: - s.settings["vk"]["use_alternative_tokens"] = True - s.settings.write() self.get_authorisation(s) session.sessions[location] = s else: diff --git a/src/sessionmanager/wxUI.py b/src/sessionmanager/wxUI.py index 009c887..667b093 100644 --- a/src/sessionmanager/wxUI.py +++ b/src/sessionmanager/wxUI.py @@ -23,12 +23,8 @@ def get_code(): if response == widgetUtils.OK: code = dlg.GetValue() 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): def __init__(self): super(newSessionDialog, self).__init__(parent=None, id=wx.NewId(), title=_(u"Authorise VK"))