From 2e4c289998a225424feb31c6feaf892d33af21f0 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 10 Aug 2025 17:19:57 -0400 Subject: [PATCH] tsk-51: Functionality of endpoint is almost done --- src/callers/login.rs | 38 +++++++++++++++++++++++++++++++++- src/repo/mod.rs | 24 ++++++++++++++++++++- src/token_stuff/mod.rs | 47 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 104 insertions(+), 5 deletions(-) diff --git a/src/callers/login.rs b/src/callers/login.rs index c171218..e58e077 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -169,7 +169,43 @@ pub mod endpoint { if token_stuff::is_token_type_valid(&token_type) { // let (token_literal, dur) = token_stuff::create_service_refresh_token(&key, ) - (axum::http::StatusCode::OK, axum::Json(response)) + // Get passphrase record with id + // match repo::service::get_passphrase + match token_stuff::extract_id_from_token(&key, &payload.access_token) { + Ok(id) => { + match repo::service::get_passphrase(&pool, &id).await { + Ok((returned_id, _, _)) => { + match token_stuff::create_service_refresh_token(&key, &returned_id) + { + Ok((access_token, exp_dur)) => { + let login_result = + icarus_models::login_result::LoginResult { + id: returned_id, + token: access_token, + expiration: exp_dur, + token_type: String::from( + icarus_models::token::TOKEN_TYPE, + ), + username: String::from("service"), + }; + } + Err(err) => {} + } + /* + */ + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::OK, axum::Json(response)) + } + } + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::OK, axum::Json(response)) + } + } } else { response.message = String::from("Invalid token type"); (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) diff --git a/src/repo/mod.rs b/src/repo/mod.rs index e959406..189ebbe 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -225,5 +225,27 @@ pub mod service { } // TODO: Write a function to get a passphrase record with an id - // pub async fn get_passphrase(pool: &) + pub async fn get_passphrase( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result<(uuid::Uuid, String, time::OffsetDateTime), sqlx::Error> { + let result = sqlx::query( + r#" + SELECT * FROM "passphrase" WHERE id = $1; + "#, + ) + .bind(id) + .fetch_one(pool) + .await; + + match result { + Ok(row) => { + let returned_id: uuid::Uuid = row.try_get("id")?; + let passphrase: String = row.try_get("passphrase")?; + let date_created: time::OffsetDateTime = row.try_get("date_created")?; + Ok((returned_id, passphrase, date_created)) + } + Err(err) => Err(err), + } + } } diff --git a/src/token_stuff/mod.rs b/src/token_stuff/mod.rs index f14848b..38f04d7 100644 --- a/src/token_stuff/mod.rs +++ b/src/token_stuff/mod.rs @@ -38,7 +38,7 @@ pub fn create_service_token( id: &uuid::Uuid, ) -> Result<(String, i64), josekit::JoseError> { let resource = icarus_models::token::TokenResource { - message: String::from("Service random"), + message: String::from(SERVICE_SUBJECT), issuer: String::from(ISSUER), audiences: vec![String::from(AUDIENCE)], id: *id, @@ -46,6 +46,19 @@ pub fn create_service_token( icarus_models::token::create_token(provided, &resource, time::Duration::hours(1)) } +pub fn create_service_refresh_token( + key: &String, + id: &uuid::Uuid, +) -> Result<(String, i64), josekit::JoseError> { + let resource = icarus_models::token::TokenResource { + message: String::from(SERVICE_SUBJECT), + issuer: String::from(ISSUER), + audiences: vec![String::from(AUDIENCE)], + id: *id, + }; + icarus_models::token::create_token(key, &resource, time::Duration::hours(4)) +} + pub fn verify_token(key: &String, token: &String) -> bool { match get_payload(key, token) { Ok((payload, _header)) => match payload.subject() { @@ -69,12 +82,40 @@ pub fn extract_id_from_token(key: &String, token: &String) -> Result Result { - Ok(String::new()) + match get_payload(key, token) { + Ok((payload, _header)) => match payload.subject() { + Some(subject) => { + if subject == APP_SUBJECT { + Ok(String::from(APP_TOKEN_TYPE)) + } else if subject == SERVICE_SUBJECT { + Ok(String::from(SERVICE_TOKEN_TYPE)) + } else { + Err(std::io::Error::other(String::from("Invalid subject"))) + } + } + None => Err(std::io::Error::other(String::from("Invalid payload"))), // } + /* + match payload.claim("id") { + Some(id) => match uuid::Uuid::parse_str(id.as_str().unwrap()) { + Ok(extracted) => Ok(extracted), + Err(err) => Err(std::io::Error::other(err.to_string())), + }, + None => Err(std::io::Error::other("No claim found")), + */ + }, + Err(err) => Err(std::io::Error::other(err.to_string())), + } } pub fn is_token_type_valid(token_type: &String) -> bool { - false + token_type == SERVICE_TOKEN_TYPE + // false } fn get_payload(