From 1d94b4cf78048428e470382a5bbd7b40a4366107 Mon Sep 17 00:00:00 2001 From: lmwangi Date: Tue, 2 Oct 2018 18:20:27 +0300 Subject: [PATCH 1/3] First pass at netrc integration --- httpie/input.py | 15 +++++++++++++++ httpie/plugins/base.py | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/httpie/input.py b/httpie/input.py index ebf46df3f5..745c016ca0 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -6,6 +6,7 @@ import sys import re import errno +import netrc import mimetypes import getpass from io import BytesIO @@ -238,6 +239,20 @@ def _process_auth(self): self.args.auth_type = default_auth_plugin.auth_type plugin = plugin_manager.get_auth_plugin(self.args.auth_type)() + if plugin.auth_require and self.args.auth is None \ + and plugin.netrc_parse: + # Authentication required, no credentials provided and + # plugin allows netrc parsing + netrc_entries = netrc.netrc() + if url.netloc in netrc_entries.hosts: + login, account, password = netrc_entries.authenticators(url.netloc) + self.args.auth = plugin.get_auth( + username=login, + password=password, + ) + # Break early, we have acquired netrc credentials + return + if plugin.auth_require and self.args.auth is None: self.error('--auth required') diff --git a/httpie/plugins/base.py b/httpie/plugins/base.py index bbf53663dc..edf34073ab 100644 --- a/httpie/plugins/base.py +++ b/httpie/plugins/base.py @@ -29,6 +29,10 @@ class AuthPlugin(BasePlugin): # through `--auth, -a`. auth_require = True + # Set to `True` to make it possible for this auth + # plugin to acquire credentials from netrc + netrc_parse = False + # By default the `-a` argument is parsed for `username:password`. # Set this to `False` to disable the parsing and error handling. auth_parse = True From f16b7d03764e2f21ee4e8b5bc237789587e930cc Mon Sep 17 00:00:00 2001 From: lmwangi Date: Mon, 19 Nov 2018 22:55:46 +0300 Subject: [PATCH 2/3] group expressions using parenthesis --- httpie/input.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httpie/input.py b/httpie/input.py index 745c016ca0..4c31da1097 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -239,8 +239,8 @@ def _process_auth(self): self.args.auth_type = default_auth_plugin.auth_type plugin = plugin_manager.get_auth_plugin(self.args.auth_type)() - if plugin.auth_require and self.args.auth is None \ - and plugin.netrc_parse: + if (plugin.auth_require and self.args.auth is None + and plugin.netrc_parse): # Authentication required, no credentials provided and # plugin allows netrc parsing netrc_entries = netrc.netrc() From 56bfb3a93ae54c5be570a64d5f3861b593180482 Mon Sep 17 00:00:00 2001 From: lmwangi Date: Mon, 19 Nov 2018 23:03:28 +0300 Subject: [PATCH 3/3] Changelog updated to reflect addition of auth plugin netrc support --- CHANGELOG.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bce1ea56a1..4a19c54e4b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,7 @@ This project adheres to `Semantic Versioning `_. (in addition to ``yes``/``no``) and the boolean value is case-insensitive. * Fixed default headers being incorrectly case-sensitive. * Removed Python 2.6 support. +* Load credentials from netrc for auth plugins if user requests it.