Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3423c1eeb1 | ||
|
|
21bfaf7657 | ||
|
|
9dba08f179 | ||
|
|
438b19e170 | ||
|
|
69d908bac3 | ||
|
|
7d2faba4f2 | ||
|
|
e9b59fc1dd | ||
|
|
1ab7bd4ae1 | ||
|
|
d83591c8a4 | ||
|
|
0cab4e8530 |
+9
-8
@@ -1,10 +1,11 @@
|
||||
SECRET_KEY=NULqYIzgt28bTiyziCd7IOO7b6LnWDW!
|
||||
DB_NAME=textsender_auth_db
|
||||
DB_USER=textsender_auth
|
||||
DB_PASSWORD=password
|
||||
DB_HOST=auth_db
|
||||
DB_PORT=5432
|
||||
DB_SSLMODE=disable
|
||||
DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:5432/${DB_NAME}
|
||||
DB_AUTH_NAME=textsender_auth_db
|
||||
DB_AUTH_USER=textsender_auth
|
||||
DB_AUTH_PASSWORD=password
|
||||
DB_AUTH_HOST=auth_db
|
||||
DB_AUTH_PORT=5432
|
||||
DB_AUTH_SSLMODE=disable
|
||||
DATABASE_URL=postgres://${DB_AUTH_USER}:${DB_AUTH_PASSWORD}@${DB_AUTH_HOST}:${DB_AUTH_PORT}/${DB_AUTH_NAME}
|
||||
ENABLE_REGISTRATION=true
|
||||
ALLOWED_ORIGINS="http://textsender.com"
|
||||
ALLOWED_ORIGINS="http://textsender.com,http://localhost:5173,http://localhost:9080"
|
||||
APP_ENV=production
|
||||
|
||||
+9
-8
@@ -1,10 +1,11 @@
|
||||
SECRET_KEY=NULqYIzgt28bTiyziCd7IOO7b6LnWDW!
|
||||
DB_NAME=textsender_auth_db
|
||||
DB_USER=textsender_auth
|
||||
DB_PASSWORD=password
|
||||
DB_HOST=localhost
|
||||
DB_PORT=5432
|
||||
DB_SSLMODE=disable
|
||||
DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:5432/${DB_NAME}
|
||||
DB_AUTH_NAME=textsender_auth_db
|
||||
DB_AUTH_USER=textsender_auth
|
||||
DB_AUTH_PASSWORD=password
|
||||
DB_AUTH_HOST=localhost
|
||||
DB_AUTH_PORT=5432
|
||||
DB_AUTH_SSLMODE=disable
|
||||
DATABASE_URL=postgres://${DB_AUTH_USER}:${DB_AUTH_PASSWORD}@${DB_AUTH_HOST}:${DB_AUTH_PORT}/${DB_AUTH_NAME}
|
||||
ENABLE_REGISTRATION=true
|
||||
ALLOWED_ORIGINS="http://textsender.com"
|
||||
ALLOWED_ORIGINS="http://textsender.com,http://localhost:5173,http://localhost:9080"
|
||||
APP_ENV=production
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
name: Rust Build
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
check:
|
||||
name: Check
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.96.1
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/textsender-models_deploy_key
|
||||
chmod 600 ~/.ssh/textsender-models_deploy_key
|
||||
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add -v ~/.ssh/textsender-models_deploy_key
|
||||
|
||||
cargo check
|
||||
|
||||
|
||||
fmt:
|
||||
name: Rustfmt
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.96.1
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- run: rustup component add rustfmt
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/textsender-models_deploy_key
|
||||
chmod 600 ~/.ssh/textsender-models_deploy_key
|
||||
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add -v ~/.ssh/textsender-models_deploy_key
|
||||
cargo fmt --all -- --check
|
||||
|
||||
clippy:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.96.1
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- run: rustup component add clippy
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/textsender-models_deploy_key
|
||||
chmod 600 ~/.ssh/textsender-models_deploy_key
|
||||
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add -v ~/.ssh/textsender-models_deploy_key
|
||||
cargo clippy -- -D warnings
|
||||
@@ -4,19 +4,20 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
check:
|
||||
name: Check
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.95
|
||||
toolchain: 1.96.1
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
@@ -41,6 +42,7 @@ jobs:
|
||||
POSTGRES_USER: ${{ secrets.DB_TEST_USER || 'testuser' }}
|
||||
POSTGRES_PASSWORD: ${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}
|
||||
POSTGRES_DB: ${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||
POSTGRES_PORT: ${{ secrets.DB_PORT || 5432 }}
|
||||
# Options to wait until the database is ready
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
@@ -49,10 +51,10 @@ jobs:
|
||||
--health-retries 5
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.95
|
||||
toolchain: 1.96.1
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
# --- Add this step for explicit verification ---
|
||||
- name: Verify Docker Environment
|
||||
@@ -71,7 +73,7 @@ jobs:
|
||||
- name: Run tests
|
||||
env:
|
||||
# Define DATABASE_URL for tests to use
|
||||
DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||
DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:${{ secrets.DB_PORT || 5432 }}/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||
RUST_LOG: info # Optional: configure test log level
|
||||
SECRET_KEY: ${{ secrets.TOKEN_SECRET_KEY }}
|
||||
# Make SSH agent available if tests fetch private dependencies
|
||||
@@ -92,10 +94,10 @@ jobs:
|
||||
name: Rustfmt
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.95
|
||||
toolchain: 1.96.1
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- run: rustup component add rustfmt
|
||||
- run: |
|
||||
@@ -112,10 +114,10 @@ jobs:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.95
|
||||
toolchain: 1.96.1
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- run: rustup component add clippy
|
||||
- run: |
|
||||
@@ -132,10 +134,10 @@ jobs:
|
||||
name: build
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.95
|
||||
toolchain: 1.96.1
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
/textsender-auth
|
||||
|
||||
.env
|
||||
.env.local
|
||||
.env.docker
|
||||
|
||||
Generated
+193
-863
File diff suppressed because it is too large
Load Diff
+11
-16
@@ -1,30 +1,25 @@
|
||||
[package]
|
||||
name = "textsender_auth"
|
||||
version = "0.1.20"
|
||||
version = "0.2.2"
|
||||
edition = "2024"
|
||||
rust-version = "1.95"
|
||||
rust-version = "1.96.1"
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.8.9" }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = { version = "1.0.149" }
|
||||
tokio = { version = "1.52.2", features = ["rt-multi-thread"] }
|
||||
serde_json = { version = "1.0.150" }
|
||||
tokio = { version = "1.52.3", features = ["rt-multi-thread"] }
|
||||
tracing-subscriber = { version = "0.3.23" }
|
||||
tower = { version = "0.5.3", features = ["full"] }
|
||||
tower-http = { version = "0.6.10", features = ["cors"] }
|
||||
hyper = { version = "1.9.0" }
|
||||
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-native-tls", "time", "uuid"] }
|
||||
uuid = { version = "1.23.1", features = ["v4", "serde"] }
|
||||
argon2 = { version = "0.5.3", features = ["std"] } # Use the latest 0.5.x version
|
||||
rand = { version = "0.10.1" }
|
||||
time = { version = "0.3.47", features = ["macros", "serde"] }
|
||||
tower-http = { version = "0.7.0", features = ["cors"] }
|
||||
sqlx = { version = "0.9.0", features = ["runtime-tokio", "tls-native-tls", "postgres", "time", "uuid"] }
|
||||
uuid = { version = "1.23.3", features = ["v4", "serde"] }
|
||||
argon2 = { version = "0.5.3", features = ["std"] }
|
||||
time = { version = "0.3.53", features = ["macros", "serde"] }
|
||||
josekit = { version = "0.10.3" }
|
||||
utoipa = { version = "5.5.0", features = ["axum_extras"] }
|
||||
utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
|
||||
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender-models.git", tag = "v0.3.0" }
|
||||
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.5.1", features = ["config", "user"] }
|
||||
|
||||
[dev-dependencies]
|
||||
http-body-util = { version = "0.1.3" }
|
||||
tower = { version = "0.5.3", features = ["full"] }
|
||||
url = { version = "2.5.8" }
|
||||
once_cell = { version = "1.21.4" } # Useful for lazy initialization in tests/app setup
|
||||
async-std = { version = "1.13.2" }
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
# Stage 1: Build the application
|
||||
# Use a specific Rust version for reproducibility. Choose one that matches your development environment.
|
||||
# Using slim variant for smaller base image
|
||||
FROM rust:1.95 as builder
|
||||
FROM rust:1.96.1 as builder
|
||||
|
||||
# Set the working directory inside the container
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
+5
-4
@@ -23,13 +23,14 @@ services:
|
||||
container_name: textsender_auth_db # Optional: Give the container a specific name
|
||||
environment:
|
||||
# These MUST match the user, password, and database name in the DATABASE_URL above
|
||||
POSTGRES_USER: ${DB_USER:-textsender_op}
|
||||
POSTGRES_PASSWORD: ${DB_PASSWORD:-password}
|
||||
POSTGRES_DB: ${DB_NAME:-textsender_auth_db}
|
||||
POSTGRES_USER: ${DB_AUTH_USER:-textsender_op}
|
||||
POSTGRES_PASSWORD: ${DB_AUTH_PASSWORD:-password}
|
||||
POSTGRES_DB: ${DB_AUTH_NAME:-textsender_auth_db}
|
||||
volumes:
|
||||
# Persist database data using a named volume
|
||||
- postgres_data:/var/lib/postgresql
|
||||
ports: []
|
||||
ports:
|
||||
- "5433:5432"
|
||||
healthcheck:
|
||||
# Checks if Postgres is ready to accept connections
|
||||
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
|
||||
|
||||
+130
-90
@@ -96,11 +96,19 @@ pub mod response {
|
||||
pub async fn extract(
|
||||
response: axum::response::Response,
|
||||
) -> Result<LoginResponse, std::io::Error> {
|
||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.unwrap();
|
||||
let _parsed_body: LoginResponse = serde_json::from_slice(&body).unwrap();
|
||||
todo!("Add code to convert axum::Response to this type");
|
||||
let body = match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
||||
Ok(body) => body,
|
||||
Err(err) => {
|
||||
return Err(std::io::Error::other(err));
|
||||
}
|
||||
};
|
||||
let parsed_body: LoginResponse = match serde_json::from_slice(&body) {
|
||||
Ok(body) => body,
|
||||
Err(err) => {
|
||||
return Err(std::io::Error::other(err));
|
||||
}
|
||||
};
|
||||
Ok(parsed_body)
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
|
||||
@@ -120,6 +128,12 @@ pub mod response {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::user::User>,
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct GetUserProfileResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::user::UserProfile>,
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint for a user login
|
||||
@@ -186,13 +200,22 @@ pub async fn user_login(
|
||||
Ok(matches) => {
|
||||
if matches {
|
||||
// Create token
|
||||
let key = textsender_models::envy::environment::get_secret_key()
|
||||
.await
|
||||
.value;
|
||||
let (token_literal, duration) =
|
||||
token_stuff::create_token(&key, &user.id).unwrap();
|
||||
let key =
|
||||
textsender_models::envy::environment::get_secret_key().value;
|
||||
let cst = match token_stuff::create_token(&key, &user.id) {
|
||||
Ok(token) => token,
|
||||
Err(_err) => {
|
||||
return (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::LoginResponse {
|
||||
message: String::from("Error creating token"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
if token_stuff::verify_token(&key, &token_literal) {
|
||||
if token_stuff::verify_token(&key, &cst.access_token) {
|
||||
let current_time = time::OffsetDateTime::now_utc();
|
||||
let _ =
|
||||
repo::user::update_last_login(&pool, &user, ¤t_time)
|
||||
@@ -204,12 +227,12 @@ pub async fn user_login(
|
||||
message: String::from("Successful"),
|
||||
data: vec![textsender_models::token::LoginResult {
|
||||
user_id: user.id,
|
||||
access_token: token_literal,
|
||||
access_token: cst.access_token,
|
||||
token_type: String::from(
|
||||
textsender_models::token::TOKEN_TYPE,
|
||||
),
|
||||
issued_at: duration,
|
||||
..Default::default()
|
||||
issued_at: cst.issued,
|
||||
expires_in: cst.expires_in,
|
||||
}],
|
||||
}),
|
||||
)
|
||||
@@ -327,13 +350,12 @@ pub async fn service_user_login(
|
||||
if matches {
|
||||
// Create token
|
||||
println!("Creating token");
|
||||
let key = textsender_models::envy::environment::get_secret_key()
|
||||
.await
|
||||
.value;
|
||||
let (token_literal, duration) =
|
||||
let key =
|
||||
textsender_models::envy::environment::get_secret_key().value;
|
||||
let cst =
|
||||
token_stuff::create_token(&key, &service_user.id).unwrap();
|
||||
|
||||
if token_stuff::verify_token(&key, &token_literal) {
|
||||
if token_stuff::verify_token(&key, &cst.access_token) {
|
||||
let current_time = time::OffsetDateTime::now_utc();
|
||||
let _ = repo::service::update_last_login(
|
||||
&pool,
|
||||
@@ -350,12 +372,12 @@ pub async fn service_user_login(
|
||||
),
|
||||
data: vec![textsender_models::token::LoginResult {
|
||||
user_id: service_user.id,
|
||||
access_token: token_literal,
|
||||
access_token: cst.access_token,
|
||||
token_type: String::from(
|
||||
textsender_models::token::TOKEN_TYPE,
|
||||
),
|
||||
issued_at: duration,
|
||||
..Default::default()
|
||||
issued_at: cst.issued,
|
||||
expires_in: cst.expires_in,
|
||||
}],
|
||||
}),
|
||||
)
|
||||
@@ -434,18 +456,14 @@ pub async fn refresh_token(
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
let key = textsender_models::envy::environment::get_secret_key()
|
||||
.await
|
||||
.value;
|
||||
let key = textsender_models::envy::environment::get_secret_key().value;
|
||||
if token_stuff::verify_token(&key, &payload.access_token) {
|
||||
match token_stuff::extract_id_from_token(&key, &payload.access_token) {
|
||||
Ok(id) => {
|
||||
let generate_service_token = |id, key| -> (Option<String>, Option<i64>) {
|
||||
match token_stuff::create_service_refresh_token(key, id) {
|
||||
Ok((token, issued)) => (Some(token), Some(issued)),
|
||||
Err(_err) => (None, None),
|
||||
}
|
||||
};
|
||||
let generate_service_token =
|
||||
|id, key| -> Option<textsender_models::token::CreateTokenResult> {
|
||||
token_stuff::create_service_refresh_token(key, id).ok()
|
||||
};
|
||||
|
||||
let mut response = response::RefreshTokenResponse {
|
||||
message: String::new(),
|
||||
@@ -453,21 +471,43 @@ pub async fn refresh_token(
|
||||
};
|
||||
|
||||
match repo::user::get_with_id(&pool, &id).await {
|
||||
Ok(_user) => {
|
||||
let (refresh_token, issued) = generate_service_token(&id, &key);
|
||||
match refresh_token {
|
||||
Some(token) => match issued {
|
||||
Some(issued_at) => {
|
||||
Ok(_user) => match generate_service_token(&id, &key) {
|
||||
Some(cst) => {
|
||||
response.message =
|
||||
String::from(super::messages::SUCCESSFUL_MESSAGE);
|
||||
let lr = textsender_models::token::LoginResult {
|
||||
user_id: id,
|
||||
access_token: cst.access_token,
|
||||
issued_at: cst.issued,
|
||||
expires_in: cst.expires_in,
|
||||
token_type: String::from(textsender_models::token::TOKEN_TYPE),
|
||||
};
|
||||
response.data.push(lr);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
None => (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::RefreshTokenResponse {
|
||||
message: String::from("Refresh token not generated"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
),
|
||||
},
|
||||
Err(err) => {
|
||||
println!("Unable to find user, checking service user: {err:?}");
|
||||
match repo::service::get(&pool, &id).await {
|
||||
Ok(_service_user) => match generate_service_token(&id, &key) {
|
||||
Some(cst) => {
|
||||
response.message =
|
||||
String::from(super::messages::SUCCESSFUL_MESSAGE);
|
||||
let lr = textsender_models::token::LoginResult {
|
||||
user_id: id,
|
||||
access_token: token,
|
||||
issued_at,
|
||||
access_token: cst.access_token,
|
||||
issued_at: cst.issued,
|
||||
expires_in: cst.expires_in,
|
||||
token_type: String::from(
|
||||
textsender_models::token::TOKEN_TYPE,
|
||||
),
|
||||
..Default::default()
|
||||
};
|
||||
response.data.push(lr);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
@@ -480,57 +520,6 @@ pub async fn refresh_token(
|
||||
}),
|
||||
),
|
||||
},
|
||||
None => (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::RefreshTokenResponse {
|
||||
message: String::from("Refresh token not generated"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Unable to find user, checking service user: {err:?}");
|
||||
match repo::service::get(&pool, &id).await {
|
||||
Ok(_service_user) => {
|
||||
let (refresh_token, issued) = generate_service_token(&id, &key);
|
||||
match refresh_token {
|
||||
Some(token) => match issued {
|
||||
Some(issued_at) => {
|
||||
response.message = String::from(
|
||||
super::messages::SUCCESSFUL_MESSAGE,
|
||||
);
|
||||
let lr = textsender_models::token::LoginResult {
|
||||
user_id: id,
|
||||
access_token: token,
|
||||
issued_at,
|
||||
token_type: String::from(
|
||||
textsender_models::token::TOKEN_TYPE,
|
||||
),
|
||||
..Default::default()
|
||||
};
|
||||
response.data.push(lr);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
None => (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::RefreshTokenResponse {
|
||||
message: String::from("Issued at not returned"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
),
|
||||
},
|
||||
None => (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::RefreshTokenResponse {
|
||||
message: String::from(
|
||||
"Refresh token not generated",
|
||||
),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
),
|
||||
}
|
||||
}
|
||||
Err(err) => (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::RefreshTokenResponse {
|
||||
@@ -828,3 +817,54 @@ pub async fn update_name_of_user(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to get User Profile
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = super::endpoints::GET_USER_PROFILE,
|
||||
params(("id" = uuid::Uuid, Path, description = "User Id")),
|
||||
responses(
|
||||
(status = 200, description = "Deleted", body = response::GetUserProfileResponse),
|
||||
(status = 400, description = "Bad request", body = response::GetUserProfileResponse),
|
||||
(status = 500, description = "Error", body = response::GetUserProfileResponse)
|
||||
)
|
||||
)]
|
||||
pub async fn get_user_profile(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<response::GetUserProfileResponse>,
|
||||
) {
|
||||
let mut response = response::GetUserProfileResponse::default();
|
||||
if id.is_nil() {
|
||||
response.message = String::from("Invalid");
|
||||
return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response));
|
||||
}
|
||||
|
||||
match repo::user::get_with_id(&pool, &id).await {
|
||||
Ok(user) => {
|
||||
let user_profile = textsender_models::user::UserProfile {
|
||||
user_id: user.id,
|
||||
phone_number: user.phone_number,
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
last_login: user.last_login,
|
||||
created: user.created,
|
||||
username: user.username,
|
||||
};
|
||||
response.message = String::from(super::messages::SUCCESSFUL_MESSAGE);
|
||||
response.data.push(user_profile);
|
||||
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = String::from("Bad request");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,6 @@ pub mod endpoints {
|
||||
pub const UPDATE_PASSWORD: &str = "/api/v1/user/password/update";
|
||||
/// Endpoint constant for updating user's name
|
||||
pub const UPDATE_USER_NAME: &str = "/api/v1/user/name/update";
|
||||
/// Endpoint constant for getting user profile
|
||||
pub const GET_USER_PROFILE: &str = "/api/v1/user/profile/{id}";
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ pub fn generate_the_salt() -> (
|
||||
responses(
|
||||
(status = 201, description = "User created", body = response::Response),
|
||||
(status = 404, description = "User already exists", body = response::Response),
|
||||
(status = 400, description = "Issue creating user", body = response::Response)
|
||||
(status = 500, description = "Issue creating user", body = response::Response)
|
||||
)
|
||||
)]
|
||||
pub async fn register_user(
|
||||
@@ -105,9 +105,7 @@ pub async fn register_user(
|
||||
let mut user = textsender_models::user::User {
|
||||
username: payload.username.clone(),
|
||||
password: payload.password.clone(),
|
||||
// email: payload.email.clone(),
|
||||
phone_number: payload.phone_number.clone(),
|
||||
// email_verified: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -183,7 +181,7 @@ pub async fn register_user(
|
||||
/// 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 = textsender_models::envy::environment::get_env(&key).await;
|
||||
let var = textsender_models::envy::environment::get_env(&key);
|
||||
let parsed_value = var.value.to_uppercase();
|
||||
|
||||
if parsed_value == "TRUE" {
|
||||
|
||||
+1
-3
@@ -1,9 +1,7 @@
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
|
||||
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||
let database_url = textsender_models::envy::environment::get_db_url()
|
||||
.await
|
||||
.value;
|
||||
let database_url = textsender_models::envy::environment::get_db_url().value;
|
||||
println!("Database url: {database_url}");
|
||||
|
||||
PgPoolOptions::new()
|
||||
|
||||
+18
-6
@@ -24,9 +24,12 @@ pub mod init {
|
||||
paths(
|
||||
common_callers::endpoint::db_ping, common_callers::endpoint::root,
|
||||
register_caller::register_user, login_caller::user_login,
|
||||
login_caller::get_user_profile,
|
||||
),
|
||||
components(schemas(common_callers::response::TestResult,
|
||||
register_responses::Response, login_responses::LoginResponse)),
|
||||
register_responses::Response, login_responses::LoginResponse,
|
||||
login_responses::GetUserProfileResponse,
|
||||
)),
|
||||
tags(
|
||||
(name = "TextSender Auth API", description = "Auth API for TextSender API")
|
||||
)
|
||||
@@ -35,18 +38,18 @@ pub mod init {
|
||||
|
||||
mod cors {
|
||||
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
|
||||
// Start building the CORS layer with common settings
|
||||
let cors = tower_http::cors::CorsLayer::new()
|
||||
.allow_methods([
|
||||
axum::http::Method::GET,
|
||||
axum::http::Method::PATCH,
|
||||
axum::http::Method::POST,
|
||||
axum::http::Method::PUT,
|
||||
axum::http::Method::DELETE,
|
||||
]) // Specify allowed methods:cite[2]
|
||||
])
|
||||
.allow_headers([
|
||||
axum::http::header::CONTENT_TYPE,
|
||||
axum::http::header::AUTHORIZATION,
|
||||
]) // Specify allowed headers:cite[2]
|
||||
])
|
||||
.allow_credentials(true) // If you need to send cookies or authentication headers:cite[2]
|
||||
.max_age(std::time::Duration::from_secs(3600)); // Cache the preflight response for 1 hour:cite[2]
|
||||
|
||||
@@ -54,7 +57,7 @@ pub mod init {
|
||||
match std::env::var(textsender_models::envy::keys::APP_ENV).as_deref() {
|
||||
Ok("production") => {
|
||||
let allowed_origins_env =
|
||||
textsender_models::envy::environment::get_allowed_origins().await;
|
||||
textsender_models::envy::environment::get_allowed_origins();
|
||||
match textsender_models::envy::utility::delimitize(&allowed_origins_env) {
|
||||
Ok(alwd) => {
|
||||
let allowed_origins: Vec<axum::http::HeaderValue> = alwd
|
||||
@@ -76,6 +79,12 @@ pub mod init {
|
||||
cors.allow_origin(vec![
|
||||
"http://localhost:4200".parse().unwrap(),
|
||||
"http://127.0.0.1:4200".parse().unwrap(),
|
||||
"http://localhost:5173".parse().unwrap(),
|
||||
"http://127.0.0.1:5173".parse().unwrap(),
|
||||
"http://localhost:9080".parse().unwrap(),
|
||||
"http://127.0.0.1:9080".parse().unwrap(),
|
||||
"http://localhost:9081".parse().unwrap(),
|
||||
"http://127.0.0.1:9081".parse().unwrap(),
|
||||
])
|
||||
}
|
||||
}
|
||||
@@ -83,7 +92,6 @@ pub mod init {
|
||||
}
|
||||
|
||||
pub async fn routes() -> Router {
|
||||
// build our application with a route
|
||||
Router::new()
|
||||
.route(
|
||||
callers::endpoints::DBTEST,
|
||||
@@ -118,6 +126,10 @@ pub mod init {
|
||||
callers::endpoints::UPDATE_USER_NAME,
|
||||
patch(callers::login::update_name_of_user),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::GET_USER_PROFILE,
|
||||
get(callers::login::get_user_profile),
|
||||
)
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
|
||||
+23
-26
@@ -17,45 +17,45 @@ pub fn get_expiration(issued: &time::OffsetDateTime) -> Result<time::OffsetDateT
|
||||
}
|
||||
|
||||
pub fn create_token(
|
||||
provided_key: &String,
|
||||
provided_key: &str,
|
||||
id: &uuid::Uuid,
|
||||
) -> Result<(String, i64), josekit::JoseError> {
|
||||
) -> Result<textsender_models::token::CreateTokenResult, josekit::JoseError> {
|
||||
let resource = textsender_models::token::TokenResource {
|
||||
message: String::from(MESSAGE),
|
||||
issuer: String::from(ISSUER),
|
||||
audiences: vec![String::from(AUDIENCE)],
|
||||
id: *id,
|
||||
user_id: *id,
|
||||
};
|
||||
textsender_models::token::create_token(provided_key, &resource, time::Duration::hours(4))
|
||||
textsender_models::token::create_token(provided_key, resource, time::Duration::hours(4))
|
||||
}
|
||||
|
||||
pub fn create_service_token(
|
||||
provided: &String,
|
||||
provided: &str,
|
||||
id: &uuid::Uuid,
|
||||
) -> Result<(String, i64), josekit::JoseError> {
|
||||
) -> Result<textsender_models::token::CreateTokenResult, josekit::JoseError> {
|
||||
let resource = textsender_models::token::TokenResource {
|
||||
message: String::from(SERVICE_SUBJECT),
|
||||
issuer: String::from(ISSUER),
|
||||
audiences: vec![String::from(AUDIENCE)],
|
||||
id: *id,
|
||||
user_id: *id,
|
||||
};
|
||||
textsender_models::token::create_token(provided, &resource, time::Duration::hours(1))
|
||||
textsender_models::token::create_token(provided, resource, time::Duration::hours(1))
|
||||
}
|
||||
|
||||
pub fn create_service_refresh_token(
|
||||
key: &String,
|
||||
key: &str,
|
||||
id: &uuid::Uuid,
|
||||
) -> Result<(String, i64), josekit::JoseError> {
|
||||
) -> Result<textsender_models::token::CreateTokenResult, josekit::JoseError> {
|
||||
let resource = textsender_models::token::TokenResource {
|
||||
message: String::from(SERVICE_SUBJECT),
|
||||
issuer: String::from(ISSUER),
|
||||
audiences: vec![String::from(AUDIENCE)],
|
||||
id: *id,
|
||||
user_id: *id,
|
||||
};
|
||||
textsender_models::token::create_token(key, &resource, time::Duration::hours(4))
|
||||
textsender_models::token::create_token(key, resource, time::Duration::hours(4))
|
||||
}
|
||||
|
||||
pub fn verify_token(key: &String, token: &String) -> bool {
|
||||
pub fn verify_token(key: &str, token: &str) -> bool {
|
||||
match get_payload(key, token) {
|
||||
Ok((payload, _header)) => match payload.subject() {
|
||||
Some(_sub) => true,
|
||||
@@ -65,9 +65,9 @@ pub fn verify_token(key: &String, token: &String) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_id_from_token(key: &String, token: &String) -> Result<uuid::Uuid, std::io::Error> {
|
||||
pub fn extract_id_from_token(key: &str, token: &str) -> Result<uuid::Uuid, std::io::Error> {
|
||||
match get_payload(key, token) {
|
||||
Ok((payload, _header)) => match payload.claim("id") {
|
||||
Ok((payload, _header)) => match payload.claim("user_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())),
|
||||
@@ -83,7 +83,7 @@ pub const APP_SUBJECT: &str = "Something random";
|
||||
pub const SERVICE_TOKEN_TYPE: &str = "Textsender_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: &str, token: &str) -> Result<String, std::io::Error> {
|
||||
match get_payload(key, token) {
|
||||
Ok((payload, _header)) => match payload.subject() {
|
||||
Some(subject) => {
|
||||
@@ -101,13 +101,13 @@ pub fn get_token_type(key: &String, token: &String) -> Result<String, std::io::E
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_token_type_valid(token_type: &String) -> bool {
|
||||
pub fn is_token_type_valid(token_type: &str) -> bool {
|
||||
token_type == SERVICE_TOKEN_TYPE
|
||||
}
|
||||
|
||||
fn get_payload(
|
||||
key: &String,
|
||||
token: &String,
|
||||
key: &str,
|
||||
token: &str,
|
||||
) -> Result<(josekit::jwt::JwtPayload, josekit::jws::JwsHeader), josekit::JoseError> {
|
||||
let ver = Hs256.verifier_from_bytes(key.as_bytes()).unwrap();
|
||||
jwt::decode_with_verifier(token, &ver)
|
||||
@@ -119,14 +119,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_tokenize() {
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
let special_key = rt
|
||||
.block_on(textsender_models::envy::environment::get_secret_key())
|
||||
.value;
|
||||
let special_key = textsender_models::envy::environment::get_secret_key();
|
||||
let id = uuid::Uuid::new_v4();
|
||||
match create_token(&special_key, &id) {
|
||||
Ok((token, _duration)) => {
|
||||
let result = verify_token(&special_key, &token);
|
||||
match create_token(&special_key.value, &id) {
|
||||
Ok(cst) => {
|
||||
let result = verify_token(&special_key.value, &cst.access_token);
|
||||
assert!(result, "Token not verified");
|
||||
}
|
||||
Err(err) => {
|
||||
|
||||
+114
-11
@@ -9,9 +9,7 @@ mod db_mgr {
|
||||
pub const LIMIT: usize = 6;
|
||||
|
||||
pub async fn get_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||
let tm_db_url = textsender_models::envy::environment::get_db_url()
|
||||
.await
|
||||
.value;
|
||||
let tm_db_url = textsender_models::envy::environment::get_db_url().value;
|
||||
let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap();
|
||||
sqlx::PgPool::connect_with(tm_options).await
|
||||
}
|
||||
@@ -23,9 +21,7 @@ mod db_mgr {
|
||||
}
|
||||
|
||||
pub async fn connect_to_db(db_name: &str) -> Result<sqlx::PgPool, sqlx::Error> {
|
||||
let db_url = textsender_models::envy::environment::get_db_url()
|
||||
.await
|
||||
.value;
|
||||
let db_url = textsender_models::envy::environment::get_db_url().value;
|
||||
let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name);
|
||||
sqlx::PgPool::connect_with(options).await
|
||||
}
|
||||
@@ -35,7 +31,10 @@ mod db_mgr {
|
||||
db_name: &str,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
let create_query = format!("CREATE DATABASE {}", db_name);
|
||||
match sqlx::query(&create_query).execute(template_pool).await {
|
||||
match sqlx::query(sqlx::AssertSqlSafe(create_query))
|
||||
.execute(template_pool)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
@@ -47,14 +46,14 @@ mod db_mgr {
|
||||
db_name: &str,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
let drop_query = format!("DROP DATABASE IF EXISTS {} WITH (FORCE)", db_name);
|
||||
sqlx::query(&drop_query).execute(template_pool).await?;
|
||||
sqlx::query(sqlx::AssertSqlSafe(drop_query))
|
||||
.execute(template_pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_database_name() -> Result<String, Box<dyn std::error::Error>> {
|
||||
let database_url = textsender_models::envy::environment::get_db_url()
|
||||
.await
|
||||
.value;
|
||||
let database_url = textsender_models::envy::environment::get_db_url().value;
|
||||
|
||||
let parsed_url = url::Url::parse(&database_url)?;
|
||||
if parsed_url.scheme() == "postgres" || parsed_url.scheme() == "postgresql" {
|
||||
@@ -225,6 +224,29 @@ pub mod requests {
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
/// Function to call get user profile endpoint
|
||||
pub async fn get_user_profile(
|
||||
app: &axum::Router,
|
||||
user_id: &uuid::Uuid,
|
||||
) -> Result<axum::response::Response, axum::http::Error> {
|
||||
let url = super::util::format_url_with_value(
|
||||
textsender_auth::callers::endpoints::GET_USER_PROFILE,
|
||||
user_id,
|
||||
);
|
||||
match axum::http::Request::builder()
|
||||
.method(axum::http::Method::GET)
|
||||
.uri(url)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.body(axum::body::Body::empty())
|
||||
{
|
||||
Ok(req) => match app.clone().oneshot(req).await {
|
||||
Ok(resp) => Ok(resp),
|
||||
Err(err) => Err(axum::http::Error::from(err)),
|
||||
},
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Test user firstname
|
||||
@@ -272,6 +294,11 @@ mod util {
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_url_with_value(endpoint: &str, value: &uuid::Uuid) -> String {
|
||||
let last = endpoint.len() - 5;
|
||||
format!("{}/{value}", &endpoint[0..last])
|
||||
}
|
||||
}
|
||||
|
||||
mod flow {
|
||||
@@ -989,3 +1016,79 @@ async fn test_update_name_of_password() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_user_profile() {
|
||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||
let db_name = db_mgr::generate_db_name().await;
|
||||
|
||||
match db_mgr::create_database(&tm_pool, &db_name).await {
|
||||
Ok(_) => {
|
||||
println!("Success");
|
||||
}
|
||||
Err(e) => {
|
||||
assert!(false, "Error: {:?}", e.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||
|
||||
db::init::migrations(&pool).await;
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
|
||||
let user_id = match flow::register_user(&app).await {
|
||||
Ok(user) => match flow::login_user(&app, &user.username, TEST_PASSWORD).await {
|
||||
Ok(login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
login_result.access_token.is_empty(),
|
||||
"Access token is empty when it should not be"
|
||||
);
|
||||
login_result.user_id
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
uuid::Uuid::nil()
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
uuid::Uuid::nil()
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(false, user_id.is_nil(), "User Id should not be empty");
|
||||
|
||||
match requests::get_user_profile(&app, &user_id).await {
|
||||
Ok(response) => {
|
||||
match util::convert_response::<
|
||||
textsender_auth::callers::login::response::GetUserProfileResponse,
|
||||
>(response)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
assert_eq!(false, response.data.is_empty(), "No User Profile found");
|
||||
let user_profile = &response.data[0];
|
||||
assert_eq!(
|
||||
TEST_USERNAME, user_profile.username,
|
||||
"Username does not match"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
match db_mgr::drop_database(&tm_pool, &db_name).await {
|
||||
Ok(()) => {}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user