tsk-230: Using UserClaims from icarus_models #243
Generated
+1
-1
@@ -964,7 +964,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icarus"
|
name = "icarus"
|
||||||
version = "0.3.25"
|
version = "0.3.26"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"axum-extra",
|
"axum-extra",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "icarus"
|
name = "icarus"
|
||||||
version = "0.3.25"
|
version = "0.3.26"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.90"
|
rust-version = "1.90"
|
||||||
|
|
||||||
|
|||||||
+2
-38
@@ -6,44 +6,8 @@ use axum::{
|
|||||||
};
|
};
|
||||||
use axum_extra::extract::cookie::CookieJar;
|
use axum_extra::extract::cookie::CookieJar;
|
||||||
use jsonwebtoken::{DecodingKey, Validation, decode};
|
use jsonwebtoken::{DecodingKey, Validation, decode};
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
// TODO: Put this in icarus_models
|
#[derive(Debug, serde::Serialize)]
|
||||||
fn deserialize_i64_from_f64<'de, D>(deserializer: D) -> Result<i64, D::Error>
|
|
||||||
where
|
|
||||||
D: serde::Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let val = f64::deserialize(deserializer)?;
|
|
||||||
// Handle NaN and infinity cases
|
|
||||||
if val.is_nan() || val.is_infinite() {
|
|
||||||
return Err(serde::de::Error::custom("invalid float value"));
|
|
||||||
}
|
|
||||||
// Round to nearest integer and convert
|
|
||||||
let rounded = val.round();
|
|
||||||
// Check if the rounded value can fit in i64
|
|
||||||
if rounded < (i64::MIN as f64) || rounded > (i64::MAX as f64) {
|
|
||||||
return Err(serde::de::Error::custom("float out of i64 range"));
|
|
||||||
}
|
|
||||||
Ok(rounded as i64)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Put this in icarus_models
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
pub struct UserClaims {
|
|
||||||
pub iss: String,
|
|
||||||
pub aud: String, // Audience
|
|
||||||
pub sub: String, // Subject (user ID)
|
|
||||||
#[serde(deserialize_with = "deserialize_i64_from_f64")]
|
|
||||||
pub exp: i64, // Expiration time (UTC timestamp)
|
|
||||||
#[serde(deserialize_with = "deserialize_i64_from_f64")]
|
|
||||||
pub iat: i64, // Issued at (UTC timestamp)
|
|
||||||
// pub azp: String,
|
|
||||||
// pub gty: String,
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
pub roles: Option<Vec<String>>, // Optional roles
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
|
||||||
pub struct ErrorResponse {
|
pub struct ErrorResponse {
|
||||||
pub status: &'static str,
|
pub status: &'static str,
|
||||||
pub message: String,
|
pub message: String,
|
||||||
@@ -76,7 +40,7 @@ pub async fn auth<B>(
|
|||||||
|
|
||||||
let mut validation = Validation::new(jsonwebtoken::Algorithm::HS256);
|
let mut validation = Validation::new(jsonwebtoken::Algorithm::HS256);
|
||||||
validation.set_audience(&["icarus"]); // Must match exactly what's in the token
|
validation.set_audience(&["icarus"]); // Must match exactly what's in the token
|
||||||
let _claims = decode::<UserClaims>(
|
let _claims = decode::<icarus_models::token::UserClaims>(
|
||||||
&token,
|
&token,
|
||||||
&DecodingKey::from_secret(secret_key.as_ref()),
|
&DecodingKey::from_secret(secret_key.as_ref()),
|
||||||
&validation,
|
&validation,
|
||||||
|
|||||||
Reference in New Issue
Block a user