Compare commits
2 Commits
v0.6.4-mai
...
v0.6.5
| Author | SHA1 | Date | |
|---|---|---|---|
| cba3e3db79 | |||
| 5407462def |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -748,7 +748,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "icarus_auth"
|
||||
version = "0.6.4"
|
||||
version = "0.6.5"
|
||||
dependencies = [
|
||||
"argon2",
|
||||
"axum",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "icarus_auth"
|
||||
version = "0.6.4"
|
||||
version = "0.6.5"
|
||||
edition = "2024"
|
||||
rust-version = "1.90"
|
||||
|
||||
|
||||
@@ -2,6 +2,13 @@ A auth web API services for the Icarus project.
|
||||
|
||||
|
||||
# Getting Started
|
||||
Install the `sqlx` tool to use migrations.
|
||||
```
|
||||
cargo install sqlx-cli
|
||||
```
|
||||
This will be used to scaffold development for local environments.
|
||||
|
||||
|
||||
The easiest way to get started is through docker. This assumes that docker is already installed
|
||||
on your system. Copy the `.env.docker.sample` as `.env`. Most of the data in the env file doesn't
|
||||
need to be modified. The `SECRET_KEY` variable should be changed since it will be used for token
|
||||
@@ -33,4 +40,4 @@ docker system prune -a
|
||||
```
|
||||
|
||||
To view the OpenAPI spec, run the project and access `/swagger-ui`. If running through docker,
|
||||
the url would be something like `http://localhost:8000/swagger-ui`.
|
||||
the url would be something like `http://localhost:8001/swagger-ui`.
|
||||
|
||||
@@ -23,6 +23,7 @@ CREATE TABLE IF NOT EXISTS "salt" (
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "passphrase" (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
username TEXT NOT NULL,
|
||||
passphrase TEXT NOT NULL,
|
||||
date_created TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
-- Add migration script here
|
||||
INSERT INTO "passphrase" (id, passphrase) VALUES('22f9c775-cce9-457a-a147-9dafbb801f61', 'iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH');
|
||||
INSERT INTO "passphrase" (id, username, passphrase) VALUES('22f9c775-cce9-457a-a147-9dafbb801f61', 'service', 'iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH');
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
TODO: At some point, move this somewhere that is appropriate
|
||||
|
||||
# Make sure role has CREATEDB
|
||||
ALTER ROLE username_that_needs_permission CREATEDB;
|
||||
|
||||
# Install migrations
|
||||
cargo install sqlx-cli
|
||||
|
||||
# Make sure to populate DATABASE_URL with correct value.
|
||||
# By default, the DATABASE_URL found in .env file will be used
|
||||
export DATABASE_URL="postgres://icarus_op_test:password@localhost/icarus_auth_test"
|
||||
|
||||
# init
|
||||
sqlx migrate add init_migration
|
||||
sqlx migrate run
|
||||
|
||||
# Create
|
||||
sqlx database create
|
||||
|
||||
# Drop
|
||||
sqlx database drop
|
||||
|
||||
# setup
|
||||
sqlx database setup
|
||||
|
||||
# Reset
|
||||
sqlx database reset
|
||||
@@ -59,10 +59,6 @@ pub mod endpoint {
|
||||
use super::request;
|
||||
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>) {
|
||||
(
|
||||
StatusCode::NOT_FOUND,
|
||||
@@ -118,7 +114,7 @@ pub mod endpoint {
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
return not_found("Could not verify password").await;
|
||||
return not_found("Could not verify token").await;
|
||||
}
|
||||
} else {
|
||||
return not_found("Error Hashing").await;
|
||||
@@ -154,7 +150,7 @@ pub mod endpoint {
|
||||
let mut response = response::service_login::Response::default();
|
||||
|
||||
match repo::service::valid_passphrase(&pool, &payload.passphrase).await {
|
||||
Ok((id, _passphrase, _date_created)) => {
|
||||
Ok((id, username, _date_created)) => {
|
||||
let key = icarus_envy::environment::get_secret_key().await.value;
|
||||
let (token_literal, duration) =
|
||||
token_stuff::create_service_token(&key, &id).unwrap();
|
||||
@@ -162,7 +158,7 @@ pub mod endpoint {
|
||||
if token_stuff::verify_token(&key, &token_literal) {
|
||||
let login_result = icarus_models::login_result::LoginResult {
|
||||
id,
|
||||
username: String::from(SERVICE_USERNAME),
|
||||
username,
|
||||
token: token_literal,
|
||||
token_type: String::from(icarus_models::token::TOKEN_TYPE),
|
||||
expiration: duration,
|
||||
@@ -216,15 +212,15 @@ pub mod endpoint {
|
||||
// Get passphrase record with id
|
||||
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((username, _, _)) => {
|
||||
match token_stuff::create_service_refresh_token(&key, &id) {
|
||||
Ok((access_token, exp_dur)) => {
|
||||
let login_result = icarus_models::login_result::LoginResult {
|
||||
id: returned_id,
|
||||
id,
|
||||
token: access_token,
|
||||
expiration: exp_dur,
|
||||
token_type: String::from(icarus_models::token::TOKEN_TYPE),
|
||||
username: String::from(SERVICE_USERNAME),
|
||||
username,
|
||||
};
|
||||
response.message = String::from("Successful");
|
||||
response.data.push(login_result);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
pub mod service;
|
||||
|
||||
pub mod user {
|
||||
use sqlx::Row;
|
||||
|
||||
@@ -195,56 +197,3 @@ 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
50
src/repo/service.rs
Normal file
50
src/repo/service.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user