From 76691f813a844e8c18d8de5fd2cc2d0117a9602e Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sat, 22 Dec 2007 22:52:44 -0800 Subject: Move the SSH username extraction to the ssh class, and the tests over as well. --- gitosis/ssh.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'gitosis/ssh.py') diff --git a/gitosis/ssh.py b/gitosis/ssh.py index f552255..10784fa 100644 --- a/gitosis/ssh.py +++ b/gitosis/ssh.py @@ -17,6 +17,20 @@ def isSafeUsername(user): match = _ACCEPTABLE_USER_RE.match(user) return (match is not None) +class InsecureSSHKeyUsername(Exception): + """Username contains not allowed characters""" + + def __str__(self): + return '%s: %s' % (self.__doc__, ': '.join(self.args)) + +def extract_user(pubkey): + """Find the username for a given SSH public key line.""" + _, user = pubkey.rsplit(None, 1) + if isSafeUsername(user): + return user + else: + raise InsecureSSHKeyUsername(repr(user)) + def readKeys(keydir): """ Read SSH public keys from ``keydir/*.pub`` -- cgit v1.2.3