jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/584269 )
Change subject: [bugfix] Fix endless loop when badtoken ......................................................................
[bugfix] Fix endless loop when badtoken
Don't continue with the old login request if badtoken calls new login using _relogin(). Raise badtoken properly and ask for new token instead.
Also reduce code duplication by creating Request._relogin().
Bug: T248768 Change-Id: I7ec86b94af88dbaa9e720c6e396abba5da507c6d --- M pywikibot/data/api.py 1 file changed, 13 insertions(+), 14 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py index 071932a..9ca7b81 100644 --- a/pywikibot/data/api.py +++ b/pywikibot/data/api.py @@ -1781,6 +1781,12 @@ self.wait() return None
+ def _relogin(self, message=''): + """Force re-login and inform user.""" + pywikibot.error('{}{}Forcing re-login.'.format(message, + ' ' if message else '')) + self.site._relogin() + def _userinfo_query(self, result): """Handle userinfo query.""" if self.action == 'query' and 'userinfo' in result.get('query', ()): @@ -1791,11 +1797,9 @@ if (self.site.user() is not None and self.site.user() != username and self.site._loginstatus != pywikibot.site.LoginStatus.IN_PROGRESS): - pywikibot.error( - "Logged in as '{actual}' instead of '{expected}'.\n" - 'Forcing re-login.'.format(actual=username, - expected=self.site.user())) - self.site._relogin() + message = ("Logged in as '{actual}' instead of '{expected}'." + .format(actual=username, expected=self.site.user())) + self._relogin(message) return True return False
@@ -1836,11 +1840,6 @@ elif code == 'assertuserfailed': message = 'User assertion failed.'
- # If incorrect login token was used, we are logged out too. - elif (self.site._loginstatus == pywikibot.site.LoginStatus.IN_PROGRESS - and code == 'badtoken'): - message = 'Received incorrect login token.' - # Lastly, the purge module requires a POST if used as anonymous user, # but we normally send a GET request. If the API tells us the request # has to be POSTed, we're probably logged out. @@ -1850,8 +1849,7 @@ else: return True
- pywikibot.error(message + ' Forcing re-login.') - self.site._relogin() + self._relogin(message) return False
def _internal_api_error(self, code, error, result): @@ -1900,7 +1898,8 @@
def _bad_token(self, code): """Check for bad token.""" - if code != 'badtoken': + if (code != 'badtoken' or self.site._loginstatus + == pywikibot.site.LoginStatus.IN_PROGRESS): return False
user_tokens = self.site.tokens._tokens[self.site.user()] @@ -3165,7 +3164,7 @@ fail_reason = response.get(self.keyword('reason'), '') if status == self.keyword('success'): return '' - elif status in ('NeedToken', 'WrongToken'): + elif status in ('NeedToken', 'WrongToken', 'badtoken'): token = response.get('token') if token and below_mw_1_27: # fetched token using action=login