Exemplo n.º 1
0
class Identity(db.Model):
    '''
    Model for storing data about present users.
    '''

    __tablename__ = 'identity'
    id = db.Column(db.Integer, primary_key=True)
    user_idx = db.Column(db.Integer, nullable=True)
    logged_in = db.Column(db.Boolean, default=False)
    clients = db.relationship('Client', backref='identity', lazy='dynamic')
Exemplo n.º 2
0
class Client(db.Model):
    '''
    Model for storing data about devices
    '''
    __tablename__ = 'clients'

    id = db.Column(db.Integer, primary_key=True)
    client_uuid = db.Column(db.String, nullable=False, unique=True)
    client = db.Column(db.String(50), nullable=True)
    tokens = db.relationship('JWTBeacon', backref='client', lazy='dynamic')
    identity_id = db.Column(db.Integer,
                            db.ForeignKey('identity.id'),
                            nullable=True)
Exemplo n.º 3
0
class WSDevice(db.Model):

    __tablename__ = 'wsdevices'

    id = db.Column(db.Integer, primary_key=True)
    idx = db.Column(db.Integer, nullable=True)
    name = db.Column(db.String(100), nullable=False)
    description = db.Column(db.String(500), nullable=True)
    machine = db.Column(db.String(100), nullable=True)
    sysname = db.Column(db.String(100), nullable=True)
    version = db.Column(db.String(100), nullable=True)
    states = db.relationship('WSState',
                             backref='wsdevice',
                             cascade='all',
                             lazy='dynamic')
    commands = db.relationship('WSCommand',
                               cascade='all',
                               backref='wsdevice',
                               lazy='dynamic')

    def to_dict(self) -> dict:
        return {
            'id': self.id,
            'idx': self.idx,
            'name': self.name,
            'machine': self.machine,
            'sysname': self.sysname,
            'version': self.version,
            'description': self.description,
            'states': [s.to_dict() for s in self.states.all()],
            'commands': [c.to_dict() for c in self.commands.all()]
        }

    def __repr__(self):
        return str(self.to_dict())
Exemplo n.º 4
0
class OAuth2Scope(db.Model):
    '''
    Google OAuth2 scope for resource
    '''
    __tablename__ = 'oauth2_scopes'

    id = db.Column(db.Integer, primary_key=True)
    scope = db.Column(db.String(100), unique=True, nullable=False)

    @classmethod
    def from_scope(cls, scope):
        ''' Create OAuth2Scope model object if not exists

        :param scope: str - scope name
        :return: OAuth2Scope object
        '''
        scopes = {row.scope: row for row in cls.query.all()}
        if not scope in scopes.keys():
            return cls(scope=scope)
        return scopes[scope]
Exemplo n.º 5
0
class JWToken(db.Model):
    '''
    Model for storing JWT authorization tokens.
    '''

    __tablename__ = 'tokens'

    id = db.Column(db.Integer, primary_key=True)
    jti = db.Column(db.String(36), nullable=False, unique=True)
    token_type = db.Column(db.String(10), nullable=True)
    expires = db.Column(db.DateTime, nullable=True)
    revoked = db.Column(db.Boolean, nullable=False)

    def to_dict(self):
        return {
            'id': self.id,
            'jti': self.jti,
            'token_type': self.token_type,
            'expires': self.expires,
            'revoked': self.revoked
        }
Exemplo n.º 6
0
class OAuth2Credentials(db.Model):
    '''
    Google OAuth2 credentials for accessing Proximity Beacon API
    '''
    __tablename__ = 'oauth2_credentials'

    id = db.Column(db.Integer, primary_key=True)
    token_uri = db.Column(db.String(100), nullable=False)
    client_id = db.Column(db.String(100), unique=True, nullable=False)
    client_secret = db.Column(db.String(100), nullable=False)
    token = db.Column(db.String(250), nullable=False)
    refresh_token = db.Column(db.String(250), nullable=True)
    scopes = db.relationship(
        'OAuth2Scope',
        secondary=scopes_m2m,
        backref=db.backref('credentials', lazy=True),
        lazy='subquery')

    @classmethod
    def from_creds(cls, credentials):
        ''' Create model object from google.oauth2.credentials.Credentials

        :param credentials: google.oauth2.credentials.Credentials object
        :return: OAuth2Credential object
        '''
        scopes = credentials.scopes
        oauth2_scopes = [OAuth2Scope.from_scope(scope) for scope in scopes]
        oauth2_credential = OAuth2Credentials(
            client_id=credentials.client_id,
            token_uri=credentials.token_uri,
            client_secret=credentials.client_secret,
            token=credentials.token,
            refresh_token=credentials.refresh_token
        )
        for oauth2_scope in oauth2_scopes:
            oauth2_credential.scopes.append(oauth2_scope)
        return oauth2_credential

    def get_creds(self):
        ''' Get google.oauth2.credentials.Credentials object
        from model's object

        :return: google.oauth2.credentials.Credentials object
        '''
        return Credentials(**self.to_dict())

    def to_dict(self):
        return {
            'token_uri': self.token_uri,
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'token': self.token,
            'refresh_token': self.refresh_token,
            'scopes': [s.scope for s in self.scopes]
        }

    def __repr__(self):
        return str(self.to_dict())
Exemplo n.º 7
0
class JWTBeacon(JWToken):
    '''
    Model for storing JWT authorization tokens.
    '''

    client_id = db.Column(db.String,
                          db.ForeignKey('clients.client_uuid'),
                          nullable=True)

    def to_dict(self):
        return {
            'id': self.id,
            'jti': self.jti,
            'token_type': self.token_type,
            'client_id': self.client_id,
            'expires': self.expires,
            'revoked': self.revoked
        }
Exemplo n.º 8
0
class WSState(db.Model):

    __tablename__ = 'WSState'

    id = db.Column(db.Integer, primary_key=True)
    idx = db.Column(db.Integer, nullable=True)
    name = db.Column(db.String(30), nullable=False)
    state_type = db.Column(db.String(100), nullable=True)
    description = db.Column(db.String(500), nullable=True)
    value = db.Column(db.String(100), nullable=True)
    device_id = db.Column(db.Integer,
                          db.ForeignKey('wsdevices.id'),
                          nullable=True)

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'value': self.value,
            'state_type': self.state_type,
        }
Exemplo n.º 9
0
class WSCommand(db.Model):

    __tablename__ = 'wscommands'

    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(300), nullable=False)
    name = db.Column(db.String(30), nullable=False)
    event = db.Column(db.String(30), nullable=False)
    json_command = db.Column(JSONType, nullable=True)
    device_id = db.Column(db.Integer,
                          db.ForeignKey('wsdevices.id'),
                          nullable=True)

    def to_dict(self) -> dict:
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'event': self.event,
            'json_command': self.json_command,
            'device_id': self.device_id,
        }
Exemplo n.º 10
0
from google.oauth2.credentials import Credentials

from automoticz.extensions import db

scopes_m2m = db.Table(
    'scopes',
    db.Column(
        'scope_id',
        db.Integer,
        db.ForeignKey('oauth2_scopes.id'),
        primary_key=True),
    db.Column(
        'credential_id',
        db.Integer,
        db.ForeignKey('oauth2_credentials.id'),
        primary_key=True),
)


class OAuth2Scope(db.Model):
    '''
    Google OAuth2 scope for resource
    '''
    __tablename__ = 'oauth2_scopes'

    id = db.Column(db.Integer, primary_key=True)
    scope = db.Column(db.String(100), unique=True, nullable=False)

    @classmethod
    def from_scope(cls, scope):
        ''' Create OAuth2Scope model object if not exists