Skip to content

Commit

Permalink
Fix redirect loop (#8)
Browse files Browse the repository at this point in the history
* Fix token verification

If the token has been cleared, then we end up returning an
internal server error instead of just returning the login page.
This has been fixed.

* Do not redirect on HTTP 500

We might end up entering an infinite redirect loop
  • Loading branch information
ohsayan authored Feb 6, 2022
1 parent 7a1204d commit 85cc6d0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/handlers/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ use tower_cookies::Cookies;
use super::{COOKIE_TOKEN, COOKIE_USERNAME};
use crate::{error::ResponseError, templates::LoginPage, util};

use skytable::{actions::AsyncActions, aio::Connection, ddl::AsyncDdl, pool::AsyncPool};
use skytable::{
actions::AsyncActions,
aio::Connection,
ddl::AsyncDdl,
error::{Error, SkyhashError},
pool::AsyncPool,
RespCode,
};

pub async fn root(
mut cookies: Cookies,
Expand Down Expand Up @@ -67,6 +74,11 @@ async fn verify_user<'a>(
uname: &'a str,
token: &'a str,
) -> crate::JotsyResponseResult<bool> {
let hash = util::sha2(token);
con.get(hash).await.map(|ret: String| Ok(ret.eq(uname)))?
let hash: String = util::sha2(token);
let x: Result<String, Error> = con.get(hash).await;
match x {
Ok(ret) => Ok(ret == uname),
Err(Error::SkyError(SkyhashError::Code(RespCode::NotFound))) => Ok(false),
Err(e) => Err(e.into()),
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ async fn main() -> DynResult<()> {
);
// get our skytable instance
let pool = pool::get_async(cfg.sky_host, cfg.sky_port, 10).await?;
// just attempt to get a connection
pool.get().await?;
log::trace!("Connected to Skytable pool");
util::create_tables(&pool).await?;
log::trace!("Created/reinitialized tables");
Expand Down
2 changes: 1 addition & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl NoticePage {
Self::new(message, true)
}
pub fn e500() -> String {
Self::new("An internal server error occurred", true)
Self::new("An internal server error occurred", false)
}
pub fn e500_resp() -> Response {
Response::builder()
Expand Down
2 changes: 2 additions & 0 deletions templates/redirect.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ <h1>{{ message }}</h1>
{% endif %}
{% if redirect %}
Redirecting you to the <a href="/">homepage</a>
{% else %}
Go back <a href="/">to the homepage</a>
{% endif %}
</p>
</body>
Expand Down

0 comments on commit 85cc6d0

Please sign in to comment.