feat: implement session last_seen update
parent
330877c8c5
commit
f00d842bad
|
@ -181,4 +181,20 @@ impl gpodder::AuthStore for SqliteRepository {
|
||||||
.execute(&mut self.pool.get()?)
|
.execute(&mut self.pool.get()?)
|
||||||
.map(|_| ())?)
|
.map(|_| ())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn refresh_session(
|
||||||
|
&self,
|
||||||
|
session: &gpodder::Session,
|
||||||
|
timestamp: DateTime<chrono::Utc>,
|
||||||
|
) -> Result<(), AuthErr> {
|
||||||
|
if diesel::update(sessions::table.filter(sessions::id.eq(session.id)))
|
||||||
|
.set(sessions::last_seen.eq(timestamp.timestamp()))
|
||||||
|
.execute(&mut self.pool.get()?)?
|
||||||
|
== 0
|
||||||
|
{
|
||||||
|
Err(AuthErr::UnknownSession)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,9 @@ pub trait AuthStore {
|
||||||
|
|
||||||
/// Remove the session with the given session ID
|
/// Remove the session with the given session ID
|
||||||
fn remove_session(&self, session_id: i64) -> Result<(), AuthErr>;
|
fn remove_session(&self, session_id: i64) -> Result<(), AuthErr>;
|
||||||
|
|
||||||
|
/// Update the session's timestamp
|
||||||
|
fn refresh_session(&self, session: &Session, timestamp: DateTime<Utc>) -> Result<(), AuthErr>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait DeviceRepository {
|
pub trait DeviceRepository {
|
||||||
|
|
|
@ -20,7 +20,7 @@ impl GpodderRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_session(&self, session_id: i64) -> Result<models::Session, AuthErr> {
|
pub fn get_session(&self, session_id: i64) -> Result<models::Session, AuthErr> {
|
||||||
let session = self
|
let session = self
|
||||||
.store
|
.store
|
||||||
.get_session(session_id)?
|
.get_session(session_id)?
|
||||||
|
@ -65,6 +65,12 @@ impl GpodderRepository {
|
||||||
Ok(session)
|
Ok(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn refresh_session(&self, session: &models::Session) -> Result<(), AuthErr> {
|
||||||
|
let now = Utc::now();
|
||||||
|
|
||||||
|
self.store.refresh_session(session, now)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn remove_session(&self, session_id: i64) -> Result<(), AuthErr> {
|
pub fn remove_session(&self, session_id: i64) -> Result<(), AuthErr> {
|
||||||
self.store.remove_session(session_id)
|
self.store.remove_session(session_id)
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,10 +47,15 @@ pub async fn auth_middleware(State(ctx): State<Context>, mut req: Request, next:
|
||||||
.get(SESSION_ID_COOKIE)
|
.get(SESSION_ID_COOKIE)
|
||||||
.and_then(|c| c.value().parse::<i64>().ok())
|
.and_then(|c| c.value().parse::<i64>().ok())
|
||||||
{
|
{
|
||||||
let ctx_clone = ctx.clone();
|
let ctx = ctx.clone();
|
||||||
match tokio::task::spawn_blocking(move || ctx_clone.store.validate_session(session_id))
|
match tokio::task::spawn_blocking(move || {
|
||||||
.await
|
let session = ctx.store.get_session(session_id)?;
|
||||||
.unwrap()
|
ctx.store.refresh_session(&session)?;
|
||||||
|
|
||||||
|
Ok(session)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
{
|
{
|
||||||
Ok(session) => {
|
Ok(session) => {
|
||||||
auth_user = Some(session.user);
|
auth_user = Some(session.user);
|
||||||
|
|
Loading…
Reference in New Issue