Removed moved service
This commit is contained in:
1 parent
54aa1cc0ad
commit
e14af22d3c
1 file changed
+16
-41
+16
-41
@@ -11,40 +11,6 @@ use crate::db::external_id::UserId;
|
||||
use crate::services::ReadOnlyServiceFactory;
|
||||
use crate::SQLiteConnectionFactory;
|
||||
|
||||
struct SessionService {
|
||||
key: Key,
|
||||
|
||||
}
|
||||
|
||||
impl SessionService {
|
||||
fn new() -> SessionService{
|
||||
let super_secret_not_hardcoded_at_all: [u8; 32] = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
|
||||
SessionService{key: Key::derive_from(&super_secret_not_hardcoded_at_all)}
|
||||
}
|
||||
|
||||
fn create_login_cookie(&self, user_id: &String ) -> Cookie{
|
||||
let mut jar = CookieJar::new();
|
||||
jar.private_mut(& self.key).add(Cookie::new("ledger-user-id", user_id.clone()));
|
||||
jar.get("ledger-user-id").unwrap_or(& Cookie::new("ledger-user-id", "")).clone()
|
||||
}
|
||||
|
||||
fn _decrypt_cookie(&self, cookie: Cookie<'static>) -> Option<Cookie>{
|
||||
let mut jar = CookieJar::new();
|
||||
jar.private(& self.key).decrypt(cookie)
|
||||
}
|
||||
|
||||
fn get_login(&self, req: &HttpRequest) -> Option<String> {
|
||||
req.cookie("ledger-user-id").and_then(|c|self._decrypt_cookie(c)).map(|c| c.value().to_string())
|
||||
}
|
||||
|
||||
fn logout(&self) -> Cookie{
|
||||
let mut cookie = Cookie::new("ledger-user-id", "");
|
||||
let epoch = OffsetDateTime::from_unix_timestamp_nanos(0).unwrap();
|
||||
cookie.set_expires(epoch);
|
||||
cookie
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct LoginPayload {
|
||||
username: String,
|
||||
@@ -69,20 +35,29 @@ pub async fn login(
|
||||
}
|
||||
|
||||
#[actix_web::post("/logout")]
|
||||
pub async fn logout(tmpl_env: MiniJinjaRenderer) -> actix_web::Result<impl Responder> {
|
||||
let ctx = context! {};
|
||||
let s = SessionService::new();
|
||||
let res = tmpl_env.render("partials/index/form.html", ctx)?;
|
||||
pub async fn logout(conn_factory: web::Data<SQLiteConnectionFactory>,
|
||||
tmpl_env: MiniJinjaRenderer) -> actix_web::Result<impl Responder> {
|
||||
|
||||
let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500
|
||||
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
||||
let authentication_service = service_factory.get_authentication_service().unwrap();
|
||||
|
||||
let res = tmpl_env.render("partials/index/form.html", context! {})?;
|
||||
Ok(res
|
||||
.customize()
|
||||
.insert_header(("Set-Cookie", s.logout().to_string())))
|
||||
.insert_header(("Set-Cookie", authentication_service.logout().to_string())))
|
||||
}
|
||||
|
||||
#[actix_web::get("/")]
|
||||
pub async fn index(req: HttpRequest,
|
||||
conn_factory: web::Data<SQLiteConnectionFactory>,
|
||||
tmpl_env: MiniJinjaRenderer) -> actix_web::Result<impl Responder> {
|
||||
let s = SessionService::new();
|
||||
let userid = & s.get_login(& req);
|
||||
|
||||
let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500
|
||||
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
||||
let authentication_service = service_factory.get_authentication_service().unwrap();
|
||||
|
||||
let userid = authentication_service.get_login(& req);;
|
||||
let username = userid.clone().unwrap_or("".to_string());
|
||||
let ctx = context!{
|
||||
is_logged_in => userid.is_some(),
|
||||
|
||||
Reference in new issue
Block a user