Skip to content

Commit

Permalink
Improve session handling (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Syfaro authored Feb 29, 2024
1 parent e95bf11 commit e7d65b6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 19 deletions.
6 changes: 6 additions & 0 deletions jobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ jobs:
queue: fuzzysearch_owo_core
custom:
initiator: schedule
- name: remove expired sessions
every: "30 * * * *"
job_type: remove_expired_sessions
queue: fuzzysearch_owo_core
custom:
initiator: schedule
4 changes: 4 additions & 0 deletions queries/user_session/remove_expired.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DELETE FROM
user_session
WHERE
last_used < current_timestamp - interval '30 days';
10 changes: 10 additions & 0 deletions sqlx-data.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ impl Job for ToggleSiteAccounts {
}
}

#[derive(Serialize, Deserialize)]
struct MigrateOwnedMediaAccounts;

impl Job for MigrateOwnedMediaAccounts {
Expand All @@ -446,6 +445,30 @@ impl Job for MigrateOwnedMediaAccounts {
}
}

struct RemoveExpiredSessions;

impl Job for RemoveExpiredSessions {
const NAME: &'static str = "remove_expired_sessions";
type Data = ();
type Queue = Queue;

fn queue(&self) -> Self::Queue {
Queue::Core
}

fn extra(&self) -> Result<Option<JobExtra>, serde_json::Error> {
Ok(None)
}

fn args(self) -> Result<Vec<serde_json::Value>, serde_json::Error> {
Ok(vec![])
}

fn deserialize(_args: Vec<serde_json::Value>) -> Result<Self::Data, serde_json::Error> {
Ok(())
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum JobInitiator {
Expand Down Expand Up @@ -1121,6 +1144,14 @@ pub async fn start_job_processing(ctx: JobContext) -> Result<(), Error> {
Ok(())
});

RemoveExpiredSessions::register(&mut forge, |cx, _job, _args| async move {
sqlx::query_file!("queries/user_session/remove_expired.sql")
.execute(&cx.conn)
.await?;

Ok(())
});

let mut client = forge.finalize();

client.labels(labels);
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,10 @@ async fn main() {
.cookie_http_only(true)
.session_lifecycle(SessionLifecycle::PersistentSession(
PersistentSession::default()
.session_ttl(actix_web::cookie::time::Duration::days(365)),
.session_ttl(actix_web::cookie::time::Duration::days(30))
.session_ttl_extension_policy(
actix_session::config::TtlExtensionPolicy::OnEveryRequest,
),
))
.build();

Expand Down
34 changes: 17 additions & 17 deletions templates/user/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,6 @@ <h2 class="subtitle is-4">Security</h2>
completing the forgot password process.
</p>

<div class="field mt-5">
<h3 class="subtitle is-5">
<span class="icon-text">
<span class="icon">
<i class="bi bi-person-fill-lock"></i>
</span>
<span>Sessions</span>
</span>
</h3>

<div class="control">
<a class="button is-link is-outlined" href="/auth/sessions">
Active Sessions
</a>
</div>
</div>

{% if passkeys_enabled %}
<div class="field mt-5">
<h3 class="subtitle is-5 mt-3">
Expand Down Expand Up @@ -257,6 +240,23 @@ <h3 class="subtitle is-5 mt-3">
{% endif %}
</div>
{% endif %}

<div class="field mt-5">
<h3 class="subtitle is-5">
<span class="icon-text">
<span class="icon">
<i class="bi bi-person-fill-lock"></i>
</span>
<span>Sessions</span>
</span>
</h3>

<div class="control">
<a class="button is-link is-outlined" href="/auth/sessions">
Active Sessions
</a>
</div>
</div>
</div>

<div class="block mt-6">
Expand Down

0 comments on commit e7d65b6

Please sign in to comment.