tsk-51: Functionality of endpoint is almost done
Some checks failed
Rust Build / Check (pull_request) Failing after 49s
Rust Build / Rustfmt (pull_request) Successful in 32s
Rust Build / Test Suite (pull_request) Failing after 1m7s
Rust Build / Clippy (pull_request) Failing after 1m5s
Rust Build / build (pull_request) Failing after 35s

This commit is contained in:
2025-08-10 17:19:57 -04:00
parent 90342dd493
commit 2e4c289998
3 changed files with 104 additions and 5 deletions

View File

@@ -169,7 +169,43 @@ pub mod endpoint {
if token_stuff::is_token_type_valid(&token_type) { if token_stuff::is_token_type_valid(&token_type) {
// let (token_literal, dur) = token_stuff::create_service_refresh_token(&key, ) // let (token_literal, dur) = token_stuff::create_service_refresh_token(&key, )
// 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)) (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 { } else {
response.message = String::from("Invalid token type"); response.message = String::from("Invalid token type");
(axum::http::StatusCode::NOT_FOUND, axum::Json(response)) (axum::http::StatusCode::NOT_FOUND, axum::Json(response))

View File

@@ -225,5 +225,27 @@ pub mod service {
} }
// TODO: Write a function to get a passphrase record with an id // 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),
}
}
} }

View File

@@ -38,7 +38,7 @@ pub fn create_service_token(
id: &uuid::Uuid, id: &uuid::Uuid,
) -> Result<(String, i64), josekit::JoseError> { ) -> Result<(String, i64), josekit::JoseError> {
let resource = icarus_models::token::TokenResource { let resource = icarus_models::token::TokenResource {
message: String::from("Service random"), message: String::from(SERVICE_SUBJECT),
issuer: String::from(ISSUER), issuer: String::from(ISSUER),
audiences: vec![String::from(AUDIENCE)], audiences: vec![String::from(AUDIENCE)],
id: *id, id: *id,
@@ -46,6 +46,19 @@ pub fn create_service_token(
icarus_models::token::create_token(provided, &resource, time::Duration::hours(1)) 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 { pub fn verify_token(key: &String, token: &String) -> bool {
match get_payload(key, token) { match get_payload(key, token) {
Ok((payload, _header)) => match payload.subject() { Ok((payload, _header)) => match payload.subject() {
@@ -69,12 +82,40 @@ pub fn extract_id_from_token(key: &String, token: &String) -> Result<uuid::Uuid,
} }
} }
pub const APP_TOKEN_TYPE: &str = "Icarus_App";
pub const APP_SUBJECT: &str = "Something random";
pub const SERVICE_TOKEN_TYPE: &str = "Icarus_Service";
pub const SERVICE_SUBJECT: &str = "Service random";
pub fn get_token_type(key: &String, token: &String) -> Result<String, std::io::Error> { pub fn get_token_type(key: &String, token: &String) -> Result<String, std::io::Error> {
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 { pub fn is_token_type_valid(token_type: &String) -> bool {
false token_type == SERVICE_TOKEN_TYPE
// false
} }
fn get_payload( fn get_payload(