Ejemplo n.º 1
0
    def __init__(self, config):

        self.config = json.load(open(config))
        self.env = Environment(
            autoescape=utils.guess_autoescape,
            loader=PackageLoader('tomatosalad', 'static/templates'),
            extensions=['jinja2.ext.autoescape'])
        self.env.filters["ejs"] = utils.jinja2_escapejs_filter

        self.db = Database({"connectionString": "sqlite:///tomatosalad.db"})
        self.db.cleanStaleAPIKeys()
Ejemplo n.º 2
0
class Web:

    app = Klein()
    auth = Auth()

    def __init__(self, config):

        self.config = json.load(open(config))
        self.env = Environment(
            autoescape=utils.guess_autoescape,
            loader=PackageLoader('tomatosalad', 'static/templates'),
            extensions=['jinja2.ext.autoescape'])
        self.env.filters["ejs"] = utils.jinja2_escapejs_filter

        self.db = Database({"connectionString": "sqlite:///tomatosalad.db"})
        self.db.cleanStaleAPIKeys()


    def _OK(self, result):
        return "OK"


    def render(self, request, path, *args, **kwargs):

        def _render(result, login):

            return self.env.get_template(path).render(
                login=result,
                sessionDetails=login,
                **kwargs
                )

        login = self.auth.logins.get(request.getSession().uid, None)

        if login:
            return self.db.fetchUser(email=login["email"]).addCallback(_render, login)
        else:
            return _render(None, None)


    @app.route('/')
    def pg_root(self, request):

        def _return(result):

            lb = []

            for user in result:
                userDict = dict(user)
                userDict["emailMD5"] = hashlib.md5(userDict["userEmail"].lower()).hexdigest()
                lb.append(userDict)

            return self.render(request, "index.html", leaderboard=lb)

        d = self.db.fetchLeaderboards()
        d.addCallback(_return)
        return d


    @app.route('/profile')
    @auth.verify_auth()
    def pg_profile(self, request):

        login = self.auth.logins.get(request.getSession().uid)

        d = self.db.fetchUser(login["email"])
        d.addCallback(lambda res: self.render(request, "profile.html", user=res))
        return d


    @app.route('/projects')
    @auth.verify_auth()
    def pg_projects(self, request):

        login = self.auth.logins.get(request.getSession().uid)

        d = self.db.fetchProjects(login["email"])
        d.addCallback(lambda res: self.render(request, "projects.html", projects=res))
        return d


    @app.route('/auth/logout', methods=['POST'])
    def pg_authLogout(self, request):

        session = request.getSession()
        if self.auth.logins.get(session.uid):
            del self.auth.logins[session.uid]

        return "OK"


    @app.route('/auth/login', methods=['POST'])
    def pg_authLogin(self, request):

        def _verify(result):

            if result["status"] == "okay":

                keyUser = utils.createAPIKey(temporary=True)
                keyPass = utils.createAPIKey(temporary=True)
                session = request.getSession()
                session.sessionTimeout = 999999999999999999

                self.auth.logins[session.uid] = {
                    "email": result["email"],
                    "APIUsername": keyUser,
                    "APIPassword": keyPass
                }
                
                d = self.db.createUser(result["email"])
                d.addCallback(lambda _: self.db.createAPIKey(result["email"], keyUser, keyPass, result["expires"]))
                d.addCallback(self._OK)
                return d
            else:
                request.setResponseCode("500")
                return "WRONG" 

        if "assertion" in request.args:

            d = treq.post("https://verifier.login.persona.org/verify",
                params={
                    "audience": "http://*****:*****@app.route('/js/', branch=True)
    def static_js(self, request):

        return File(basePath + '/static/js')


    @app.route('/img/', branch=True)
    def static_img(self, request):

        return File(basePath + '/static/img')


    @app.route('/css/', branch=True)
    def static_css(self, request):

        return File(basePath + '/static/css')


    @app.route('/robots.txt')
    def static_robotstxt(self, request):

        return FilePath(basePath + '/static/templates/robots.txt').getContent()