Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cba3e3db79 | |||
| 5407462def | |||
| eb6ddbc97a | |||
| e86ca4b2c8 | |||
| 6ec3b25e7d |
@@ -10,3 +10,4 @@ POSTGRES_AUTH_PASSWORD=password
|
||||
POSTGRES_AUTH_DB=icarus_auth_db
|
||||
POSTGRES_AUTH_HOST=auth_db
|
||||
DATABASE_URL=postgresql://${POSTGRES_AUTH_USER}:${POSTGRES_AUTH_PASSWORD}@${POSTGRES_AUTH_HOST}:5432/${POSTGRES_AUTH_DB}
|
||||
ENABLE_REGISTRATION=TRUE
|
||||
|
||||
@@ -10,3 +10,4 @@ POSTGRES_AUTH_PASSWORD=password
|
||||
POSTGRES_AUTH_DB=icarus_auth_test_db
|
||||
POSTGRES_AUTH_HOST=localhost
|
||||
DATABASE_URL=postgresql://${POSTGRES_AUTH_USER}:${POSTGRES_AUTH_PASSWORD}@${POSTGRES_AUTH_HOST}:5432/${POSTGRES_AUTH_DB}
|
||||
ENABLE_REGISTRATION=TRUE
|
||||
@@ -76,6 +76,7 @@ jobs:
|
||||
SECRET_KEY: ${{ secrets.TOKEN_SECRET_KEY }}
|
||||
# Make SSH agent available if tests fetch private dependencies
|
||||
SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }}
|
||||
ENABLE_REGISTRATION: 'TRUE'
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/icarus_models_deploy_key
|
||||
|
||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -748,7 +748,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "icarus_auth"
|
||||
version = "0.6.1"
|
||||
version = "0.6.5"
|
||||
dependencies = [
|
||||
"argon2",
|
||||
"axum",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "icarus_auth"
|
||||
version = "0.6.1"
|
||||
version = "0.6.5"
|
||||
edition = "2024"
|
||||
rust-version = "1.90"
|
||||
|
||||
|
||||
21
README.md
21
README.md
@@ -2,31 +2,42 @@ 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
|
||||
generation. The `SECRET_PASSPHASE` should also be changed when in production mode, but make sure
|
||||
the respective `passphrase` database table record exists.
|
||||
|
||||
Build image
|
||||
To enable or disable registrations, use `TRUE` or `FALSE` for the `ENABLE_REGISTRATION` variable.
|
||||
By default it is `TRUE`.
|
||||
|
||||
|
||||
### Build image
|
||||
```
|
||||
docker compose build
|
||||
```
|
||||
|
||||
Start images
|
||||
### Start images
|
||||
```
|
||||
docker compose up -d --force-recreate
|
||||
```
|
||||
|
||||
Bring it down
|
||||
### Bring it down
|
||||
```
|
||||
docker compose down -v
|
||||
```
|
||||
|
||||
Pruning
|
||||
### Pruning
|
||||
```
|
||||
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);
|
||||
|
||||
@@ -52,69 +52,108 @@ pub async fn register_user(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
Json(payload): Json<request::Request>,
|
||||
) -> (StatusCode, Json<response::Response>) {
|
||||
let mut user = icarus_models::user::User {
|
||||
id: uuid::Uuid::nil(),
|
||||
username: payload.username.clone(),
|
||||
password: payload.password.clone(),
|
||||
email: payload.email.clone(),
|
||||
phone: payload.phone.clone(),
|
||||
firstname: payload.firstname.clone(),
|
||||
lastname: payload.lastname.clone(),
|
||||
status: String::from("Active"),
|
||||
email_verified: true,
|
||||
date_created: Some(time::OffsetDateTime::now_utc()),
|
||||
last_login: None,
|
||||
salt_id: uuid::Uuid::nil(),
|
||||
let registration_enabled = match is_registration_enabled().await {
|
||||
Ok(value) => value,
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
return (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(response::Response {
|
||||
message: String::from("Registration check failed"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
match repo::user::exists(&pool, &user.username).await {
|
||||
Ok(res) => {
|
||||
if res {
|
||||
(
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(response::Response {
|
||||
message: String::from("Error"),
|
||||
data: vec![user],
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
let salt_string = hashing::generate_salt().unwrap();
|
||||
let mut salt = icarus_models::user::salt::Salt::default();
|
||||
let generated_salt = salt_string;
|
||||
salt.salt = generated_salt.to_string();
|
||||
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
||||
user.salt_id = salt.id;
|
||||
let hashed_password =
|
||||
hashing::hash_password(&user.password, &generated_salt).unwrap();
|
||||
user.password = hashed_password;
|
||||
if registration_enabled {
|
||||
let mut user = icarus_models::user::User {
|
||||
username: payload.username.clone(),
|
||||
password: payload.password.clone(),
|
||||
email: payload.email.clone(),
|
||||
phone: payload.phone.clone(),
|
||||
firstname: payload.firstname.clone(),
|
||||
lastname: payload.lastname.clone(),
|
||||
status: String::from("Active"),
|
||||
email_verified: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
match repo::user::insert(&pool, &user).await {
|
||||
Ok(id) => {
|
||||
user.id = id;
|
||||
(
|
||||
StatusCode::CREATED,
|
||||
Json(response::Response {
|
||||
message: String::from("User created"),
|
||||
data: vec![user],
|
||||
}),
|
||||
)
|
||||
}
|
||||
Err(err) => (
|
||||
match repo::user::exists(&pool, &user.username).await {
|
||||
Ok(res) => {
|
||||
if res {
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(response::Response {
|
||||
message: err.to_string(),
|
||||
data: vec![user],
|
||||
message: String::from("Error"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
),
|
||||
)
|
||||
} else {
|
||||
let salt_string = hashing::generate_salt().unwrap();
|
||||
let mut salt = icarus_models::user::salt::Salt::default();
|
||||
let generated_salt = salt_string;
|
||||
salt.salt = generated_salt.to_string();
|
||||
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
||||
user.salt_id = salt.id;
|
||||
let hashed_password =
|
||||
hashing::hash_password(&user.password, &generated_salt).unwrap();
|
||||
user.password = hashed_password;
|
||||
|
||||
match repo::user::insert(&pool, &user).await {
|
||||
Ok((id, date_created)) => {
|
||||
user.id = id;
|
||||
user.date_created = date_created;
|
||||
(
|
||||
StatusCode::CREATED,
|
||||
Json(response::Response {
|
||||
message: String::from("User created"),
|
||||
data: vec![user],
|
||||
}),
|
||||
)
|
||||
}
|
||||
Err(err) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(response::Response {
|
||||
message: err.to_string(),
|
||||
data: vec![user],
|
||||
}),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(response::Response {
|
||||
message: err.to_string(),
|
||||
data: vec![user],
|
||||
}),
|
||||
),
|
||||
}
|
||||
Err(err) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
} else {
|
||||
(
|
||||
axum::http::StatusCode::NOT_ACCEPTABLE,
|
||||
Json(response::Response {
|
||||
message: err.to_string(),
|
||||
data: vec![user],
|
||||
message: String::from("Registration is not enabled"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks to see if registration is enabled
|
||||
async fn is_registration_enabled() -> Result<bool, std::io::Error> {
|
||||
let key = String::from("ENABLE_REGISTRATION");
|
||||
let var = icarus_envy::environment::get_env(&key).await;
|
||||
let parsed_value = var.value.to_uppercase();
|
||||
|
||||
if parsed_value == "TRUE" {
|
||||
Ok(true)
|
||||
} else if parsed_value == "FALSE" {
|
||||
Ok(false)
|
||||
} else {
|
||||
Err(std::io::Error::other(
|
||||
"Could not determine value of ENABLE_REGISTRATION",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
20
src/db/init.rs
Normal file
20
src/db/init.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
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");
|
||||
}
|
||||
5
src/db/mod.rs
Normal file
5
src/db/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod init;
|
||||
|
||||
mod connection_settings {
|
||||
pub const MAXCONN: u32 = 5;
|
||||
}
|
||||
36
src/lib.rs
36
src/lib.rs
@@ -1,36 +0,0 @@
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
34
src/main.rs
34
src/main.rs
@@ -1,5 +1,9 @@
|
||||
use icarus_auth::callers;
|
||||
use icarus_auth::config;
|
||||
pub mod callers;
|
||||
pub mod config;
|
||||
pub mod db;
|
||||
pub mod hashing;
|
||||
pub mod repo;
|
||||
pub mod token_stuff;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -21,7 +25,7 @@ mod init {
|
||||
};
|
||||
use utoipa::OpenApi;
|
||||
|
||||
use crate::callers;
|
||||
use super::callers;
|
||||
use callers::common as common_callers;
|
||||
use callers::login as login_caller;
|
||||
use callers::register as register_caller;
|
||||
@@ -124,11 +128,11 @@ mod init {
|
||||
}
|
||||
|
||||
pub async fn app() -> Router {
|
||||
let pool = icarus_auth::db::create_pool()
|
||||
let pool = super::db::init::create_pool()
|
||||
.await
|
||||
.expect("Failed to create pool");
|
||||
|
||||
icarus_auth::db::migrations(&pool).await;
|
||||
super::db::init::migrations(&pool).await;
|
||||
|
||||
routes()
|
||||
.await
|
||||
@@ -216,8 +220,8 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_test_register_request() -> icarus_auth::callers::register::request::Request {
|
||||
icarus_auth::callers::register::request::Request {
|
||||
fn get_test_register_request() -> callers::register::request::Request {
|
||||
callers::register::request::Request {
|
||||
username: String::from("somethingsss"),
|
||||
password: String::from("Raindown!"),
|
||||
email: String::from("dev@null.com"),
|
||||
@@ -227,9 +231,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_test_register_payload(
|
||||
usr: &icarus_auth::callers::register::request::Request,
|
||||
) -> serde_json::Value {
|
||||
fn get_test_register_payload(usr: &callers::register::request::Request) -> serde_json::Value {
|
||||
json!({
|
||||
"username": &usr.username,
|
||||
"password": &usr.password,
|
||||
@@ -245,7 +247,7 @@ mod tests {
|
||||
|
||||
pub async fn register(
|
||||
app: &axum::Router,
|
||||
usr: &icarus_auth::callers::register::request::Request,
|
||||
usr: &super::callers::register::request::Request,
|
||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||
let payload = super::get_test_register_payload(&usr);
|
||||
let req = axum::http::Request::builder()
|
||||
@@ -298,7 +300,7 @@ mod tests {
|
||||
|
||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||
|
||||
icarus_auth::db::migrations(&pool).await;
|
||||
db::init::migrations(&pool).await;
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
|
||||
@@ -355,7 +357,7 @@ mod tests {
|
||||
|
||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||
|
||||
icarus_auth::db::migrations(&pool).await;
|
||||
db::init::migrations(&pool).await;
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
|
||||
@@ -443,7 +445,7 @@ mod tests {
|
||||
|
||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||
|
||||
icarus_auth::db::migrations(&pool).await;
|
||||
db::init::migrations(&pool).await;
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
let passphrase =
|
||||
@@ -497,13 +499,13 @@ mod tests {
|
||||
|
||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||
|
||||
icarus_auth::db::migrations(&pool).await;
|
||||
db::init::migrations(&pool).await;
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
let id = uuid::Uuid::parse_str("22f9c775-cce9-457a-a147-9dafbb801f61").unwrap();
|
||||
let key = icarus_envy::environment::get_secret_key().await.value;
|
||||
|
||||
match icarus_auth::token_stuff::create_service_token(&key, &id) {
|
||||
match token_stuff::create_service_token(&key, &id) {
|
||||
Ok((token, _expire)) => {
|
||||
let payload = serde_json::json!({
|
||||
"access_token": token
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
pub mod service;
|
||||
|
||||
pub mod user {
|
||||
use sqlx::Row;
|
||||
|
||||
@@ -94,7 +96,7 @@ pub mod user {
|
||||
pub async fn insert(
|
||||
pool: &sqlx::PgPool,
|
||||
user: &icarus_models::user::User,
|
||||
) -> Result<uuid::Uuid, sqlx::Error> {
|
||||
) -> Result<(uuid::Uuid, std::option::Option<time::OffsetDateTime>), sqlx::Error> {
|
||||
let row = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO "user" (username, password, email, phone, firstname, lastname, email_verified, status, salt_id)
|
||||
@@ -124,10 +126,10 @@ pub mod user {
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)?,
|
||||
};
|
||||
|
||||
if !result.id.is_nil() {
|
||||
Ok(result.id)
|
||||
} else {
|
||||
if result.id.is_nil() && result.date_created.is_none() {
|
||||
Err(sqlx::Error::RowNotFound)
|
||||
} else {
|
||||
Ok((result.id, result.date_created))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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