summaryrefslogtreecommitdiffstats
path: root/src/jwt.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-04-08 16:31:44 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-04-11 23:49:41 -0500
commitd9ed52fc239e3547eb99fe03bd296ab2808d2ebc (patch)
tree2fdc8a0e33bdf0902f608daa8e41d61df80ea9b2 /src/jwt.rs
parent9a6c04d52edb10431f9f5ca2dbc83c410cb5daee (diff)
wip: impl jwt handling
Diffstat (limited to 'src/jwt.rs')
-rw-r--r--src/jwt.rs51
1 files changed, 0 insertions, 51 deletions
diff --git a/src/jwt.rs b/src/jwt.rs
deleted file mode 100644
index 6382a01..0000000
--- a/src/jwt.rs
+++ /dev/null
@@ -1,51 +0,0 @@
-use std::sync::Arc;
-
-use axum::extract::{Request, State};
-use axum_extra::{
- headers::{authorization::Bearer, Authorization},
- TypedHeader,
-};
-use jsonwebtoken::{DecodingKey, Validation};
-use serde::{Deserialize, Serialize};
-use uuid::Uuid;
-
-use crate::{error::AuthError, state::AppState};
-
-#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
-pub struct Claims {
- pub sub: Uuid,
- pub iat: i64,
- pub exp: i64,
-}
-
-impl Claims {
- pub fn new(sub: Uuid, max_age: time::Duration) -> Self {
- let iat = time::OffsetDateTime::now_utc().unix_timestamp();
- let exp = iat + max_age.whole_seconds();
- Self { sub, iat, exp }
- }
-
- pub fn encode(&self, secret: &[u8]) -> Result<String, jsonwebtoken::errors::Error> {
- jsonwebtoken::encode(
- &jsonwebtoken::Header::default(),
- self,
- &jsonwebtoken::EncodingKey::from_secret(secret),
- )
- }
-}
-
-pub async fn authenticate(
- State(state): State<Arc<AppState>>,
- TypedHeader(Authorization(bearer)): TypedHeader<Authorization<Bearer>>,
- mut req: Request,
-) -> Result<Request, AuthError> {
- let claims = jsonwebtoken::decode::<Claims>(
- bearer.token(),
- &DecodingKey::from_secret(state.jwt_secret.as_ref()),
- &Validation::default(),
- )?
- .claims;
-
- req.extensions_mut().insert(claims);
- Ok(req)
-}