Ejemplo n.º 1
0
 def output():
     while True:
         with self.db.transaction() as t:
             top_id = first(
                 first(
                     t.query(SQL_SELECT + quote_column("next_id") +
                             SQL_FROM +
                             quote_column(ABOUT_TABLE)).data))
             max_id = top_id + 1000
             t.execute(SQL_UPDATE + quote_column(ABOUT_TABLE) +
                       SQL_SET + sql_eq(next_id=max_id))
         while top_id < max_id:
             yield top_id
             top_id += 1
Ejemplo n.º 2
0
    def device_callback(self, path=None):
        # HANDLE BROWESR RETURN FROM AUTH0 LOGIN
        error = request.args.get("error")
        if error:
            Log.error("You did it wrong")
        session_id = request.cookies.get(self.device.login.session.name)
        if not session_id:
            Log.error("You did it wrong")
        login_session = self.session_manager.get_session(session_id)

        code = request.args.get("code")
        state = request.args.get("state")

        result = self.device.db.query(
            sql_query({
                "from": "device",
                "select": "session_id",
                "where": {
                    "eq": {
                        "state": state
                    }
                },
            }))
        if not result.data:
            Log.error("expecting valid state")
        device_session_id = result.data[0][0]

        # GO BACK TO AUTH0 TO GET TOKENS
        token_request = {
            "client_id": self.device.auth0.client_id,
            "redirect_uri": self.device.auth0.redirect_uri,
            "code_verifier": login_session.code_verifier,
            "code": code,
            "grant_type": "authorization_code",
        }
        DEBUG and Log.note("Send token request to Auth0:\n {{request}}",
                           request=token_request)
        auth_response = requests.request(
            "POST",
            str(URL("https://" + self.device.auth0.domain,
                    path="oauth/token")),
            headers={
                "Accept": mimetype.JSON,
                "Content-Type": mimetype.JSON,
                # "Referer": str(URL(self.device.auth0.redirect_uri, query={"code": code, "state": state})),
            },
            data=value2json(token_request),
        )

        try:
            auth_result = wrap(auth_response.json())
        except Exception as e:
            Log.error("not json {{value}}",
                      value=auth_response.content,
                      cause=e)

        # VERIFY TOKENS, ADD USER TO DEVICE'S SESSION
        user_details = self.verify_opaque_token(auth_result.access_token)
        self.session_manager.update_session(
            device_session_id,
            {"user": self.permissions.get_or_create_user(user_details)},
        )

        # REMOVE DEVICE SETUP STATE
        with self.device.db.transaction() as t:
            t.execute(
                ConcatSQL(
                    SQL_DELETE,
                    SQL_FROM,
                    quote_column(self.device.table),
                    SQL_WHERE,
                    sql_eq(state=state),
                ))
        Log.note("login complete")
        return Response("Login complete. You may close this page", status=200)