~sircmpwn/meta.sr.ht

meta.sr.ht/metasrht/types/sshkey.py -rw-r--r-- 1.1 KiB
6fe4d21aDrew DeVault admin: add billing info transfer tool 3 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sqlalchemy as sa
import sqlalchemy_utils as sau
import sshpubkeys as ssh
from srht.database import Base, db
from srht.oauth import current_token

class SSHKey(Base):
    __tablename__ = 'sshkey'
    id = sa.Column(sa.Integer, primary_key=True)
    created = sa.Column(sa.DateTime)
    user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
    user = sa.orm.relationship('User', backref=sa.orm.backref('ssh_keys'))
    key = sa.Column(sa.String(4096))
    fingerprint = sa.Column(sa.String(512))
    comment = sa.Column(sa.String(256))
    last_used = sa.Column(sa.DateTime)

    def __repr__(self):
        return '<SSHKey {} {}>'.format(self.id, self.fingerprint)

    def to_dict(self):
        return {
            "id": self.id,
            "authorized": self.created,
            "comment": self.comment,
            "fingerprint": self.fingerprint,
            "key": self.key,
            "owner": self.user.to_dict(short=True),
            **({
                "last_used": self.last_used,
            } if current_token and current_token.user_id == self.user_id else {})
        }