Skip to content

Commit 2d4914b

Browse files
committed
Merge branch 'gpgauth' into testing
Conflicts: src/ircdb.py
2 parents 0537166 + 97bffbd commit 2d4914b

File tree

5 files changed

+313
-1
lines changed

5 files changed

+313
-1
lines changed

plugins/User/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,13 @@ def configure(advanced):
4747
# conf.registerGlobalValue(User, 'someConfigVariableName',
4848
# registry.Boolean(False, """Help for someConfigVariableName."""))
4949

50+
conf.registerGroup(User, 'gpg')
51+
52+
conf.registerGlobalValue(User.gpg, 'enable',
53+
registry.Boolean(True, """Determines whether or not users are
54+
allowed to use GPG for authentication."""))
55+
conf.registerGlobalValue(User.gpg, 'TokenTimeout',
56+
registry.PositiveInteger(60*10, """Determines the lifetime of a GPG
57+
authentication token (in seconds)."""))
58+
5059
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

plugins/User/plugin.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
###
2929

3030
import re
31+
import uuid
32+
import time
3133
import fnmatch
3234

3335
import supybot.conf as conf
36+
import supybot.gpg as gpg
3437
import supybot.utils as utils
3538
import supybot.ircdb as ircdb
3639
from supybot.commands import *
@@ -391,6 +394,121 @@ def remove(self, irc, msg, args, user, hostmask, password):
391394
remove = wrap(remove, ['private', 'otherUser', 'something',
392395
additional('something', '')])
393396

397+
class gpg(callbacks.Commands):
398+
def __init__(self, *args):
399+
super(User.gpg, self).__init__(*args)
400+
self._tokens = {}
401+
402+
def callCommand(self, command, irc, msg, *args, **kwargs):
403+
if gpg.available and self.registryValue('gpg.enable'):
404+
return super(User.gpg, self) \
405+
.callCommand(command, irc, msg, *args, **kwargs)
406+
else:
407+
irc.error(_('GPG features are not enabled.'))
408+
409+
def _expire_tokens(self):
410+
now = time.time()
411+
self._tokens = dict(filter(lambda (x,y): y[1]>now,
412+
self._tokens.items()))
413+
414+
@internationalizeDocstring
415+
def add(self, irc, msg, args, user, keyid, keyserver):
416+
"""<key id> <key server>
417+
418+
Add a GPG key to your account."""
419+
if keyid in user.gpgkeys:
420+
irc.error(_('This key is already associated with your '
421+
'account.'))
422+
return
423+
result = gpg.keyring.recv_keys(keyserver, keyid)
424+
reply = format(_('%n imported, %i unchanged, %i not imported.'),
425+
(result.imported, _('key')),
426+
result.unchanged,
427+
result.not_imported,
428+
[x['fingerprint'] for x in result.results])
429+
if result.imported == 1:
430+
user.gpgkeys.append(keyid)
431+
irc.reply(reply)
432+
else:
433+
irc.error(reply)
434+
add = wrap(add, ['user', 'somethingWithoutSpaces',
435+
'somethingWithoutSpaces'])
436+
437+
@internationalizeDocstring
438+
def remove(self, irc, msg, args, user, fingerprint):
439+
"""<fingerprint>
440+
441+
Remove a GPG key from your account."""
442+
try:
443+
keyids = [x['keyid'] for x in gpg.keyring.list_keys()
444+
if x['fingerprint'] == fingerprint]
445+
if len(keyids) == 0:
446+
raise ValueError
447+
for keyid in keyids:
448+
user.gpgkeys.remove(keyid)
449+
gpg.keyring.delete_keys(fingerprint)
450+
irc.replySuccess()
451+
except ValueError:
452+
irc.error(_('GPG key not associated with your account.'))
453+
remove = wrap(remove, ['user', 'somethingWithoutSpaces'])
454+
455+
@internationalizeDocstring
456+
def gettoken(self, irc, msg, args):
457+
"""takes no arguments
458+
459+
Send you a token that you'll have to sign with your key."""
460+
self._expire_tokens()
461+
token = '{%s}' % str(uuid.uuid4())
462+
lifetime = conf.supybot.plugins.User.gpg.TokenTimeout()
463+
self._tokens.update({token: (msg.prefix, time.time()+lifetime)})
464+
irc.reply(_('Your token is: %s. Please sign it with your '
465+
'GPG key, paste it somewhere, and call the \'auth\' '
466+
'command with the URL to the (raw) file containing the '
467+
'signature.') % token)
468+
gettoken = wrap(gettoken, [])
469+
470+
_auth_re = re.compile(r'-----BEGIN PGP SIGNED MESSAGE-----\r?\n'
471+
r'Hash: .*\r?\n\r?\n'
472+
r'\s*({[0-9a-z-]+})\s*\r?\n'
473+
r'-----BEGIN PGP SIGNATURE-----\r?\n.*'
474+
r'\r?\n-----END PGP SIGNATURE-----',
475+
re.S)
476+
@internationalizeDocstring
477+
def auth(self, irc, msg, args, url):
478+
"""<url>
479+
480+
Check the GPG signature at the <url> and authenticates you if
481+
the key used is associated to a user."""
482+
self._expire_tokens()
483+
match = self._auth_re.search(utils.web.getUrl(url))
484+
if not match:
485+
irc.error(_('Signature or token not found.'), Raise=True)
486+
data = match.group(0)
487+
token = match.group(1)
488+
if token not in self._tokens:
489+
irc.error(_('Unknown token. It may have expired before you '
490+
'submit it.'), Raise=True)
491+
if self._tokens[token][0] != msg.prefix:
492+
irc.error(_('Your hostname/nick changed in the process. '
493+
'Authentication aborted.'))
494+
verified = gpg.keyring.verify(data)
495+
if verified and verified.valid:
496+
keyid = verified.key_id
497+
prefix, expiry = self._tokens.pop(token)
498+
found = False
499+
for (id, user) in ircdb.users.items():
500+
if keyid in map(lambda x:x[-len(keyid):], user.gpgkeys):
501+
user.addAuth(msg.prefix)
502+
ircdb.users.setUser(user, flush=False)
503+
irc.reply(_('You are now authenticated as %s.') %
504+
user.name)
505+
return
506+
irc.error(_('Unknown GPG key.'), Raise=True)
507+
else:
508+
irc.error(_('Signature could not be verified. Make sure '
509+
'this is a valid GPG signature and the URL is valid.'))
510+
auth = wrap(auth, ['url'])
511+
394512
@internationalizeDocstring
395513
def capabilities(self, irc, msg, args, user):
396514
"""[<name>]

plugins/User/test.py

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,67 @@
2727
# POSSIBILITY OF SUCH DAMAGE.
2828
###
2929

30-
from supybot.test import *
30+
import re
31+
from cStringIO import StringIO
3132

33+
import supybot.gpg as gpg
34+
from supybot.test import PluginTestCase, network
35+
36+
import supybot.conf as conf
3237
import supybot.world as world
3338
import supybot.ircdb as ircdb
39+
import supybot.utils as utils
40+
41+
PRIVATE_KEY = """
42+
-----BEGIN PGP PRIVATE KEY BLOCK-----
43+
Version: GnuPG v1.4.12 (GNU/Linux)
44+
45+
lQHYBFD7GxQBBACeu7bj/wgnnv5NkfHImZJVJLaq2cwKYc3rErv7pqLXpxXZbDOI
46+
jP+5eSmTLhPUK67aRD6gG0wQ9iAhYR03weOmyjDGh0eF7kLYhu/4Il56Y/YbB8ll
47+
Imz/pep/Hi72ShcW8AtifDup/KeHjaWa1yF2WThHbX/0N2ghSxbJnatpBwARAQAB
48+
AAP6Arf7le7FD3ZhGZvIBkPr25qca6i0Qxb5XpOinV7jLcoycZriJ9Xofmhda9UO
49+
xhNVppMvs/ofI/m0umnR4GLKtRKnJSc8Edxi4YKyqLehfBTF20R/kBYPZ772FkNW
50+
Kzo5yCpP1jpOc0+QqBuU7OmrG4QhQzTLXIUgw4XheORncEECAMGkvR47PslJqzbY
51+
VRIzWEv297r1Jxqy6qgcuCJn3RWYJbEZ/qdTYy+MgHGmaNFQ7yhfIzkBueq0RWZp
52+
Z4PfJn8CANHZGj6AJZcvb+VclNtc5VNfnKjYD+qQOh2IS8NhE/0umGMKz3frH1TH
53+
yCbh2LlPR89cqNcd4QvbHKA/UmzISXkB/37MbUnxXTpS9Y4HNpQCh/6SYlB0lucV
54+
QN0cgjfhd6nBrb6uO6+u40nBzgynWcEpPMNfN0AtQeA4Dx+WrnK6kZqfd7QMU3Vw
55+
eWJvdCB0ZXN0iLgEEwECACIFAlD7GxQCGwMGCwkIBwMCBhUIAgkKCwQWAgMBAh4B
56+
AheAAAoJEMnTMjwgrwErV3AD/0kRq8UWPlkc6nyiIR6qiT3EoBNHKIi4cz68Wa1u
57+
F2M6einrRR0HolrxonynTGsdr1u2f3egOS4fNfGhTNAowSefYR9q5kIYiYE2DL5G
58+
YnjJKNfmnRxZM9YqmEnN50rgu2cifSRehp61fXdTtmOAR3js+9wb73dwbYzr3kIc
59+
3WH1
60+
=UBcd
61+
-----END PGP PRIVATE KEY BLOCK-----
62+
"""
63+
64+
WRONG_TOKEN_SIGNATURE = """
65+
-----BEGIN PGP SIGNED MESSAGE-----
66+
Hash: SHA1
67+
68+
{a95dc112-780e-47f7-a83a-c6f3820d7dc3}
69+
-----BEGIN PGP SIGNATURE-----
70+
Version: GnuPG v1.4.12 (GNU/Linux)
71+
72+
iJwEAQECAAYFAlD7Jb0ACgkQydMyPCCvASv9HgQAhQf/oFMWcKwGncH0hjXC3QYz
73+
7ck3chgL3S1pPAvS69viz6i2bwYZYD8fhzHNJ/qtw/rx6thO6PwT4SpdhKerap+I
74+
kdem3LjM4fAGHRunHZYP39obNKMn1xv+f26mEAAWxdv/W/BLAFqxi3RijJywRkXm
75+
zo5GUl844kpnV+uk0Xk=
76+
=z2Cz
77+
-----END PGP SIGNATURE-----
78+
"""
79+
80+
FINGERPRINT = '2CF3E41500218D30F0B654F5C9D3323C20AF012B'
3481

3582
class UserTestCase(PluginTestCase):
3683
plugins = ('User', 'Admin', 'Config')
3784
prefix1 = '[email protected]'
3885
prefix2 = '[email protected]'
86+
87+
def setUp(self):
88+
super(UserTestCase, self).setUp()
89+
gpg.loadKeyring()
90+
3991
def testHostmaskList(self):
4092
self.assertError('hostmask list')
4193
original = self.prefix
@@ -151,5 +203,59 @@ def testUserPluginAndUserList(self):
151203
self.assertNotError('load Seen')
152204
self.assertResponse('user list', 'Foo')
153205

206+
if gpg.available and network:
207+
def testGpgAddRemove(self):
208+
self.assertNotError('register foo bar')
209+
self.assertError('user gpg add 51E516F0B0C5CE6A pgp.mit.edu')
210+
self.assertResponse('user gpg add EB17F1E0CEB63930 pgp.mit.edu',
211+
'1 key imported, 0 unchanged, 0 not imported.')
212+
self.assertNotError(
213+
'user gpg remove F88ECDE235846FA8652DAF5FEB17F1E0CEB63930')
214+
self.assertResponse('user gpg add EB17F1E0CEB63930 pgp.mit.edu',
215+
'1 key imported, 0 unchanged, 0 not imported.')
216+
self.assertResponse('user gpg add EB17F1E0CEB63930 pgp.mit.edu',
217+
'Error: This key is already associated with your account.')
218+
219+
if gpg.available:
220+
def testGpgAuth(self):
221+
self.assertNotError('register spam egg')
222+
gpg.keyring.import_keys(PRIVATE_KEY).__dict__
223+
(id, user) = ircdb.users.items()[0]
224+
user.gpgkeys.append(FINGERPRINT)
225+
msg = self.getMsg('gpg gettoken').args[-1]
226+
match = re.search('is: ({.*}).', msg)
227+
assert match, repr(msg)
228+
token = match.group(1)
229+
230+
def fakeGetUrlFd(*args, **kwargs):
231+
return fd
232+
(utils.web.getUrlFd, realGetUrlFd) = (fakeGetUrlFd, utils.web.getUrlFd)
233+
234+
fd = StringIO()
235+
fd.write('foo')
236+
fd.seek(0)
237+
self.assertResponse('gpg auth http://foo.bar/baz.gpg',
238+
'Error: Signature or token not found.')
239+
240+
fd = StringIO()
241+
fd.write(token)
242+
fd.seek(0)
243+
self.assertResponse('gpg auth http://foo.bar/baz.gpg',
244+
'Error: Signature or token not found.')
245+
246+
fd = StringIO()
247+
fd.write(WRONG_TOKEN_SIGNATURE)
248+
fd.seek(0)
249+
self.assertRegexp('gpg auth http://foo.bar/baz.gpg',
250+
'Error: Unknown token.*')
251+
252+
fd = StringIO()
253+
fd.write(str(gpg.keyring.sign(token)))
254+
fd.seek(0)
255+
self.assertResponse('gpg auth http://foo.bar/baz.gpg',
256+
'You are now authenticated as spam.')
257+
258+
utils.web.getUrlFd = realGetUrlFd
259+
154260
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
155261

src/gpg.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
###
2+
# Copyright (c) 2012, Valentin Lorentz
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions, and the following disclaimer.
10+
# * Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions, and the following disclaimer in the
12+
# documentation and/or other materials provided with the distribution.
13+
# * Neither the name of the author of this software nor the name of
14+
# contributors to this software may be used to endorse or promote products
15+
# derived from this software without specific prior written consent.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
# POSSIBILITY OF SUCH DAMAGE.
28+
###
29+
30+
import os
31+
32+
import supybot.log as log
33+
import supybot.conf as conf
34+
import supybot.world as world
35+
36+
try:
37+
import gnupg
38+
except ImportError:
39+
# As we do not want Supybot to depend on GnuPG, we will use it only if
40+
# it is available. Otherwise, we just don't allow user auth through GPG.
41+
log.debug('Cannot import gnupg, using fallback.')
42+
gnupg = None
43+
44+
available = (gnupg is not None)
45+
46+
def fallback(default_return=None):
47+
"""Decorator.
48+
Does nothing if gnupg is loaded. Otherwise, returns the supplied
49+
default value."""
50+
def decorator(f):
51+
if available:
52+
def newf(*args, **kwargs):
53+
return f(*args, **kwargs)
54+
else:
55+
def newf(*args, **kwargs):
56+
return default_return
57+
return newf
58+
return decorator
59+
60+
@fallback()
61+
def loadKeyring():
62+
global keyring
63+
path = os.path.abspath(conf.supybot.directories.data.dirize('GPGkeyring'))
64+
if not os.path.isdir(path):
65+
log.info('Creating directory %s' % path)
66+
os.mkdir(path, 0700)
67+
assert os.path.isdir(path)
68+
keyring = gnupg.GPG(gnupghome=path)
69+
loadKeyring()
70+
71+
# Reload the keyring if path changed
72+
conf.supybot.directories.data.addCallback(loadKeyring)

src/ircdb.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def __init__(self, ignore=False, password='', name='',
225225
self.nicks = {} # {'network1': ['foo', 'bar'], 'network': ['baz']}
226226
else:
227227
self.nicks = nicks
228+
self.gpgkeys = [] # GPG key ids
228229

229230
def __repr__(self):
230231
return format('%s(id=%s, ignore=%s, password="", name=%q, hashed=%r, '
@@ -357,6 +358,8 @@ def write(s):
357358
write('hostmask %s' % hostmask)
358359
for network, nicks in self.nicks.items():
359360
write('nicks %s %s' % (network, ' '.join(nicks)))
361+
for key in self.gpgkeys:
362+
write('gpgkey %s' % key)
360363
fd.write(os.linesep)
361364

362365

@@ -537,6 +540,10 @@ def capability(self, rest, lineno):
537540
self._checkId()
538541
self.u.capabilities.add(rest)
539542

543+
def gpgkey(self, rest, lineno):
544+
self._checkId()
545+
self.u.gpgkeys.append(rest)
546+
540547
def finish(self):
541548
if self.u.name:
542549
try:

0 commit comments

Comments
 (0)