Esempio n. 1
0
    def index(self, error: str = "", code: int = 200) -> HttpResponse:
        self.security.check_local()

        if not self.storage.cache_file_exists():
            self.storage.resetCache()
            return self.session.create()

        had_error = self.security.check_session()

        if self.cache.is_empty():
            return render(self, "pages/login.jinja"), 401

        elif had_error:
            return render(self, "pages/login.jinja", error="Invalid session"), 401

        res = make_response(
            render(
                self,
                "pages/index.jinja",
                error=error,
                preload=json.dumps(api_recent(self)),
                friends=json.dumps(api_friends(self)),
            )
        )

        res.set_cookie("uname", "", expires=0)

        return res, code
Esempio n. 2
0
    def msg(self, uuid: str) -> HttpResponse:
        ret = self.security.check_all()
        if ret:
            return ret

        for friend in self.cache["history"]:
            if friend["id"] == uuid:
                break
        else:
            abort(404)

        self.redraw = True

        res = make_response(
            render(
                self,
                "pages/msg.jinja",
                preload=json.dumps(api_allfor(self, uuid)),
                friends=json.dumps(api_friends(self)),
            )
        )
        res.set_cookie("uname", uuid)

        self.redraw = True

        return res, 200
Esempio n. 3
0
    def login(self) -> HttpResponse:
        self.security.check_local()

        if self.cache.is_empty():
            return render(self, "pages/login.jinja"), 200

        else:
            return self.index(error="Already logged in", code=301)
Esempio n. 4
0
    def test_all_settings_added_automatically(self) -> None:
        template = render(self.shimon, "testing/render.jinja")

        def assertHasKeys(html: str) -> None:
            keys = ["developer", "theme", "fresh_js", "fresh_css"]

            for key in keys:
                assert html.find(key + "=" +
                                 str(getattr(self.shimon, key))) > -1

        assertHasKeys(template.data.decode())
Esempio n. 5
0
    def create(self, target: str = "pages/index.jinja") -> HttpResponse:
        res = make_response(
            render(
                self.shimon,
                target,
                preload=json.dumps(api_recent(self.shimon)),
                friends=json.dumps(api_friends(self.shimon)),
            ))

        self.session = urlb64encode(os.urandom(32)).decode().replace("=", "")

        res.set_cookie("session", self.session)
        self.keepalive()

        return res, 200
Esempio n. 6
0
    def check(self, data: Dict) -> Optional[HttpResponse]:
        if datetime.now() > (self.lastcall + timedelta(seconds=self.expires)):
            self.kill()

        elif self.session == data.get("session", ""):
            self.keepalive()
            return None

        return error(
            401,
            render(self.shimon,
                   "pages/login.jinja",
                   msg="Session is no longer valid"),
            data["redirect"],
            True,
        )
Esempio n. 7
0
    def attempt_lock(self,
                     pwd: str) -> Union[HttpResponse, Literal["fail"], None]:
        if not self.shimon.cache or self.shimon.cache.is_empty():
            return error(
                400,
                render(self.shimon,
                       "pages/login.jinja",
                       msg="Please re-open cache"),
                False,
                True,
            )

        if self.shimon.cache["sha512"]:
            if self.shimon.security.correct_pwd(pwd):
                self.raw_lock(self.filepath,
                              json.dumps(self.shimon.cache.export()), pwd)

                return None

        return "fail"
Esempio n. 8
0
    def lock(self, pwd: str) -> Optional[HttpResponse]:
        error_status = self.attempt_lock(pwd)

        if error_status == "fail":
            return error(
                401,
                render(
                    self.shimon,
                    "pages/account.jinja",
                    error="Cache could not be locked",
                    version=self.shimon.VERSION,
                ),
                True,
                False,
            )

        elif not error_status:
            return None

        return error_status
Esempio n. 9
0
def lock(self: "Shimon", pwd: str, redirect: bool) -> HttpResponse:
    # dont kill session unless user will be directed to login
    if not redirect:
        return error_400()

    returned_error = self.storage.lock(pwd)
    if returned_error:
        return returned_error

    # clean up object states
    self.cache.wipe()
    self.session.kill()

    res = make_response(
        render(self, "pages/login.jinja", error="Cache has been locked"))

    res.set_cookie("uname", "", expires=0)
    res.set_cookie("session", "", expires=0)

    return res, 200
Esempio n. 10
0
    def settings(self) -> HttpResponse:
        ret = self.security.check_all()
        if ret:
            return ret

        themes = []

        theme_folder = Path("SHIMON/templates/themes/").resolve()
        for theme in theme_folder.iterdir():
            if Path(theme).is_file() and theme.name.endswith(".css"):
                themes.append((theme.name[:-4],) * 2)

        return (
            render(
                self,
                "pages/settings.jinja",
                seconds=self.session.expires,
                msg_policy=self.msg_policy,
                themes=themes,
            ),
            200,
        )
Esempio n. 11
0
    def error(self, ex: Union[int, Exception]) -> HttpResponse:
        return_code = 500

        if isinstance(ex, HTTPException):
            return_code = ex.code or 500

        elif isinstance(ex, int):
            return_code = ex if (300 <= ex <= 417) else 400

        tb = ""
        if isinstance(ex, BaseException) and self.developer:
            tb = traceback.format_exc()

        return (
            render(
                self,
                "pages/error.jinja",
                error=return_code,
                url=request.url,
                traceback=tb,
                msg=http_codes.get(return_code, ""),
            ),
            return_code,
        )
Esempio n. 12
0
    def add(self) -> HttpResponse:
        ret = self.security.check_all()
        if ret:
            return ret

        return render(self, "pages/add.jinja"), 200
Esempio n. 13
0
    def account(self) -> HttpResponse:
        ret = self.security.check_all()
        if ret:
            return ret

        return render(self, "pages/account.jinja", version=self.VERSION), 200
Esempio n. 14
0
def render_login(self: "Shimon", error: str) -> HttpResponse:
    return render(self, "pages/login.jinja", error=error), 401