1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-06-20 14:36:52 +02:00

[Niconico] Support 2FA (#3559)

Authored by: ekangmonyet
This commit is contained in:
ekangmonyet 2022-04-28 00:44:29 +08:00 committed by GitHub
parent c171445431
commit 83bfb5e290
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,8 +7,6 @@
from .common import InfoExtractor, SearchInfoExtractor from .common import InfoExtractor, SearchInfoExtractor
from ..compat import ( from ..compat import (
compat_parse_qs,
compat_urllib_parse_urlparse,
compat_HTTPError, compat_HTTPError,
) )
from ..utils import ( from ..utils import (
@ -32,6 +30,7 @@
update_url_query, update_url_query,
url_or_none, url_or_none,
urlencode_postdata, urlencode_postdata,
urljoin,
) )
@ -192,7 +191,7 @@ def _perform_login(self, username, password):
self._request_webpage( self._request_webpage(
'https://account.nicovideo.jp/login', None, 'https://account.nicovideo.jp/login', None,
note='Acquiring Login session') note='Acquiring Login session')
urlh = self._request_webpage( page = self._download_webpage(
'https://account.nicovideo.jp/login/redirector?show_button_twitter=1&site=niconico&show_button_facebook=1', None, 'https://account.nicovideo.jp/login/redirector?show_button_twitter=1&site=niconico&show_button_facebook=1', None,
note='Logging in', errnote='Unable to log in', note='Logging in', errnote='Unable to log in',
data=urlencode_postdata(login_form_strs), data=urlencode_postdata(login_form_strs),
@ -200,14 +199,27 @@ def _perform_login(self, username, password):
'Referer': 'https://account.nicovideo.jp/login', 'Referer': 'https://account.nicovideo.jp/login',
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
}) })
if urlh is False: if 'oneTimePw' in page:
login_ok = False post_url = self._search_regex(
else: r'<form[^>]+action=(["\'])(?P<url>.+?)\1', page, 'post url', group='url')
parts = compat_urllib_parse_urlparse(urlh.geturl()) page = self._download_webpage(
if compat_parse_qs(parts.query).get('message', [None])[0] == 'cant_login': urljoin('https://account.nicovideo.jp', post_url), None,
login_ok = False note='Performing MFA', errnote='Unable to complete MFA',
data=urlencode_postdata({
'otp': self._get_tfa_info('6 digits code')
}), headers={
'Content-Type': 'application/x-www-form-urlencoded',
})
if 'oneTimePw' in page or 'formError' in page:
err_msg = self._html_search_regex(
r'formError["\']+>(.*?)</div>', page, 'form_error',
default='There\'s an error but the message can\'t be parsed.',
flags=re.DOTALL)
self.report_warning(f'Unable to log in: MFA challenge failed, "{err_msg}"')
return False
login_ok = 'class="notice error"' not in page
if not login_ok: if not login_ok:
self.report_warning('unable to log in: bad username or password') self.report_warning('Unable to log in: bad username or password')
return login_ok return login_ok
def _get_heartbeat_info(self, info_dict): def _get_heartbeat_info(self, info_dict):