~sloecode/sloecode/1.0

« back to all changes in this revision

Viewing changes to sloecode/model/authkey.py

  • Committer: Thomi Richards
  • Date: 2012-03-04 04:35:48 UTC
  • mfrom: (109.1.36 trunk)
  • Revision ID: thomi.richards@canonical.com-20120304043548-xlo68ox0cckxeph1
Merged from trunk, in preparation with 1.1 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from sqlalchemy import Column
5
5
from sqlalchemy.types import Integer, Text
6
6
from sqlalchemy.schema import ForeignKey
 
7
from twisted.conch.ssh.keys import BadKeyError, Key as SSHKey
7
8
import formencode
 
9
import base64
8
10
import re
9
11
 
10
12
from sloecode.model.meta import Base, QueryMixin
43
45
    def _to_python(self, value, state):
44
46
        """Try to validate form data.
45
47
        """
 
48
        def raise_badkey():
 
49
            raise formencode.Invalid('Invalid SSH Key.', value, state)
46
50
        match = self.regexp.match(value)
47
 
        if match:
48
 
            return match.groups()
49
 
        else:
50
 
            raise formencode.Invalid(
51
 
                'Invalid SSH Key.', value, state)
 
51
        if not match:
 
52
            raise_badkey()
 
53
        ssh_type, data, comment = match.groups()
 
54
        try:
 
55
            blob = base64.b64decode(data)
 
56
        except TypeError:
 
57
            raise_badkey()
 
58
        try:
 
59
            key = SSHKey.fromString(blob, type='BLOB')
 
60
        except BadKeyError:
 
61
            raise_badkey()
 
62
        return ssh_type, data, comment
52
63
 
53
64
class AuthKeySchema(formencode.Schema):
54
65
    "A formencode schema for the AuthKey object."