Skip to content

Commit

Permalink
Fix deleting expired session_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-lloyd03 committed Apr 8, 2024
1 parent fda366e commit 7448cf2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
27 changes: 27 additions & 0 deletions flake.lock

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

15 changes: 15 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
description = "Rudric, the keeper of secrets";

inputs = { nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; };

outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
devShells.${system}.default = pkgs.mkShell {
packages = with pkgs; [ cargo rustc rust-analyzer rustfmt sqlite ];
};
};
}
11 changes: 9 additions & 2 deletions src/types/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{env, fmt::Display};
use anyhow::{bail, Context, Result};
use base64::{engine::general_purpose::STANDARD_NO_PAD as b64, Engine};
use orion::aead::SecretKey;
use sqlx::{sqlite::SqliteRow, FromRow, Row, SqlitePool};
use sqlx::{sqlite::SqliteRow, Execute, FromRow, Row, SqlitePool};
use time::OffsetDateTime;
use uuid::Uuid;

Expand Down Expand Up @@ -91,13 +91,16 @@ impl SessionKey {
}

pub async fn delete_expired(db: &SqlitePool) -> Result<()> {
let now = OffsetDateTime::now_utc().unix_timestamp();
let now = OffsetDateTime::now_utc();

sqlx::query!("delete from session_keys where expire_time < ?", now)
.execute(db)
.await
.context("Failed to delete expired session key")?;

println!("{}",sqlx::query!("delete from session_keys where expire_time < ?", now).sql());


Ok(())
}
}
Expand Down Expand Up @@ -141,6 +144,10 @@ impl SessionToken {
// The session key ID is prepended to the encrypted timed key.
let session_token = [session_key.id.as_bytes(), encrypted_timed_key.as_slice()].concat();

if let Err(e) = SessionKey::delete_expired(db).await {
eprintln!("Error deleting expired session tokens: {e}");
}

Ok(Self(b64.encode(session_token)))
}

Expand Down

0 comments on commit 7448cf2

Please sign in to comment.