Skip to content

Commit 6cbe289

Browse files
djmitchecrobinso
authored andcommitted
Add support for API Keys
Currently only via Bugzilla(api_key=XXX) parameter
1 parent 79f1754 commit 6cbe289

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

bugzilla/base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def fix_url(url):
235235
return url
236236

237237
def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
238-
sslverify=True, tokenfile=-1, use_creds=True):
238+
sslverify=True, tokenfile=-1, use_creds=True, api_key=None):
239239
"""
240240
:param url: The bugzilla instance URL, which we will connect
241241
to immediately. Most users will want to specify this at
@@ -258,6 +258,7 @@ def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
258258
# Settings the user might want to tweak
259259
self.user = user or ''
260260
self.password = password or ''
261+
self.api_key = api_key
261262
self.url = ''
262263

263264
self._proxy = None
@@ -507,6 +508,10 @@ def connect(self, url=None):
507508
log.info("user and password present - doing login()")
508509
self.login()
509510

511+
if self.api_key:
512+
log.debug("using API key")
513+
self._proxy.use_api_key(self.api_key)
514+
510515
version = self._proxy.Bugzilla.version()["version"]
511516
log.debug("Bugzilla version string: %s", version)
512517
self._set_bz_version(version)
@@ -542,6 +547,9 @@ def login(self, user=None, password=None):
542547
and password are both set. So under most circumstances you won't need
543548
to call this yourself.
544549
'''
550+
if self.api_key:
551+
raise ValueError("cannot login when using an API key")
552+
545553
if user:
546554
self.user = user
547555
if password:

bugzilla/transport.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,22 @@ def __init__(self, uri, tokenfile, *args, **kwargs):
7777
# No idea why pylint complains here, must be a bug
7878
ServerProxy.__init__(self, uri, *args, **kwargs)
7979
self.token_cache = _BugzillaTokenCache(uri, tokenfile)
80+
self.api_key = None
81+
82+
def use_api_key(self, api_key):
83+
self.api_key = api_key
8084

8185
def clear_token(self):
8286
self.token_cache.value = None
8387

8488
def _ServerProxy__request(self, methodname, params):
85-
if self.token_cache.value is not None:
86-
if len(params) == 0:
87-
params = ({}, )
89+
if len(params) == 0:
90+
params = ({}, )
8891

92+
if self.api_key is not None:
93+
if 'Bugzilla_api_key' not in params[0]:
94+
params[0]['Bugzilla_api_key'] = self.api_key
95+
elif self.token_cache.value is not None:
8996
if 'Bugzilla_token' not in params[0]:
9097
params[0]['Bugzilla_token'] = self.token_cache.value
9198

examples/apikey.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
#
3+
# This program is free software; you can redistribute it and/or modify it
4+
# under the terms of the GNU General Public License as published by the
5+
# Free Software Foundation; either version 2 of the License, or (at your
6+
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
7+
# the full text of the license.
8+
9+
# create.py: Create a new bug report
10+
11+
from __future__ import print_function
12+
13+
import bugzilla
14+
15+
# Don't worry, changing things here is fine, and won't send any email to
16+
# users or anything. It's what landfill.bugzilla.org is for!
17+
URL = "https://landfill.bugzilla.org/bugzilla-5.0-branch/xmlrpc.cgi"
18+
19+
print("You can get an API key at "
20+
"https://landfill.bugzilla.org/bugzilla-5.0-branch/userprefs.cgi")
21+
print("after creating an account, if necessary. "
22+
"This is a test site, so no harm will come!")
23+
api_key = raw_input("Enter Bugzilla API Key: ")
24+
25+
# API key usage assumes the API caller is storing the API key; if you would
26+
# like to use one of the login options that stores credentials on-disk for
27+
# command-line usage, use tokens or cookies.
28+
bzapi = bugzilla.Bugzilla(URL, api_key=api_key)
29+
assert bzapi.logged_in

0 commit comments

Comments
 (0)