Store prompted passwords in local sessions (#1239)

Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
This commit is contained in:
Sebastian Czech 2021-12-29 10:00:47 +01:00 committed by GitHub
parent 05c02f0f39
commit 17ed3bb8c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 10 deletions

View File

@ -296,6 +296,10 @@ class HTTPieArgumentParser(BaseHTTPieArgumentParser):
' --ignore-stdin is set.' ' --ignore-stdin is set.'
) )
credentials.prompt_password(url.netloc) credentials.prompt_password(url.netloc)
if (credentials.key and credentials.value):
plugin.raw_auth = credentials.key + ":" + credentials.value
self.args.auth = plugin.get_auth( self.args.auth = plugin.get_auth(
username=credentials.key, username=credentials.key,
password=credentials.value, password=credentials.value,

View File

@ -15,6 +15,7 @@ from httpie.sessions import Session
from httpie.utils import get_expired_cookies from httpie.utils import get_expired_cookies
from .test_auth_plugins import basic_auth from .test_auth_plugins import basic_auth
from .utils import HTTP_OK, MockEnvironment, http, mk_config_dir from .utils import HTTP_OK, MockEnvironment, http, mk_config_dir
from base64 import b64encode
class SessionTestBase: class SessionTestBase:
@ -298,8 +299,6 @@ class TestSession(SessionTestBase):
assert f'Authorization: {header}' in r2 assert f'Authorization: {header}' in r2
plugin_manager.unregister(Plugin) plugin_manager.unregister(Plugin)
@mock.patch('httpie.cli.argtypes.AuthCredentials._getpass',
new=lambda self, prompt: 'password')
def test_auth_plugin_prompt_password_in_session(self, httpbin): def test_auth_plugin_prompt_password_in_session(self, httpbin):
self.start_session(httpbin) self.start_session(httpbin)
session_path = self.config_dir / 'test-session.json' session_path = self.config_dir / 'test-session.json'
@ -308,17 +307,22 @@ class TestSession(SessionTestBase):
auth_type = 'test-prompted' auth_type = 'test-prompted'
def get_auth(self, username=None, password=None): def get_auth(self, username=None, password=None):
return basic_auth() basic_auth_header = "Basic " + b64encode(self.raw_auth.encode()).strip().decode('latin1')
return basic_auth(basic_auth_header)
plugin_manager.register(Plugin) plugin_manager.register(Plugin)
r1 = http( with mock.patch(
'--session', str(session_path), 'httpie.cli.argtypes.AuthCredentials._getpass',
httpbin + '/basic-auth/user/password', new=lambda self, prompt: 'password'
'--auth-type', ):
Plugin.auth_type, r1 = http(
'--auth', 'user:', '--session', str(session_path),
) httpbin + '/basic-auth/user/password',
'--auth-type',
Plugin.auth_type,
'--auth', 'user',
)
r2 = http( r2 = http(
'--session', str(session_path), '--session', str(session_path),
@ -326,6 +330,13 @@ class TestSession(SessionTestBase):
) )
assert HTTP_OK in r1 assert HTTP_OK in r1
assert HTTP_OK in r2 assert HTTP_OK in r2
# additional test for issue: https://github.com/httpie/httpie/issues/1098
with open(session_path) as session_file:
session_file_lines = ''.join(session_file.readlines())
assert "\"type\": \"test-prompted\"" in session_file_lines
assert "\"raw_auth\": \"user:password\"" in session_file_lines
plugin_manager.unregister(Plugin) plugin_manager.unregister(Plugin)
def test_auth_type_stored_in_session_file(self, httpbin): def test_auth_type_stored_in_session_file(self, httpbin):