Ejemplo n.º 1
0
 def setup(self):
     if not self.db.about(ABOUT_TABLE):
         with self.db.transaction() as t:
             t.execute(
                 sql_create(ABOUT_TABLE, {"version": "TEXT", "next_id": "INTEGER"})
             )
             t.execute(sql_insert(ABOUT_TABLE, {"version": "1.0", "next_id": 1000}))
             t.execute(sql_create(DIGITS_TABLE, {"value": "INTEGER"}))
             t.execute(sql_insert(DIGITS_TABLE, [{"value": i} for i in range(10)]))
Ejemplo n.º 2
0
 def setup(self):
     with self.db.transaction() as t:
         t.execute(
             sql_create(
                 self.table,
                 {
                     "session_id": "TEXT PRIMARY KEY",
                     "data": "TEXT",
                     "last_used": "NUMBER",
                     "expires": "NUMBER",
                 },
             ))
Ejemplo n.º 3
0
    def __init__(self,
                 flask_app,
                 auth0,
                 permissions,
                 session_manager,
                 device=None):
        if not auth0.domain:
            Log.error("expecting auth0 configuration")

        self.auth0 = auth0
        self.permissions = permissions
        self.session_manager = session_manager

        # ATTACH ENDPOINTS TO FLASK APP
        endpoints = auth0.endpoints
        if not endpoints.login or not endpoints.logout or not endpoints.keep_alive:
            Log.error("Expecting paths for login, logout and keep_alive")

        add_flask_rule(flask_app, endpoints.login, self.login)
        add_flask_rule(flask_app, endpoints.logout, self.logout)
        add_flask_rule(flask_app, endpoints.keep_alive, self.keep_alive)

        if device:
            self.device = device
            db = self.device.db = Sqlite(device.db)
            if not db.about("device"):
                with db.transaction() as t:
                    t.execute(
                        sql_create(
                            "device",
                            {
                                "state": "TEXT PRIMARY KEY",
                                "session_id": "TEXT"
                            },
                        ))
            if device.auth0.redirect_uri != text_type(
                    URL(device.home, path=device.endpoints.callback)):
                Log.error(
                    "expecting home+endpoints.callback == auth0.redirect_uri")

            add_flask_rule(flask_app, device.endpoints.register,
                           self.device_register)
            add_flask_rule(flask_app, device.endpoints.status,
                           self.device_status)
            add_flask_rule(flask_app, device.endpoints.login,
                           self.device_login)
            add_flask_rule(flask_app, device.endpoints.callback,
                           self.device_callback)
Ejemplo n.º 4
0
def id_generator(db):
    """
    INSTALL AN ID GENERATOR
    """
    about = db.about(VERSION_TABLE)
    if not about:
        with db.transaction() as t:
            t.execute(
                sql_create(VERSION_TABLE, {
                    "version": "TEXT",
                    "next_id": "LONG"
                }))
            t.execute(
                sql_insert(VERSION_TABLE, {
                    "version": "1.0",
                    "next_id": 1000
                }))
    else:
        for cid, name, dtype, notnull, dfft_value, pk in about:
            if name == "next_id":
                break
        else:
            with db.transaction() as t:
                t.execute("ALTER TABLE " + quote_column(VERSION_TABLE) +
                          " ADD COLUMN next_id LONG")
                t.execute(SQL_UPDATE + quote_column(VERSION_TABLE) + SQL_SET +
                          sql_eq(next_id=1000))

    def _gen_ids():
        while True:
            with db.transaction() as t:
                top_id = first(
                    first(
                        t.query(
                            sql_query({
                                "select": "next_id",
                                "from": VERSION_TABLE
                            })).data))
                max_id = top_id + 1000
                t.execute(SQL_UPDATE + quote_column(VERSION_TABLE) + SQL_SET +
                          sql_eq(next_id=max_id))
            while top_id < max_id:
                yield top_id
                top_id += 1

    return _gen_ids().__next__
Ejemplo n.º 5
0
    def get_or_create_facts(self, fact_name, uid=UID):
        """
        FIND TABLE BY NAME, OR CREATE IT IF IT DOES NOT EXIST
        :param fact_name:  NAME FOR THE CENTRAL FACTS
        :param uid: name, or list of names, for the GUID
        :return: Facts
        """
        about = self.db.about(fact_name)
        if not about:
            if uid != UID:
                Log.error("do not know how to handle yet")

            self.ns.columns._snowflakes[fact_name] = ["."]
            command = sql_create(fact_name, {UID: "INTEGER PRIMARY KEY", GUID: "TEXT"}, unique=UID)

            with self.db.transaction() as t:
                t.execute(command)

        return QueryTable(fact_name, self)
Ejemplo n.º 6
0
    def create_or_replace_facts(self, fact_name, uid=UID):
        """
        MAKE NEW TABLE, REPLACE OLD ONE IF EXISTS
        :param fact_name:  NAME FOR THE CENTRAL FACTS
        :param uid: name, or list of names, for the GUID
        :return: Facts
        """
        self.remove_facts(fact_name)
        self.ns.columns._snowflakes[fact_name] = ["."]

        if uid != UID:
            Log.error("do not know how to handle yet")

        command = sql_create(fact_name, {UID: "INTEGER PRIMARY KEY", GUID: "TEXT"}, unique=UID)

        with self.db.transaction() as t:
            t.execute(command)

        snowflake = Snowflake(fact_name, self.ns)
        return Facts(self, snowflake)
Ejemplo n.º 7
0
    def setup(self):
        with self.db.transaction() as t:
            t.execute(sql_create(VERSION_TABLE, {"version": "TEXT"}))
            t.execute(sql_insert(VERSION_TABLE, {"version": "1.0"}))

            t.execute(
                sql_create(
                    GROUP_TABLE,
                    {
                        "_id": "LONG PRIMARY KEY",
                        "name": "TEXT",
                        "group": "TEXT",
                        "email": "TEXT",
                        "issuer": "TEXT",
                        "email_verified": "INTEGER",
                        "description": "TEXT",
                        "owner": "LONG",
                    },
                ))

            t.execute(
                sql_insert(
                    GROUP_TABLE,
                    [
                        {
                            "_id": 1,
                            "name": "root",
                            "email": "*****@*****.**",
                            "description": "access for security system",
                        },
                        {
                            "_id": 11,
                            "group": "public",
                            "description": "everyone with confirmed email",
                            "owner": 1,
                        },
                        {
                            "_id": 12,
                            "group": "mozillians",
                            "description":
                            "people that mozilla authentication has recongized as mozillian",
                            "owner": 1,
                        },
                        {
                            "_id": 13,
                            "group": "moz-employee",
                            "description":
                            "people that mozilla authentication has recongized as employee",
                            "owner": 1,
                        },
                    ],
                ))

            t.execute(
                sql_create(
                    RESOURCE_TABLE,
                    {
                        "_id": "LONG PRIMARY KEY",
                        "table": "TEXT",
                        "operation": "TEXT",
                        "owner": "LONG",
                    },
                ))
            t.execute(
                sql_insert(
                    RESOURCE_TABLE,
                    [
                        CREATE_TABLE,
                        {
                            "_id": 101,
                            "table": ".",
                            "operation": "update",
                            "owner": 1
                        },
                        {
                            "_id": 102,
                            "table": ".",
                            "operation": "from",
                            "owner": 1
                        },
                    ],
                ))

            t.execute(
                sql_create(
                    PERMISSION_TABLE,
                    {
                        "user": "******",
                        "resource": "LONG",
                        "owner": "LONG"
                    },
                ))
            t.execute(
                sql_insert(
                    PERMISSION_TABLE,
                    [
                        {
                            "user": 12,
                            "resource": 11,
                            "owner": 1
                        },
                        {
                            "user": 13,
                            "resource": 11,
                            "owner": 1
                        },
                        {
                            "user": 13,
                            "resource": 12,
                            "owner": 1
                        },
                        {
                            "user": 1,
                            "resource": 100,
                            "owner": 1
                        },
                        {
                            "user": 1,
                            "resource": 101,
                            "owner": 1
                        },
                        {
                            "user": 1,
                            "resource": 102,
                            "owner": 1
                        },
                    ],
                ))