Saving changes
Rust Build / Rustfmt (pull_request) Failing after 32s
Rust Build / Clippy (pull_request) Failing after 1m34s
Rust Build / Check (pull_request) Successful in 2m17s

This commit is contained in:
2026-06-22 23:40:11 -04:00
parent 1a68f48315
commit a9b41b12dd
2 changed files with 29 additions and 51 deletions
+23 -45
View File
@@ -189,10 +189,10 @@ pub async fn user_login(
let key = textsender_models::envy::environment::get_secret_key()
.await
.value;
let (token_literal, duration) =
let cst =
token_stuff::create_token(&key, &user.id).unwrap();
if token_stuff::verify_token(&key, &token_literal) {
if token_stuff::verify_token(&key, &cst.access_token) {
let current_time = time::OffsetDateTime::now_utc();
let _ =
repo::user::update_last_login(&pool, &user, &current_time)
@@ -204,12 +204,12 @@ pub async fn user_login(
message: String::from("Successful"),
data: vec![textsender_models::token::LoginResult {
user_id: user.id,
access_token: token_literal,
access_token: cst.access_token,
token_type: String::from(
textsender_models::token::TOKEN_TYPE,
),
issued_at: duration,
..Default::default()
issued_at: cst.issued,
expires_in: cst.expires_in,
}],
}),
)
@@ -330,10 +330,10 @@ pub async fn service_user_login(
let key = textsender_models::envy::environment::get_secret_key()
.await
.value;
let (token_literal, duration) =
let cst =
token_stuff::create_token(&key, &service_user.id).unwrap();
if token_stuff::verify_token(&key, &token_literal) {
if token_stuff::verify_token(&key, &cst.access_token) {
let current_time = time::OffsetDateTime::now_utc();
let _ = repo::service::update_last_login(
&pool,
@@ -350,12 +350,12 @@ pub async fn service_user_login(
),
data: vec![textsender_models::token::LoginResult {
user_id: service_user.id,
access_token: token_literal,
access_token: cst.access_token,
token_type: String::from(
textsender_models::token::TOKEN_TYPE,
),
issued_at: duration,
..Default::default()
issued_at: cst.issued,
expires_in: cst.expires_in
}],
}),
)
@@ -440,10 +440,10 @@ pub async fn refresh_token(
if token_stuff::verify_token(&key, &payload.access_token) {
match token_stuff::extract_id_from_token(&key, &payload.access_token) {
Ok(id) => {
let generate_service_token = |id, key| -> (Option<String>, Option<i64>) {
let generate_service_token = |id, key| -> Option<textsender_models::token::CreateTokenResult> {
match token_stuff::create_service_refresh_token(key, id) {
Ok((token, issued)) => (Some(token), Some(issued)),
Err(_err) => (None, None),
Ok(cst) => Some(cst),
Err(_err) => None,
}
};
@@ -454,31 +454,21 @@ pub async fn refresh_token(
match repo::user::get_with_id(&pool, &id).await {
Ok(_user) => {
let (refresh_token, issued) = generate_service_token(&id, &key);
match refresh_token {
Some(token) => match issued {
Some(issued_at) => {
match generate_service_token(&id, &key) {
Some(cst) => {
response.message =
String::from(super::messages::SUCCESSFUL_MESSAGE);
let lr = textsender_models::token::LoginResult {
user_id: id,
access_token: token,
issued_at,
access_token: cst.access_token,
issued_at: cst.issued,
expires_in: cst.expires_in,
token_type: String::from(
textsender_models::token::TOKEN_TYPE,
),
..Default::default()
};
response.data.push(lr);
(axum::http::StatusCode::OK, axum::Json(response))
}
None => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: String::from("Issued at not returned"),
data: Vec::new(),
}),
),
},
None => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
@@ -493,21 +483,19 @@ pub async fn refresh_token(
println!("Unable to find user, checking service user: {err:?}");
match repo::service::get(&pool, &id).await {
Ok(_service_user) => {
let (refresh_token, issued) = generate_service_token(&id, &key);
match refresh_token {
Some(token) => match issued {
Some(issued_at) => {
match generate_service_token(&id, &key) {
Some(cst) => {
response.message = String::from(
super::messages::SUCCESSFUL_MESSAGE,
);
let lr = textsender_models::token::LoginResult {
user_id: id,
access_token: token,
issued_at,
access_token: cst.access_token,
issued_at: cst.issued,
expires_in: cst.expires_in,
token_type: String::from(
textsender_models::token::TOKEN_TYPE,
),
..Default::default()
};
response.data.push(lr);
(axum::http::StatusCode::OK, axum::Json(response))
@@ -519,16 +507,6 @@ pub async fn refresh_token(
data: Vec::new(),
}),
),
},
None => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::RefreshTokenResponse {
message: String::from(
"Refresh token not generated",
),
data: Vec::new(),
}),
),
}
}
Err(err) => (
+5 -5
View File
@@ -19,7 +19,7 @@ pub fn get_expiration(issued: &time::OffsetDateTime) -> Result<time::OffsetDateT
pub fn create_token(
provided_key: &String,
id: &uuid::Uuid,
) -> Result<(String, i64), josekit::JoseError> {
) -> Result<textsender_models::token::CreateTokenResult, josekit::JoseError> {
let resource = textsender_models::token::TokenResource {
message: String::from(MESSAGE),
issuer: String::from(ISSUER),
@@ -32,7 +32,7 @@ pub fn create_token(
pub fn create_service_token(
provided: &String,
id: &uuid::Uuid,
) -> Result<(String, i64), josekit::JoseError> {
) -> Result<textsender_models::token::CreateTokenResult, josekit::JoseError> {
let resource = textsender_models::token::TokenResource {
message: String::from(SERVICE_SUBJECT),
issuer: String::from(ISSUER),
@@ -45,7 +45,7 @@ pub fn create_service_token(
pub fn create_service_refresh_token(
key: &String,
id: &uuid::Uuid,
) -> Result<(String, i64), josekit::JoseError> {
) -> Result<textsender_models::token::CreateTokenResult, josekit::JoseError> {
let resource = textsender_models::token::TokenResource {
message: String::from(SERVICE_SUBJECT),
issuer: String::from(ISSUER),
@@ -125,8 +125,8 @@ mod tests {
.value;
let id = uuid::Uuid::new_v4();
match create_token(&special_key, &id) {
Ok((token, _duration)) => {
let result = verify_token(&special_key, &token);
Ok(cst) => {
let result = verify_token(&special_key, &cst.access_token);
assert!(result, "Token not verified");
}
Err(err) => {