def _q_index(request): errors = [] key_lines = '' user = request.user sshkeys = user.sshkeys if request.method == "POST": key_lines = request.get_form_var('ssh') newsshkeys = [] errorkeys = [] for index, line in enumerate(key_lines.splitlines()): valid = SSHKey.validate(user.name, line) if not valid: errorkeys.append((index, line)) continue duplicated = SSHKey.is_duplicated(user.name, line) if duplicated: errorkeys.append((index, line)) continue newsshkeys.append(line) if not errorkeys: for key in newsshkeys: SSHKey.add(user.name, key) return request.redirect('/settings/ssh') error_prefix = 'Please check your SSH Key, Line: ' for no, key in errorkeys: error = error_prefix + '%s ' % no errors.append(error) return st('/settings/ssh.html', **locals())
def test_get(self): newkey = SSHKey.add('test', KEY1) key = SSHKey.get(newkey.id) assert isinstance(key, SSHKey) assert key.fingerprint == KEY1_FINGERPRINT assert key.finger == KEY1_FINGERPRINT assert key.title == KEY1_TITLE assert key.key == KEY1
def test_validate(self): r = SSHKey.validate('test', KEY1) assert r is True r = SSHKey.validate('test', KEY2) assert r is None r = SSHKey.validate('test', KEY3) assert r is None r = SSHKey.validate('test', KEY4) assert r is True
def _q_lookup(request, sshkey_id): if request.get_form_var('_method') == 'delete': user = request.user sshkey = SSHKey.check_own_by_user(user.name, sshkey_id) if sshkey: sshkey.delete() return request.redirect('/settings/ssh')
def get_user_handler(username, key): # ssh username if username != "git": return None # ssh key if not key: return None fingerprint = hexlify(key.get_fingerprint()) fingerprint = ":".join([fingerprint[i : 2 + i] for i in range(0, len(fingerprint), 2)]) sshkey = SSHKey.get_by_fingerprint(fingerprint) if not sshkey: return "guest" return sshkey.user_id
def get_user_handler(username, key): # ssh username if username != 'git': return None # ssh key if not key: return None fingerprint = hexlify(key.get_fingerprint()) fingerprint = ":".join( [fingerprint[i:2 + i] for i in range(0, len(fingerprint), 2)]) sshkey = SSHKey.get_by_fingerprint(fingerprint) if not sshkey: return 'guest' return sshkey.user_id
def test_duplicate(self): SSHKey.add('test', KEY1) r = SSHKey.is_duplicated('test', KEY1) assert r is True r = SSHKey.is_duplicated('test', KEY5) assert r is None
def test_delete(self): newkey = SSHKey.add('test', KEY1) newkey.delete() key = SSHKey.get(newkey.id) assert key is None
def sshkeys(self): return SSHKey.gets_by_user_id(self.name)