lint code.
This commit is contained in:
1 parent
663bf85257
commit
f8d676e5df
9 files changed
+71
-62
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
use rusqlite::{Connection, params, Row, Transaction, Result};
|
|
||||||
use crate::util::{CustomResult, Error};
|
use crate::util::{CustomResult, Error};
|
||||||
use base64::{Engine as _, engine::general_purpose};
|
use base64::{engine::general_purpose, Engine as _};
|
||||||
|
use rusqlite::{params, Connection, Result, Row, Transaction};
|
||||||
|
|
||||||
pub struct LedgerInternal {}
|
pub struct LedgerInternal {}
|
||||||
|
|
||||||
@@ -47,11 +47,11 @@ impl LedgerInternal {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test_ledger_internal {
|
mod test_ledger_internal {
|
||||||
use crate::migration;
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::migration;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_initial_cookie_secret() -> CustomResult<()>{
|
fn test_initial_cookie_secret() -> CustomResult<()> {
|
||||||
let mut conn = Connection::open_in_memory().unwrap();
|
let mut conn = Connection::open_in_memory().unwrap();
|
||||||
migration::apply_migrations(&mut conn).unwrap();
|
migration::apply_migrations(&mut conn).unwrap();
|
||||||
let tx = conn.transaction().unwrap();
|
let tx = conn.transaction().unwrap();
|
||||||
|
|||||||
+2
-2
@@ -2,12 +2,12 @@ mod _accounts;
|
|||||||
mod _cheques;
|
mod _cheques;
|
||||||
mod _external_id;
|
mod _external_id;
|
||||||
mod _interests;
|
mod _interests;
|
||||||
|
mod _ledger_internal;
|
||||||
mod _securities;
|
mod _securities;
|
||||||
mod _test_utils;
|
mod _test_utils;
|
||||||
mod _users;
|
mod _users;
|
||||||
mod _ledger_internal;
|
|
||||||
|
|
||||||
pub mod ledger_internal{
|
pub mod ledger_internal {
|
||||||
pub use super::_ledger_internal::*;
|
pub use super::_ledger_internal::*;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,33 +1,36 @@
|
|||||||
use actix_web::HttpRequest;
|
|
||||||
use cookie::{CookieJar, Cookie, Key};
|
|
||||||
use cookie::time::OffsetDateTime;
|
|
||||||
use rusqlite::Connection;
|
|
||||||
use crate::db::ledger_internal::LedgerInternal;
|
use crate::db::ledger_internal::LedgerInternal;
|
||||||
use crate::{CustomResult, Error};
|
use crate::{CustomResult, Error};
|
||||||
|
use actix_web::HttpRequest;
|
||||||
|
use cookie::time::OffsetDateTime;
|
||||||
|
use cookie::{Cookie, CookieJar, Key};
|
||||||
|
use rusqlite::Connection;
|
||||||
|
|
||||||
pub struct SessionService {
|
pub struct SessionService {
|
||||||
key: Key,
|
key: Key,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl SessionService {
|
impl SessionService {
|
||||||
|
pub fn from_conn(conn: &mut Connection) -> CustomResult<SessionService> {
|
||||||
pub fn from_conn(conn: &mut Connection) -> CustomResult<SessionService>{
|
|
||||||
let tx = conn.transaction().map_err(Error::SqlError)?;
|
let tx = conn.transaction().map_err(Error::SqlError)?;
|
||||||
let secret = LedgerInternal::new().get_cookie_secret(& tx)?;
|
let secret = LedgerInternal::new().get_cookie_secret(&tx)?;
|
||||||
let ret = SessionService{key: Key::derive_from(&secret)};
|
let ret = SessionService {
|
||||||
|
key: Key::derive_from(&secret),
|
||||||
|
};
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_login_cookie(&self, user_id: &String ) -> Cookie{
|
pub fn create_login_cookie(&self, user_id: &String) -> Cookie {
|
||||||
let mut jar = CookieJar::new();
|
let mut jar = CookieJar::new();
|
||||||
jar.private_mut(& self.key).add(Cookie::new("ledger-user-id", user_id.clone()));
|
jar.private_mut(&self.key)
|
||||||
jar.get("ledger-user-id").unwrap_or(& Cookie::new("ledger-user-id", "")).clone()
|
.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>{
|
fn _decrypt_cookie(&self, cookie: Cookie<'static>) -> Option<Cookie> {
|
||||||
let mut jar = CookieJar::new();
|
let mut jar = CookieJar::new();
|
||||||
jar.private(& self.key).decrypt(cookie)
|
jar.private(&self.key).decrypt(cookie)
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: this service should not be aware of http requests.
|
// todo: this service should not be aware of http requests.
|
||||||
@@ -35,13 +38,14 @@ impl SessionService {
|
|||||||
// web framework
|
// web framework
|
||||||
pub fn get_login(&self, req: &HttpRequest) -> Option<String> {
|
pub fn get_login(&self, req: &HttpRequest) -> Option<String> {
|
||||||
req.cookie("ledger-user-id")
|
req.cookie("ledger-user-id")
|
||||||
.and_then(|c|self._decrypt_cookie(c)).map(|c| c.value().to_string())
|
.and_then(|c| self._decrypt_cookie(c))
|
||||||
|
.map(|c| c.value().to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn logout(&self) -> Cookie{
|
pub fn logout(&self) -> Cookie {
|
||||||
let mut cookie = Cookie::new("ledger-user-id", "");
|
let mut cookie = Cookie::new("ledger-user-id", "");
|
||||||
let epoch = OffsetDateTime::from_unix_timestamp_nanos(0).unwrap();
|
let epoch = OffsetDateTime::from_unix_timestamp_nanos(0).unwrap();
|
||||||
cookie.set_expires(epoch);
|
cookie.set_expires(epoch);
|
||||||
cookie
|
cookie
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
use rusqlite::{Connection};
|
use rusqlite::Connection;
|
||||||
|
|
||||||
pub struct SecurityDTO{
|
pub struct SecurityDTO {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SecurityService<'a> {
|
pub struct SecurityService<'a> {
|
||||||
conn: &'a Connection,
|
conn: &'a Connection,
|
||||||
|
|||||||
+4
-4
@@ -1,13 +1,13 @@
|
|||||||
use rusqlite::Connection;
|
|
||||||
use crate::{CustomResult, Error};
|
use crate::{CustomResult, Error};
|
||||||
|
use rusqlite::Connection;
|
||||||
|
|
||||||
pub use crate::services::_users::UserService;
|
|
||||||
pub use crate::services::_authentication::SessionService;
|
pub use crate::services::_authentication::SessionService;
|
||||||
|
pub use crate::services::_users::UserService;
|
||||||
|
|
||||||
mod _transactions;
|
|
||||||
mod _users;
|
|
||||||
mod _authentication;
|
mod _authentication;
|
||||||
mod _securities;
|
mod _securities;
|
||||||
|
mod _transactions;
|
||||||
|
mod _users;
|
||||||
|
|
||||||
pub use crate::db::ledger_internal::LedgerInternal;
|
pub use crate::db::ledger_internal::LedgerInternal;
|
||||||
|
|
||||||
|
|||||||
+22
-18
@@ -1,15 +1,15 @@
|
|||||||
use std::time::Instant;
|
use actix_web::{web, HttpRequest, Responder};
|
||||||
use actix_web::{HttpRequest, Responder, web};
|
|
||||||
use cookie::{CookieJar, Cookie, Key};
|
|
||||||
use cookie::time::macros::datetime;
|
use cookie::time::macros::datetime;
|
||||||
use cookie::time::OffsetDateTime;
|
use cookie::time::OffsetDateTime;
|
||||||
|
use cookie::{Cookie, CookieJar, Key};
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
use minijinja::context;
|
|
||||||
use crate::webui::MiniJinjaRenderer;
|
|
||||||
use serde::Deserialize;
|
|
||||||
use crate::db::external_id::UserId;
|
use crate::db::external_id::UserId;
|
||||||
use crate::services::ReadOnlyServiceFactory;
|
use crate::services::ReadOnlyServiceFactory;
|
||||||
|
use crate::webui::MiniJinjaRenderer;
|
||||||
use crate::SQLiteConnectionFactory;
|
use crate::SQLiteConnectionFactory;
|
||||||
|
use minijinja::context;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct LoginPayload {
|
struct LoginPayload {
|
||||||
@@ -26,18 +26,21 @@ pub async fn login(
|
|||||||
let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500
|
let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500
|
||||||
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
||||||
let authentication_service = service_factory.get_authentication_service().unwrap();
|
let authentication_service = service_factory.get_authentication_service().unwrap();
|
||||||
let cookie = authentication_service.create_login_cookie(& form.username);
|
let cookie = authentication_service.create_login_cookie(&form.username);
|
||||||
let ctx = context! {
|
let ctx = context! {
|
||||||
name => form.username,
|
name => form.username,
|
||||||
};
|
};
|
||||||
let mut ret = tmpl_env.render("partials/index/success.html", ctx)?.customize();
|
let mut ret = tmpl_env
|
||||||
|
.render("partials/index/success.html", ctx)?
|
||||||
|
.customize();
|
||||||
Ok(ret.insert_header(("Set-Cookie", cookie.to_string())))
|
Ok(ret.insert_header(("Set-Cookie", cookie.to_string())))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::post("/logout")]
|
#[actix_web::post("/logout")]
|
||||||
pub async fn logout(conn_factory: web::Data<SQLiteConnectionFactory>,
|
pub async fn logout(
|
||||||
tmpl_env: MiniJinjaRenderer) -> actix_web::Result<impl Responder> {
|
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 conn = conn_factory.readonly().unwrap(); // todo; to proper 500
|
||||||
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
||||||
let authentication_service = service_factory.get_authentication_service().unwrap();
|
let authentication_service = service_factory.get_authentication_service().unwrap();
|
||||||
@@ -49,19 +52,20 @@ pub async fn logout(conn_factory: web::Data<SQLiteConnectionFactory>,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::get("/")]
|
#[actix_web::get("/")]
|
||||||
pub async fn index(req: HttpRequest,
|
pub async fn index(
|
||||||
conn_factory: web::Data<SQLiteConnectionFactory>,
|
req: HttpRequest,
|
||||||
tmpl_env: MiniJinjaRenderer) -> actix_web::Result<impl Responder> {
|
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 conn = conn_factory.readonly().unwrap(); // todo; to proper 500
|
||||||
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
let mut service_factory = ReadOnlyServiceFactory::new(&mut conn);
|
||||||
let authentication_service = service_factory.get_authentication_service().unwrap();
|
let authentication_service = service_factory.get_authentication_service().unwrap();
|
||||||
|
|
||||||
let userid = authentication_service.get_login(& req);
|
let userid = authentication_service.get_login(&req);
|
||||||
let username = userid.clone().unwrap_or("".to_string());
|
let username = userid.clone().unwrap_or("".to_string());
|
||||||
let ctx = context!{
|
let ctx = context! {
|
||||||
is_logged_in => userid.is_some(),
|
is_logged_in => userid.is_some(),
|
||||||
name => username,
|
name => username,
|
||||||
};
|
};
|
||||||
tmpl_env.render("index.html", ctx)
|
tmpl_env.render("index.html", ctx)
|
||||||
}
|
}
|
||||||
+10
-6
@@ -3,7 +3,13 @@
|
|||||||
use std::{env, path::PathBuf};
|
use std::{env, path::PathBuf};
|
||||||
|
|
||||||
use actix_files;
|
use actix_files;
|
||||||
use actix_web::{dev::ServiceResponse, error, http::{header::ContentType, StatusCode}, middleware::{ErrorHandlerResponse, ErrorHandlers, Logger}, web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder, Result, Scope};
|
use actix_web::{
|
||||||
|
dev::ServiceResponse,
|
||||||
|
error,
|
||||||
|
http::{header::ContentType, StatusCode},
|
||||||
|
middleware::{ErrorHandlerResponse, ErrorHandlers, Logger},
|
||||||
|
web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder, Result, Scope,
|
||||||
|
};
|
||||||
use actix_web_lab::respond::Html;
|
use actix_web_lab::respond::Html;
|
||||||
use async_channel;
|
use async_channel;
|
||||||
use futures::{pin_mut, select, FutureExt};
|
use futures::{pin_mut, select, FutureExt};
|
||||||
@@ -13,7 +19,7 @@ use rusqlite::Connection;
|
|||||||
|
|
||||||
use crate::events::Event;
|
use crate::events::Event;
|
||||||
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
|
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
|
||||||
use crate::webui::worker::{event_processor_loop};
|
use crate::webui::worker::event_processor_loop;
|
||||||
|
|
||||||
mod index_views;
|
mod index_views;
|
||||||
mod services;
|
mod services;
|
||||||
@@ -71,13 +77,11 @@ fn create_auto_reloader(enable_template_autoreload: bool) -> AutoReloader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn create_static_scope() -> Scope {
|
fn create_static_scope() -> Scope {
|
||||||
web::scope("/static").service(
|
web::scope("/static").service(actix_files::Files::new("/", "./resources/static/"))
|
||||||
actix_files::Files::new("/", "./resources/static/"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_api_scope() -> Scope {
|
fn create_api_scope() -> Scope {
|
||||||
web::scope("/v1")
|
web::scope("/v1").service(views::push_event)
|
||||||
.service(views::push_event)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_views_scope() -> Scope {
|
fn create_views_scope() -> Scope {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
use crate::services::{ReadOnlyServiceFactory, SessionService, UserService};
|
||||||
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
|
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
|
||||||
use crate::services::{SessionService, ReadOnlyServiceFactory, UserService};
|
|
||||||
|
|
||||||
struct ServicesCache {
|
struct ServicesCache {
|
||||||
session_service: SessionService,
|
session_service: SessionService,
|
||||||
@@ -13,13 +13,13 @@ struct Services {
|
|||||||
impl Services {
|
impl Services {
|
||||||
pub fn new(conn_fact: SQLiteConnectionFactory) -> Services {
|
pub fn new(conn_fact: SQLiteConnectionFactory) -> Services {
|
||||||
let mut tmp_conn = conn_fact.connection().unwrap();
|
let mut tmp_conn = conn_fact.connection().unwrap();
|
||||||
let session_service = SessionService::from_conn(&mut tmp_conn).unwrap();
|
let session_service = SessionService::from_conn(&mut tmp_conn).unwrap();
|
||||||
let cache = ServicesCache{session_service};
|
let cache = ServicesCache { session_service };
|
||||||
Services { conn_fact, cache }
|
Services { conn_fact, cache }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_authentication_service(& self) -> &SessionService {
|
pub fn get_authentication_service(&self) -> &SessionService {
|
||||||
& self.cache.session_service
|
&self.cache.session_service
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_user_service(&self) -> i32 {
|
pub fn get_user_service(&self) -> i32 {
|
||||||
@@ -28,4 +28,4 @@ impl Services {
|
|||||||
// service_factory.get_user_service()
|
// service_factory.get_user_service()
|
||||||
88
|
88
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,4 +25,3 @@ pub async fn event_processor_loop(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user