Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e50b12be16
|
||
|
|
e7d62a8f11
|
||
|
|
1676951409
|
||
|
|
8317f41e3c | ||
|
|
ba998096cb |
Generated
+1
-1
@@ -522,7 +522,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "textsender_models"
|
||||
version = "0.3.1"
|
||||
version = "0.3.3"
|
||||
dependencies = [
|
||||
"const_format",
|
||||
"dotenvy",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "textsender_models"
|
||||
version = "0.3.1"
|
||||
version = "0.3.3"
|
||||
edition = "2024"
|
||||
rust-version = "1.95"
|
||||
description = "Models used for the textsender project"
|
||||
|
||||
+26
-4
@@ -7,11 +7,32 @@ pub struct Claims {
|
||||
#[serde(alias = "iss")]
|
||||
pub issued: String,
|
||||
#[serde(alias = "exp")]
|
||||
#[serde(deserialize_with = "deserialize_i64_from_f64")]
|
||||
pub expired: i64,
|
||||
#[serde(alias = "iat")]
|
||||
#[serde(deserialize_with = "deserialize_i64_from_f64")]
|
||||
pub issued_at: i64,
|
||||
}
|
||||
|
||||
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) {
|
||||
Err(serde::de::Error::custom("float out of i64 range"))
|
||||
} else {
|
||||
Ok(rounded as i64)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct LoginResult {
|
||||
pub user_id: uuid::Uuid,
|
||||
@@ -21,7 +42,7 @@ pub struct LoginResult {
|
||||
pub issued_at: i64,
|
||||
}
|
||||
|
||||
// Alias for LoginResult
|
||||
/// Alias for LoginResult
|
||||
pub type Login = LoginResult;
|
||||
|
||||
pub fn get_issued() -> time::Result<time::OffsetDateTime> {
|
||||
@@ -42,9 +63,10 @@ pub struct TokenResource {
|
||||
pub message: String,
|
||||
pub issuer: String,
|
||||
pub audiences: Vec<String>,
|
||||
pub id: uuid::Uuid,
|
||||
pub user_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
/// Token type
|
||||
pub const TOKEN_TYPE: &str = "JWT";
|
||||
|
||||
pub fn create_token(
|
||||
@@ -62,8 +84,8 @@ pub fn create_token(
|
||||
payload.set_subject(message);
|
||||
payload.set_issuer(issuer);
|
||||
payload.set_audience(audiences.clone());
|
||||
if !token_resource.id.is_nil() {
|
||||
match payload.set_claim("id", Some(serde_json::json!(token_resource.id))) {
|
||||
if !token_resource.user_id.is_nil() {
|
||||
match payload.set_claim("user_id", Some(serde_json::json!(token_resource.user_id))) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
return Err(err);
|
||||
|
||||
Reference in New Issue
Block a user