Skip to content

Commit f20947d

Browse files
committed
Integrate support for token auth
Per upcoming changes in Patchwork. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent e80ec47 commit f20947d

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

README.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,32 @@ Getting Started
7171
To begin, you'll need to configure Git settings appropriately. The following
7272
settings are **required**:
7373

74-
pw.server
74+
``pw.server``
7575

7676
The URL for the Patchwork instance. This will typically look like. For
7777
example::
7878

7979
https://patchwork.ozlabs.org/
8080

81-
pw.project
81+
``pw.project``
8282

8383
The project name or list-id. This will appear in the URL when using the web
8484
UI::
8585

8686
https://patchwork.ozlabs.org/project/{project_name}/list/
8787

88-
pw.username
88+
You also require authentication - you can use either API tokens or a
89+
username/password combination:
90+
91+
``pw.token``
92+
93+
The API token for you Patchwork account.
94+
95+
``pw.username``
8996

9097
The username for your Patchwork account.
9198

92-
pw.password
99+
``pw.password``
93100

94101
The password for your Patchwork account.
95102

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ git-pw
33

44
.. include:: ../README.rst
55
:start-line: 19
6-
:end-line: 124
6+
:end-line: -7
77

88
Usage
99
-----

git_pw/api.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,37 @@
99

1010
from git_pw import config
1111

12+
if 0: # noqa
13+
from typing import Dict # noqa
14+
1215
CONF = config.CONF
1316
LOG = logging.getLogger(__name__)
1417

1518

16-
def _get_auth(): # type: () -> (str, str)
17-
if CONF.username and CONF.password:
18-
return (CONF.username, CONF.password)
19+
class HTTPTokenAuth(requests.auth.AuthBase):
20+
"""Attaches HTTP Token Authentication to the given Request object."""
21+
def __init__(self, token):
22+
self.token = token
23+
24+
def __call__(self, r):
25+
r.headers['Authorization'] = self._token_auth_str(self.token)
26+
return r
27+
28+
@staticmethod
29+
def _token_auth_str(token): # type: (str) -> str
30+
"""Return a Token auth string."""
31+
return 'Token {}'.format(token.strip())
32+
33+
34+
def _get_auth(): # type: () -> requests.auth.AuthBase
35+
if CONF.token:
36+
return HTTPTokenAuth(CONF.token)
37+
elif CONF.username and CONF.password:
38+
return requests.auth.HTTPBasicAuth(CONF.username, CONF.password)
1939
else:
2040
LOG.error('Authentication information missing')
2141
LOG.error('You must configure authentication via git-config or via '
22-
'--username, --password')
42+
'--token or --username, --password')
2343
sys.exit(1)
2444

2545

git_pw/shell.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
@click.group()
1717
@click.option('--debug', default=False, is_flag=True,
1818
help="Output more information about what's going on.")
19+
@click.option('--token', metavar='TOKEN', envvar='PW_TOKEN',
20+
help='Authentication token.')
1921
@click.option('--username', metavar='USERNAME', envvar='PW_USERNAME',
2022
help='Authentication username.')
2123
@click.option('--password', metavar='PASSWORD', envvar='PW_PASSWORD',
@@ -25,7 +27,7 @@
2527
@click.option('--project', metavar='PROJECT', envvar='PW_PROJECT',
2628
help='Patchwork project.')
2729
@click.version_option()
28-
def cli(debug, username, password, server, project):
30+
def cli(debug, token, username, password, server, project):
2931
"""git-pw is a tool for integrating Git with Patchwork.
3032
3133
git-pw can interact with individual patches, complete patch series, and
@@ -34,19 +36,14 @@ def cli(debug, username, password, server, project):
3436
3537
The git-pw utility is a wrapper which makes REST calls to the Patchwork
3638
service. To use git-pw, you must set up your environment by configuring
37-
your Patchwork server URL, username, and password.
38-
39-
Configuring the patchwork URL::
39+
your Patchwork server URL and either an API token or a username and
40+
password. To configure the server URL, run::
4041
4142
git config pw.server http://pw.server.com/path/to/patchwork
4243
43-
Configuring username::
44-
45-
git config pw.username userid
46-
47-
Configuring password::
44+
To configure the token, run::
4845
49-
git config pw.password pass
46+
git config pw.token token
5047
5148
Alternatively, you can pass these options via command line parameters or
5249
environment variables.
@@ -57,6 +54,7 @@ def cli(debug, username, password, server, project):
5754
logger.configure_verbosity(debug)
5855

5956
CONF.debug = debug
57+
CONF.token = token
6058
CONF.username = username
6159
CONF.password = password
6260
CONF.server = server

0 commit comments

Comments
 (0)