summaryrefslogtreecommitdiff
path: root/src/token
diff options
context:
space:
mode:
authorKit La Touche <kit@transneptune.net>2025-02-21 22:18:56 -0500
committerKit La Touche <kit@transneptune.net>2025-02-21 22:53:49 -0500
commit9d1dbac74866a6175c65a25bbd8a3ccbe8cf87e4 (patch)
treef15b3f0695b948e335774aa4d92a5b064a1c0f10 /src/token
parent743b59b69857da81b214970ec9252bc918ad243d (diff)
parent36cadfe00cacc6a6523f9862d3f7a08a9d0ce611 (diff)
Merge branch 'main' into prop/preserve-state
Diffstat (limited to 'src/token')
-rw-r--r--src/token/app.rs14
-rw-r--r--src/token/extract/cookie.rs3
-rw-r--r--src/token/extract/identity.rs18
-rw-r--r--src/token/repo/auth.rs8
-rw-r--r--src/token/repo/token.rs6
5 files changed, 31 insertions, 18 deletions
diff --git a/src/token/app.rs b/src/token/app.rs
index 5c0aeb0..3f054ff 100644
--- a/src/token/app.rs
+++ b/src/token/app.rs
@@ -1,19 +1,18 @@
use chrono::TimeDelta;
use futures::{
- future,
+ Stream, future,
stream::{self, StreamExt as _},
- Stream,
};
use sqlx::sqlite::SqlitePool;
use super::{
- repo::{self, auth::Provider as _, Provider as _},
Broadcaster, Event as TokenEvent, Id, Secret,
+ repo::{self, Provider as _, auth::Provider as _},
};
use crate::{
clock::DateTime,
db::NotFound as _,
- login::{repo::Provider as _, Login, Password},
+ login::{Login, Password, repo::Provider as _},
name::{self, Name},
};
@@ -120,12 +119,13 @@ impl<'a> Tokens<'a> {
Ok((token, login))
}
- pub async fn limit_stream<E>(
+ pub async fn limit_stream<S, E>(
&self,
token: Id,
- events: impl Stream<Item = E> + std::fmt::Debug,
- ) -> Result<impl Stream<Item = E> + std::fmt::Debug, ValidateError>
+ events: S,
+ ) -> Result<impl Stream<Item = E> + std::fmt::Debug + use<S, E>, ValidateError>
where
+ S: Stream<Item = E> + std::fmt::Debug,
E: std::fmt::Debug,
{
// Subscribe, first.
diff --git a/src/token/extract/cookie.rs b/src/token/extract/cookie.rs
index af5787d..a8e15d4 100644
--- a/src/token/extract/cookie.rs
+++ b/src/token/extract/cookie.rs
@@ -25,7 +25,7 @@ impl fmt::Debug for Identity {
}
impl Identity {
- const COOKIE_NAME: &str = "identity";
+ const COOKIE_NAME: &'static str = "identity";
// Creates a new, unpopulated identity token store.
#[cfg(test)]
@@ -71,7 +71,6 @@ impl Identity {
}
}
-#[async_trait::async_trait]
impl<S> FromRequestParts<S> for Identity
where
S: Send + Sync,
diff --git a/src/token/extract/identity.rs b/src/token/extract/identity.rs
index a69f509..acfd7ae 100644
--- a/src/token/extract/identity.rs
+++ b/src/token/extract/identity.rs
@@ -1,5 +1,5 @@
use axum::{
- extract::{FromRequestParts, State},
+ extract::{FromRequestParts, OptionalFromRequestParts, State},
http::request::Parts,
response::{IntoResponse, Response},
};
@@ -20,7 +20,6 @@ pub struct Identity {
pub login: Login,
}
-#[async_trait::async_trait]
impl FromRequestParts<App> for Identity {
type Rejection = LoginError<Internal>;
@@ -39,6 +38,21 @@ impl FromRequestParts<App> for Identity {
}
}
+impl OptionalFromRequestParts<App> for Identity {
+ type Rejection = LoginError<Internal>;
+
+ async fn from_request_parts(
+ parts: &mut Parts,
+ state: &App,
+ ) -> Result<Option<Self>, Self::Rejection> {
+ match <Self as FromRequestParts<App>>::from_request_parts(parts, state).await {
+ Ok(identity) => Ok(Some(identity)),
+ Err(LoginError::Unauthorized) => Ok(None),
+ Err(other) => Err(other),
+ }
+ }
+}
+
pub enum LoginError<E> {
Failure(E),
Unauthorized,
diff --git a/src/token/repo/auth.rs b/src/token/repo/auth.rs
index b51db8c..0deed10 100644
--- a/src/token/repo/auth.rs
+++ b/src/token/repo/auth.rs
@@ -1,10 +1,10 @@
-use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
+use sqlx::{SqliteConnection, Transaction, sqlite::Sqlite};
use crate::{
clock::DateTime,
db::NotFound,
event::{Instant, Sequence},
- login::{self, password::StoredHash, History, Login},
+ login::{self, History, Login, password::StoredHash},
name::{self, Name},
};
@@ -12,7 +12,7 @@ pub trait Provider {
fn auth(&mut self) -> Auth;
}
-impl<'c> Provider for Transaction<'c, Sqlite> {
+impl Provider for Transaction<'_, Sqlite> {
fn auth(&mut self) -> Auth {
Auth(self)
}
@@ -20,7 +20,7 @@ impl<'c> Provider for Transaction<'c, Sqlite> {
pub struct Auth<'t>(&'t mut SqliteConnection);
-impl<'t> Auth<'t> {
+impl Auth<'_> {
pub async fn for_name(&mut self, name: &Name) -> Result<(History, StoredHash), LoadError> {
let name = name.canonical();
let row = sqlx::query!(
diff --git a/src/token/repo/token.rs b/src/token/repo/token.rs
index 33b89d5..ff42fad 100644
--- a/src/token/repo/token.rs
+++ b/src/token/repo/token.rs
@@ -1,4 +1,4 @@
-use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
+use sqlx::{SqliteConnection, Transaction, sqlite::Sqlite};
use uuid::Uuid;
use crate::{
@@ -14,7 +14,7 @@ pub trait Provider {
fn tokens(&mut self) -> Tokens;
}
-impl<'c> Provider for Transaction<'c, Sqlite> {
+impl Provider for Transaction<'_, Sqlite> {
fn tokens(&mut self) -> Tokens {
Tokens(self)
}
@@ -22,7 +22,7 @@ impl<'c> Provider for Transaction<'c, Sqlite> {
pub struct Tokens<'t>(&'t mut SqliteConnection);
-impl<'c> Tokens<'c> {
+impl Tokens<'_> {
// Issue a new token for an existing login. The issued_at timestamp will
// be used to control expiry, until the token is actually used.
pub async fn issue(