Compare commits

..

8 Commits

Author SHA1 Message Date
27e6260087 tsk-61: Version bump
All checks were successful
Rust Build / Check (pull_request) Successful in 38s
Rust Build / Test Suite (pull_request) Successful in 1m17s
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Clippy (pull_request) Successful in 39s
Rust Build / build (pull_request) Successful in 58s
2025-10-20 12:41:45 -04:00
b52faed595 tsk-61: Workflow fix 2025-10-20 12:41:18 -04:00
6ee2db43d2 tsk-61: code formatting
Some checks failed
Rust Build / Check (pull_request) Successful in 40s
Rust Build / Test Suite (pull_request) Failing after 1m25s
Rust Build / Rustfmt (pull_request) Successful in 30s
Rust Build / Clippy (pull_request) Successful in 39s
Rust Build / build (pull_request) Successful in 58s
2025-10-20 12:33:11 -04:00
3db7bc330b tsk-61: Added code to check registration status 2025-10-20 12:32:55 -04:00
76f7bbc9e2 tsk-61: Updating readme 2025-10-20 12:16:43 -04:00
34687dda7d tsk-61: Adding changes to make feature available 2025-10-20 12:16:31 -04:00
6c83e566bf tsk-61: Filename change 2025-10-20 12:12:19 -04:00
b74c0fc3b0 tsk-61: Added env variable 2025-10-20 12:11:52 -04:00
11 changed files with 119 additions and 106 deletions

2
Cargo.lock generated
View File

@@ -748,7 +748,7 @@ dependencies = [
[[package]] [[package]]
name = "icarus_auth" name = "icarus_auth"
version = "0.6.5" version = "0.6.3"
dependencies = [ dependencies = [
"argon2", "argon2",
"axum", "axum",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "icarus_auth" name = "icarus_auth"
version = "0.6.5" version = "0.6.3"
edition = "2024" edition = "2024"
rust-version = "1.90" rust-version = "1.90"

View File

@@ -23,7 +23,6 @@ CREATE TABLE IF NOT EXISTS "salt" (
CREATE TABLE IF NOT EXISTS "passphrase" ( CREATE TABLE IF NOT EXISTS "passphrase" (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username TEXT NOT NULL,
passphrase TEXT NOT NULL, passphrase TEXT NOT NULL,
date_created TIMESTAMPTZ NOT NULL DEFAULT NOW() date_created TIMESTAMPTZ NOT NULL DEFAULT NOW()
); );

View File

@@ -1,2 +1,2 @@
-- Add migration script here -- Add migration script here
INSERT INTO "passphrase" (id, username, passphrase) VALUES('22f9c775-cce9-457a-a147-9dafbb801f61', 'service', 'iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH'); INSERT INTO "passphrase" (id, passphrase) VALUES('22f9c775-cce9-457a-a147-9dafbb801f61', 'iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH');

View File

@@ -59,6 +59,10 @@ pub mod endpoint {
use super::request; use super::request;
use super::response; use super::response;
// TODO: At some point, get the username from the DB
// Name of service username when returning a login result
pub const SERVICE_USERNAME: &str = "service";
async fn not_found(message: &str) -> (StatusCode, Json<response::Response>) { async fn not_found(message: &str) -> (StatusCode, Json<response::Response>) {
( (
StatusCode::NOT_FOUND, StatusCode::NOT_FOUND,
@@ -114,7 +118,7 @@ pub mod endpoint {
}), }),
) )
} else { } else {
return not_found("Could not verify token").await; return not_found("Could not verify password").await;
} }
} else { } else {
return not_found("Error Hashing").await; return not_found("Error Hashing").await;
@@ -150,7 +154,7 @@ pub mod endpoint {
let mut response = response::service_login::Response::default(); let mut response = response::service_login::Response::default();
match repo::service::valid_passphrase(&pool, &payload.passphrase).await { match repo::service::valid_passphrase(&pool, &payload.passphrase).await {
Ok((id, username, _date_created)) => { Ok((id, _passphrase, _date_created)) => {
let key = icarus_envy::environment::get_secret_key().await.value; let key = icarus_envy::environment::get_secret_key().await.value;
let (token_literal, duration) = let (token_literal, duration) =
token_stuff::create_service_token(&key, &id).unwrap(); token_stuff::create_service_token(&key, &id).unwrap();
@@ -158,7 +162,7 @@ pub mod endpoint {
if token_stuff::verify_token(&key, &token_literal) { if token_stuff::verify_token(&key, &token_literal) {
let login_result = icarus_models::login_result::LoginResult { let login_result = icarus_models::login_result::LoginResult {
id, id,
username, username: String::from(SERVICE_USERNAME),
token: token_literal, token: token_literal,
token_type: String::from(icarus_models::token::TOKEN_TYPE), token_type: String::from(icarus_models::token::TOKEN_TYPE),
expiration: duration, expiration: duration,
@@ -212,15 +216,15 @@ pub mod endpoint {
// Get passphrase record with id // Get passphrase record with id
match token_stuff::extract_id_from_token(&key, &payload.access_token) { match token_stuff::extract_id_from_token(&key, &payload.access_token) {
Ok(id) => match repo::service::get_passphrase(&pool, &id).await { Ok(id) => match repo::service::get_passphrase(&pool, &id).await {
Ok((username, _, _)) => { Ok((returned_id, _, _)) => {
match token_stuff::create_service_refresh_token(&key, &id) { match token_stuff::create_service_refresh_token(&key, &returned_id) {
Ok((access_token, exp_dur)) => { Ok((access_token, exp_dur)) => {
let login_result = icarus_models::login_result::LoginResult { let login_result = icarus_models::login_result::LoginResult {
id, id: returned_id,
token: access_token, token: access_token,
expiration: exp_dur, expiration: exp_dur,
token_type: String::from(icarus_models::token::TOKEN_TYPE), token_type: String::from(icarus_models::token::TOKEN_TYPE),
username, username: String::from(SERVICE_USERNAME),
}; };
response.message = String::from("Successful"); response.message = String::from("Successful");
response.data.push(login_result); response.data.push(login_result);

View File

@@ -1,20 +0,0 @@
use sqlx::postgres::PgPoolOptions;
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
let database_url = icarus_envy::environment::get_db_url().await.value;
println!("Database url: {database_url}");
PgPoolOptions::new()
.max_connections(super::connection_settings::MAXCONN)
.connect(&database_url)
.await
}
pub async fn migrations(pool: &sqlx::PgPool) {
// Run migrations using the sqlx::migrate! macro
// Assumes your migrations are in a ./migrations folder relative to Cargo.toml
sqlx::migrate!("./migrations")
.run(pool)
.await
.expect("Failed to run migrations");
}

View File

@@ -1,5 +0,0 @@
pub mod init;
mod connection_settings {
pub const MAXCONN: u32 = 5;
}

36
src/lib.rs Normal file
View File

@@ -0,0 +1,36 @@
// TODO: Get rid of this file and place the code in more appropriate places
pub mod callers;
pub mod config;
pub mod hashing;
pub mod repo;
pub mod token_stuff;
mod connection_settings {
pub const MAXCONN: u32 = 5;
}
pub mod db {
use sqlx::postgres::PgPoolOptions;
use crate::connection_settings;
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
let database_url = icarus_envy::environment::get_db_url().await.value;
println!("Database url: {database_url}");
PgPoolOptions::new()
.max_connections(connection_settings::MAXCONN)
.connect(&database_url)
.await
}
pub async fn migrations(pool: &sqlx::PgPool) {
// Run migrations using the sqlx::migrate! macro
// Assumes your migrations are in a ./migrations folder relative to Cargo.toml
sqlx::migrate!("./migrations")
.run(pool)
.await
.expect("Failed to run migrations");
}
}

View File

@@ -1,9 +1,5 @@
pub mod callers; use icarus_auth::callers;
pub mod config; use icarus_auth::config;
pub mod db;
pub mod hashing;
pub mod repo;
pub mod token_stuff;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@@ -25,7 +21,7 @@ mod init {
}; };
use utoipa::OpenApi; use utoipa::OpenApi;
use super::callers; use crate::callers;
use callers::common as common_callers; use callers::common as common_callers;
use callers::login as login_caller; use callers::login as login_caller;
use callers::register as register_caller; use callers::register as register_caller;
@@ -128,11 +124,11 @@ mod init {
} }
pub async fn app() -> Router { pub async fn app() -> Router {
let pool = super::db::init::create_pool() let pool = icarus_auth::db::create_pool()
.await .await
.expect("Failed to create pool"); .expect("Failed to create pool");
super::db::init::migrations(&pool).await; icarus_auth::db::migrations(&pool).await;
routes() routes()
.await .await
@@ -220,8 +216,8 @@ mod tests {
} }
} }
fn get_test_register_request() -> callers::register::request::Request { fn get_test_register_request() -> icarus_auth::callers::register::request::Request {
callers::register::request::Request { icarus_auth::callers::register::request::Request {
username: String::from("somethingsss"), username: String::from("somethingsss"),
password: String::from("Raindown!"), password: String::from("Raindown!"),
email: String::from("dev@null.com"), email: String::from("dev@null.com"),
@@ -231,7 +227,9 @@ mod tests {
} }
} }
fn get_test_register_payload(usr: &callers::register::request::Request) -> serde_json::Value { fn get_test_register_payload(
usr: &icarus_auth::callers::register::request::Request,
) -> serde_json::Value {
json!({ json!({
"username": &usr.username, "username": &usr.username,
"password": &usr.password, "password": &usr.password,
@@ -247,7 +245,7 @@ mod tests {
pub async fn register( pub async fn register(
app: &axum::Router, app: &axum::Router,
usr: &super::callers::register::request::Request, usr: &icarus_auth::callers::register::request::Request,
) -> Result<axum::response::Response, std::convert::Infallible> { ) -> Result<axum::response::Response, std::convert::Infallible> {
let payload = super::get_test_register_payload(&usr); let payload = super::get_test_register_payload(&usr);
let req = axum::http::Request::builder() let req = axum::http::Request::builder()
@@ -300,7 +298,7 @@ mod tests {
let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
db::init::migrations(&pool).await; icarus_auth::db::migrations(&pool).await;
let app = init::routes().await.layer(axum::Extension(pool)); let app = init::routes().await.layer(axum::Extension(pool));
@@ -357,7 +355,7 @@ mod tests {
let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
db::init::migrations(&pool).await; icarus_auth::db::migrations(&pool).await;
let app = init::routes().await.layer(axum::Extension(pool)); let app = init::routes().await.layer(axum::Extension(pool));
@@ -445,7 +443,7 @@ mod tests {
let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
db::init::migrations(&pool).await; icarus_auth::db::migrations(&pool).await;
let app = init::routes().await.layer(axum::Extension(pool)); let app = init::routes().await.layer(axum::Extension(pool));
let passphrase = let passphrase =
@@ -499,13 +497,13 @@ mod tests {
let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
db::init::migrations(&pool).await; icarus_auth::db::migrations(&pool).await;
let app = init::routes().await.layer(axum::Extension(pool)); let app = init::routes().await.layer(axum::Extension(pool));
let id = uuid::Uuid::parse_str("22f9c775-cce9-457a-a147-9dafbb801f61").unwrap(); let id = uuid::Uuid::parse_str("22f9c775-cce9-457a-a147-9dafbb801f61").unwrap();
let key = icarus_envy::environment::get_secret_key().await.value; let key = icarus_envy::environment::get_secret_key().await.value;
match token_stuff::create_service_token(&key, &id) { match icarus_auth::token_stuff::create_service_token(&key, &id) {
Ok((token, _expire)) => { Ok((token, _expire)) => {
let payload = serde_json::json!({ let payload = serde_json::json!({
"access_token": token "access_token": token

View File

@@ -1,5 +1,3 @@
pub mod service;
pub mod user { pub mod user {
use sqlx::Row; use sqlx::Row;
@@ -197,3 +195,56 @@ pub mod salt {
} }
} }
} }
pub mod service {
use sqlx::Row;
pub async fn valid_passphrase(
pool: &sqlx::PgPool,
passphrase: &String,
) -> Result<(uuid::Uuid, String, time::OffsetDateTime), sqlx::Error> {
let result = sqlx::query(
r#"
SELECT * FROM "passphrase" WHERE passphrase = $1
"#,
)
.bind(passphrase)
.fetch_one(pool)
.await;
match result {
Ok(row) => {
let id: uuid::Uuid = row.try_get("id")?;
let passphrase: String = row.try_get("passphrase")?;
let date_created: Option<time::OffsetDateTime> = row.try_get("date_created")?;
Ok((id, passphrase, date_created.unwrap()))
}
Err(err) => Err(err),
}
}
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

@@ -1,50 +0,0 @@
use sqlx::Row;
pub async fn valid_passphrase(
pool: &sqlx::PgPool,
passphrase: &String,
) -> Result<(uuid::Uuid, String, time::OffsetDateTime), sqlx::Error> {
let result = sqlx::query(
r#"
SELECT id, username, date_created FROM "passphrase" WHERE passphrase = $1
"#,
)
.bind(passphrase)
.fetch_one(pool)
.await;
match result {
Ok(row) => {
let id: uuid::Uuid = row.try_get("id")?;
let username: String = row.try_get("username")?;
let date_created: Option<time::OffsetDateTime> = row.try_get("date_created")?;
Ok((id, username, date_created.unwrap()))
}
Err(err) => Err(err),
}
}
pub async fn get_passphrase(
pool: &sqlx::PgPool,
id: &uuid::Uuid,
) -> Result<(String, String, time::OffsetDateTime), sqlx::Error> {
let result = sqlx::query(
r#"
SELECT username, passphrase, date_created FROM "passphrase" WHERE id = $1;
"#,
)
.bind(id)
.fetch_one(pool)
.await;
match result {
Ok(row) => {
let username: String = row.try_get("username")?;
let passphrase: String = row.try_get("passphrase")?;
let date_created: time::OffsetDateTime = row.try_get("date_created")?;
Ok((username, passphrase, date_created))
}
Err(err) => Err(err),
}
}