Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b678c098cb | ||
|
|
e3ee24c131 | ||
|
|
8e0b8b737b |
Generated
+1
-1
@@ -2213,7 +2213,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "textsender_auth"
|
name = "textsender_auth"
|
||||||
version = "0.1.17"
|
version = "0.1.20"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"argon2",
|
"argon2",
|
||||||
"async-std",
|
"async-std",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "textsender_auth"
|
name = "textsender_auth"
|
||||||
version = "0.1.17"
|
version = "0.1.20"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.95"
|
rust-version = "1.95"
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,51 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, utoipa::ToSchema)]
|
||||||
|
pub struct RefreshTokenRequest {
|
||||||
|
pub access_token: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, utoipa::ToSchema)]
|
||||||
|
pub struct UpdatePasswordRequest {
|
||||||
|
pub user_id: uuid::Uuid,
|
||||||
|
pub current_password: String,
|
||||||
|
pub updated_password: String,
|
||||||
|
pub confirmed_password: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UpdatePasswordRequest {
|
||||||
|
pub fn is_valid(&self) -> bool {
|
||||||
|
if self.user_id.is_nil()
|
||||||
|
|| self.current_password.is_empty()
|
||||||
|
|| self.updated_password.is_empty()
|
||||||
|
|| self.confirmed_password.is_empty()
|
||||||
|
{
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
self.updated_password == self.confirmed_password
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, utoipa::ToSchema)]
|
||||||
|
pub struct UserUpdateNameRequest {
|
||||||
|
pub user_id: uuid::Uuid,
|
||||||
|
pub firstname: String,
|
||||||
|
pub lastname: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UserUpdateNameRequest {
|
||||||
|
pub fn is_valid(&self) -> (bool, Option<String>) {
|
||||||
|
if self.user_id.is_nil() || self.firstname.is_empty() || self.lastname.is_empty() {
|
||||||
|
let reason = String::from("Missing fields");
|
||||||
|
(false, Some(reason))
|
||||||
|
} else {
|
||||||
|
(true, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
@@ -57,6 +102,24 @@ pub mod response {
|
|||||||
let _parsed_body: LoginResponse = serde_json::from_slice(&body).unwrap();
|
let _parsed_body: LoginResponse = serde_json::from_slice(&body).unwrap();
|
||||||
todo!("Add code to convert axum::Response to this type");
|
todo!("Add code to convert axum::Response to this type");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
|
pub struct RefreshTokenResponse {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<textsender_models::token::LoginResult>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
|
pub struct UpdatePasswordResponse {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<uuid::Uuid>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
|
pub struct UserUpdateNameResponse {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<textsender_models::user::User>,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Endpoint for a user login
|
/// Endpoint for a user login
|
||||||
@@ -337,3 +400,431 @@ pub async fn service_user_login(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint for service user login
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::endpoints::REFRESH_TOKEN,
|
||||||
|
request_body(
|
||||||
|
content = request::RefreshTokenRequest,
|
||||||
|
description = "Data required refresh token",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Service uuser login successful", body = response::RefreshTokenResponse),
|
||||||
|
(status = 400, description = "Bad data", body = response::RefreshTokenResponse),
|
||||||
|
(status = 500, description = "Something went wrong", body = response::RefreshTokenResponse)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub async fn refresh_token(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::Json(payload): axum::Json<request::RefreshTokenRequest>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<response::RefreshTokenResponse>,
|
||||||
|
) {
|
||||||
|
if payload.access_token.is_empty() {
|
||||||
|
let reason = String::from("Access token not provided");
|
||||||
|
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::BAD_REQUEST,
|
||||||
|
axum::Json(response::RefreshTokenResponse {
|
||||||
|
message: reason,
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
let key = textsender_models::envy::environment::get_secret_key()
|
||||||
|
.await
|
||||||
|
.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 mut response = response::RefreshTokenResponse {
|
||||||
|
message: String::new(),
|
||||||
|
data: Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
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) => {
|
||||||
|
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) => {
|
||||||
|
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 {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::RefreshTokenResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::RefreshTokenResponse {
|
||||||
|
message: String::from("Unable to verify token"),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Endpoint for a updating password
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = super::endpoints::UPDATE_PASSWORD,
|
||||||
|
request_body(
|
||||||
|
content = request::UpdatePasswordRequest,
|
||||||
|
description = "Data required to update password",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "User login successful", body = response::UpdatePasswordResponse),
|
||||||
|
(status = 400, description = "Bad data", body = response::UpdatePasswordResponse),
|
||||||
|
(status = 500, description = "Something went wrong", body = response::UpdatePasswordResponse)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub async fn update_password(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::Json(payload): axum::Json<request::UpdatePasswordRequest>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<response::UpdatePasswordResponse>,
|
||||||
|
) {
|
||||||
|
if !payload.is_valid() {
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::BAD_REQUEST,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: String::from("Invalid passwords"),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
let verify_password = |current_password, hashed_password| -> Result<bool, std::io::Error> {
|
||||||
|
match hashing::verify_password(current_password, hashed_password) {
|
||||||
|
Ok(matches) => Ok(matches),
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match repo::user::get_with_id(&pool, &payload.user_id).await {
|
||||||
|
Ok(user) => {
|
||||||
|
let hashed_password = user.password.clone();
|
||||||
|
match verify_password(&payload.current_password, hashed_password) {
|
||||||
|
Ok(matches) => {
|
||||||
|
if matches {
|
||||||
|
let (generate_salt, mut salt) = super::register::generate_the_salt();
|
||||||
|
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
||||||
|
let updated_hashed_password = match hashing::hash_password(
|
||||||
|
&payload.updated_password,
|
||||||
|
&generate_salt,
|
||||||
|
) {
|
||||||
|
Ok(hashed) => hashed,
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
String::new()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match repo::user::update_password(
|
||||||
|
&pool,
|
||||||
|
&user,
|
||||||
|
&updated_hashed_password,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(()) => (
|
||||||
|
axum::http::StatusCode::OK,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: String::from(super::messages::SUCCESSFUL_MESSAGE),
|
||||||
|
data: vec![user.id],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
Err(err) => (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: String::from("Issue updating password"),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
println!("No User found, trying Service User: {err:?}");
|
||||||
|
|
||||||
|
// Try service user
|
||||||
|
match repo::service::get(&pool, &payload.user_id).await {
|
||||||
|
Ok(service_user) => {
|
||||||
|
let hashed_password = service_user.passphrase.clone();
|
||||||
|
match verify_password(&payload.current_password, hashed_password) {
|
||||||
|
Ok(matches) => {
|
||||||
|
if matches {
|
||||||
|
let (generate_salt, mut salt) =
|
||||||
|
super::register::generate_the_salt();
|
||||||
|
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
|
||||||
|
let updated_hashed_password = match hashing::hash_password(
|
||||||
|
&payload.updated_password,
|
||||||
|
&generate_salt,
|
||||||
|
) {
|
||||||
|
Ok(hashed) => hashed,
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
String::new()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match repo::service::update_passphrase(
|
||||||
|
&pool,
|
||||||
|
&service_user,
|
||||||
|
&updated_hashed_password,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(()) => (
|
||||||
|
axum::http::StatusCode::OK,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: String::from(
|
||||||
|
super::messages::SUCCESSFUL_MESSAGE,
|
||||||
|
),
|
||||||
|
data: vec![service_user.id],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
Err(err) => (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: String::from("Issue updating password"),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::UpdatePasswordResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Endpoint for a updating password
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = super::endpoints::UPDATE_USER_NAME,
|
||||||
|
request_body(
|
||||||
|
content = request::UserUpdateNameRequest,
|
||||||
|
description = "Data required to update name of a user",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Names were updated", body = response::UserUpdateNameResponse),
|
||||||
|
(status = 304, description = "Nothing to change", body = response::UserUpdateNameResponse),
|
||||||
|
(status = 400, description = "Bad data", body = response::UserUpdateNameResponse),
|
||||||
|
(status = 500, description = "Something went wrong", body = response::UserUpdateNameResponse)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub async fn update_name_of_user(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::Json(payload): axum::Json<request::UserUpdateNameRequest>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<response::UserUpdateNameResponse>,
|
||||||
|
) {
|
||||||
|
let (valid_request, reason) = payload.is_valid();
|
||||||
|
if !valid_request {
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::BAD_REQUEST,
|
||||||
|
axum::Json(response::UserUpdateNameResponse {
|
||||||
|
message: reason.unwrap_or_default(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
match repo::user::get_with_id(&pool, &payload.user_id).await {
|
||||||
|
Ok(user) => {
|
||||||
|
if user.firstname == payload.firstname || user.lastname == payload.lastname {
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::NOT_MODIFIED,
|
||||||
|
axum::Json(response::UserUpdateNameResponse {
|
||||||
|
message: String::from("No change"),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
match repo::user::update_name(
|
||||||
|
&pool,
|
||||||
|
&user.id,
|
||||||
|
&payload.firstname,
|
||||||
|
&payload.lastname,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => (
|
||||||
|
axum::http::StatusCode::OK,
|
||||||
|
axum::Json(response::UserUpdateNameResponse {
|
||||||
|
message: String::from(super::messages::SUCCESSFUL_MESSAGE),
|
||||||
|
data: vec![textsender_models::user::User {
|
||||||
|
id: user.id,
|
||||||
|
phone_number: user.phone_number,
|
||||||
|
firstname: payload.firstname,
|
||||||
|
lastname: payload.lastname,
|
||||||
|
username: user.username,
|
||||||
|
created: user.created,
|
||||||
|
last_login: user.last_login,
|
||||||
|
..Default::default()
|
||||||
|
}],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
Err(err) => (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::UserUpdateNameResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::UserUpdateNameResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,4 +13,10 @@ pub mod endpoints {
|
|||||||
pub const REGISTER_SERVICE_USER: &str = "/api/v1/service/register";
|
pub const REGISTER_SERVICE_USER: &str = "/api/v1/service/register";
|
||||||
/// Endpoint constant for service login user
|
/// Endpoint constant for service login user
|
||||||
pub const LOGIN_SERVICE_USER: &str = "/api/v1/service/login";
|
pub const LOGIN_SERVICE_USER: &str = "/api/v1/service/login";
|
||||||
|
/// Endpoint constant for refresh token
|
||||||
|
pub const REFRESH_TOKEN: &str = "/api/v1/token/refresh";
|
||||||
|
/// Endpoint constant for updating password
|
||||||
|
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";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_the_salt() -> (
|
pub fn generate_the_salt() -> (
|
||||||
argon2::password_hash::SaltString,
|
argon2::password_hash::SaltString,
|
||||||
textsender_models::user::Salt,
|
textsender_models::user::Salt,
|
||||||
) {
|
) {
|
||||||
|
|||||||
+13
-1
@@ -8,7 +8,7 @@ pub mod token_stuff;
|
|||||||
pub mod init {
|
pub mod init {
|
||||||
use axum::{
|
use axum::{
|
||||||
Router,
|
Router,
|
||||||
routing::{get, post},
|
routing::{get, patch, post},
|
||||||
};
|
};
|
||||||
use utoipa::OpenApi;
|
use utoipa::OpenApi;
|
||||||
|
|
||||||
@@ -106,6 +106,18 @@ pub mod init {
|
|||||||
callers::endpoints::LOGIN_SERVICE_USER,
|
callers::endpoints::LOGIN_SERVICE_USER,
|
||||||
post(callers::login::service_user_login),
|
post(callers::login::service_user_login),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
callers::endpoints::REFRESH_TOKEN,
|
||||||
|
post(callers::login::refresh_token),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
callers::endpoints::UPDATE_PASSWORD,
|
||||||
|
patch(callers::login::update_password),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
callers::endpoints::UPDATE_USER_NAME,
|
||||||
|
patch(callers::login::update_name_of_user),
|
||||||
|
)
|
||||||
.layer(cors::configure_cors().await)
|
.layer(cors::configure_cors().await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,36 @@ pub mod user {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_with_id(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<textsender_models::user::User, sqlx::Error> {
|
||||||
|
match sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT id, username, password, phone_number, salt_id, firstname, lastname, created, last_login FROM "user" WHERE id = $1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await {
|
||||||
|
Ok(r) => match r {
|
||||||
|
Some(r) => Ok(textsender_models::user::User {
|
||||||
|
id: r.try_get("id")?,
|
||||||
|
username: r.try_get("username")?,
|
||||||
|
password: r.try_get("password")?,
|
||||||
|
phone_number: r.try_get("phone_number")?,
|
||||||
|
salt_id: r.try_get("salt_id")?,
|
||||||
|
firstname: r.try_get("firstname")?,
|
||||||
|
lastname: r.try_get("lastname")?,
|
||||||
|
created: r.try_get("created")?,
|
||||||
|
last_login: r.try_get("last_login")?,
|
||||||
|
}),
|
||||||
|
None => Err(sqlx::Error::RowNotFound),
|
||||||
|
},
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn update_last_login(
|
pub async fn update_last_login(
|
||||||
pool: &sqlx::PgPool,
|
pool: &sqlx::PgPool,
|
||||||
user: &textsender_models::user::User,
|
user: &textsender_models::user::User,
|
||||||
@@ -74,6 +104,60 @@ pub mod user {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn update_password(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
user: &textsender_models::user::User,
|
||||||
|
updated_hashed_password: &String,
|
||||||
|
) -> Result<(), sqlx::Error> {
|
||||||
|
match sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE "user" SET password = $1 WHERE id = $2
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(updated_hashed_password)
|
||||||
|
.bind(user.id)
|
||||||
|
.execute(pool)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(row) => {
|
||||||
|
if row.rows_affected() > 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(sqlx::Error::RowNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_name(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
firstname: &str,
|
||||||
|
lastname: &str,
|
||||||
|
) -> Result<(), sqlx::Error> {
|
||||||
|
match sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE "user" SET firstname = $1, lastname = $2 WHERE id = $3
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(firstname)
|
||||||
|
.bind(lastname)
|
||||||
|
.bind(id)
|
||||||
|
.execute(pool)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(row) => {
|
||||||
|
if row.rows_affected() > 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(sqlx::Error::RowNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn exists(pool: &sqlx::PgPool, username: &String) -> Result<bool, sqlx::Error> {
|
pub async fn exists(pool: &sqlx::PgPool, username: &String) -> Result<bool, sqlx::Error> {
|
||||||
let result = sqlx::query(
|
let result = sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
|
|||||||
@@ -49,6 +49,42 @@ pub async fn get_passphrase(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<textsender_models::user::ServiceUser, sqlx::Error> {
|
||||||
|
match sqlx::query(
|
||||||
|
r#"SELECT id, username, passphrase, created, last_login, salt_id FROM "service_user" WHERE id = $1"#
|
||||||
|
).bind(id)
|
||||||
|
.fetch_one(pool).await {
|
||||||
|
Ok(row) => {
|
||||||
|
let last_login: Option<time::OffsetDateTime> = match row.try_get("last_login") {
|
||||||
|
Ok(login) => {
|
||||||
|
Some(login)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let service_user = textsender_models::user::ServiceUser {
|
||||||
|
id: row.try_get("id")?,
|
||||||
|
username: row.try_get("username")?,
|
||||||
|
passphrase: row.try_get("passphrase")?,
|
||||||
|
created: row.try_get("created")?,
|
||||||
|
last_login,
|
||||||
|
salt_id: row.try_get("salt_id")?,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(service_user)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_with_username(
|
pub async fn get_with_username(
|
||||||
pool: &sqlx::PgPool,
|
pool: &sqlx::PgPool,
|
||||||
username: &String,
|
username: &String,
|
||||||
@@ -118,6 +154,32 @@ pub async fn update_last_login(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn update_passphrase(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
user: &textsender_models::user::ServiceUser,
|
||||||
|
updated_hashed_passphrase: &String,
|
||||||
|
) -> Result<(), sqlx::Error> {
|
||||||
|
match sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE "service_user" SET passphrase = $1 WHERE id = $2
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(updated_hashed_passphrase)
|
||||||
|
.bind(user.id)
|
||||||
|
.execute(pool)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(row) => {
|
||||||
|
if row.rows_affected() > 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(sqlx::Error::RowNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn insert(
|
pub async fn insert(
|
||||||
pool: &sqlx::PgPool,
|
pool: &sqlx::PgPool,
|
||||||
service_user: &textsender_models::user::ServiceUser,
|
service_user: &textsender_models::user::ServiceUser,
|
||||||
|
|||||||
+509
-18
@@ -134,6 +134,7 @@ pub mod requests {
|
|||||||
app.clone().oneshot(req).await
|
app.clone().oneshot(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Function to call service user login endpoint
|
||||||
pub async fn login_service_user(
|
pub async fn login_service_user(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
username: &str,
|
username: &str,
|
||||||
@@ -152,6 +153,78 @@ pub mod requests {
|
|||||||
|
|
||||||
app.clone().oneshot(req).await
|
app.clone().oneshot(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Function to call token refresh endpoint
|
||||||
|
pub async fn refresh_token(
|
||||||
|
app: &axum::Router,
|
||||||
|
access_token: &str,
|
||||||
|
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"access_token": access_token,
|
||||||
|
});
|
||||||
|
let req = axum::http::Request::builder()
|
||||||
|
.method(axum::http::Method::POST)
|
||||||
|
.uri(super::callers::endpoints::REFRESH_TOKEN)
|
||||||
|
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||||
|
.body(axum::body::Body::from(payload.to_string()))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
app.clone().oneshot(req).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Function to call update password endpoint
|
||||||
|
pub async fn update_password(
|
||||||
|
app: &axum::Router,
|
||||||
|
user_id: &uuid::Uuid,
|
||||||
|
current_password: &str,
|
||||||
|
updated_password: &str,
|
||||||
|
confirmed_password: &str,
|
||||||
|
) -> Result<axum::response::Response, axum::http::Error> {
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"user_id": user_id,
|
||||||
|
"current_password": current_password,
|
||||||
|
"updated_password": updated_password,
|
||||||
|
"confirmed_password": confirmed_password,
|
||||||
|
});
|
||||||
|
match axum::http::Request::builder()
|
||||||
|
.method(axum::http::Method::PATCH)
|
||||||
|
.uri(super::callers::endpoints::UPDATE_PASSWORD)
|
||||||
|
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||||
|
.body(axum::body::Body::from(payload.to_string()))
|
||||||
|
{
|
||||||
|
Ok(req) => match app.clone().oneshot(req).await {
|
||||||
|
Ok(resp) => Ok(resp),
|
||||||
|
Err(err) => Err(axum::http::Error::from(err)),
|
||||||
|
},
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Function to call update name of user endpoint
|
||||||
|
pub async fn update_name_of_user(
|
||||||
|
app: &axum::Router,
|
||||||
|
user_id: &uuid::Uuid,
|
||||||
|
firstname: &str,
|
||||||
|
lastname: &str,
|
||||||
|
) -> Result<axum::response::Response, axum::http::Error> {
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"user_id": user_id,
|
||||||
|
"firstname": firstname,
|
||||||
|
"lastname": lastname,
|
||||||
|
});
|
||||||
|
match axum::http::Request::builder()
|
||||||
|
.method(axum::http::Method::PATCH)
|
||||||
|
.uri(super::callers::endpoints::UPDATE_USER_NAME)
|
||||||
|
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||||
|
.body(axum::body::Body::from(payload.to_string()))
|
||||||
|
{
|
||||||
|
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
|
/// Test user firstname
|
||||||
@@ -164,26 +237,49 @@ const TEST_USERNAME: &str = "BillyBob01";
|
|||||||
const TEST_PASSWORD: &str = "923ndcry392qryudx328qrdy328r";
|
const TEST_PASSWORD: &str = "923ndcry392qryudx328qrdy328r";
|
||||||
/// Test user phone number
|
/// Test user phone number
|
||||||
const TEST_PHONE_NUMBER: &str = "+10123456789";
|
const TEST_PHONE_NUMBER: &str = "+10123456789";
|
||||||
|
/// Test updated user password
|
||||||
|
const TEST_UPDATED_PASSWORD: &str = "3cnf29ry8q27i3yrc928qi37ryndxc2198q7yd9xzq12837e";
|
||||||
|
|
||||||
|
/// Test updated user firstname
|
||||||
|
const TEST_UPDATED_FIRSTNAME: &str = "Kuoth";
|
||||||
|
/// Test updated user lastname
|
||||||
|
const TEST_UPDATED_LASTNAME: &str = "Wech";
|
||||||
|
|
||||||
/// Test service username
|
/// Test service username
|
||||||
const TEST_SERVICE_USERNAME: &str = "swoon";
|
const TEST_SERVICE_USERNAME: &str = "swoon";
|
||||||
/// Test service passphrase
|
/// Test service passphrase
|
||||||
const TEST_SERVICE_PASSPHRASE: &str = "4n5cf349tfy34w857ty39wq45nfdq23";
|
const TEST_SERVICE_PASSPHRASE: &str = "4n5cf349tfy34w857ty39wq45nfdq23";
|
||||||
|
/// Test updated service user passphrase
|
||||||
|
const TEST_SERVICE_UPDATED_PASSPHRASE: &str = "3487ncfyth934287fcrty32487fry32in7";
|
||||||
|
|
||||||
async fn convert_response<T>(response: axum::response::Response) -> Result<T, std::io::Error>
|
mod util {
|
||||||
|
pub async fn convert_response<T>(
|
||||||
|
response: axum::response::Response,
|
||||||
|
) -> Result<T, std::io::Error>
|
||||||
where
|
where
|
||||||
T: serde::de::DeserializeOwned,
|
T: serde::de::DeserializeOwned,
|
||||||
{
|
{
|
||||||
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
||||||
Ok(body) => {
|
Ok(body) => {
|
||||||
let resp: T = serde_json::from_slice(&body).unwrap();
|
let resp: T = match serde_json::from_slice(&body) {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => {
|
||||||
|
return Err(std::io::Error::other(err.to_string()));
|
||||||
|
}
|
||||||
|
};
|
||||||
Ok(resp)
|
Ok(resp)
|
||||||
}
|
}
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn register_user(
|
mod flow {
|
||||||
|
use super::callers;
|
||||||
|
use super::requests;
|
||||||
|
use super::util;
|
||||||
|
|
||||||
|
pub async fn register_user(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
) -> Result<textsender_models::user::User, std::io::Error> {
|
) -> Result<textsender_models::user::User, std::io::Error> {
|
||||||
match requests::register_user(&app).await {
|
match requests::register_user(&app).await {
|
||||||
@@ -194,7 +290,9 @@ async fn register_user(
|
|||||||
response.status()
|
response.status()
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
match convert_response::<callers::register::response::Response>(response).await {
|
match util::convert_response::<callers::register::response::Response>(response)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
if response.data.len() > 0 {
|
if response.data.len() > 0 {
|
||||||
let user = response.data[0].clone();
|
let user = response.data[0].clone();
|
||||||
@@ -211,7 +309,7 @@ async fn register_user(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn login_user(
|
pub async fn login_user(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
username: &str,
|
username: &str,
|
||||||
password: &str,
|
password: &str,
|
||||||
@@ -224,7 +322,11 @@ async fn login_user(
|
|||||||
response.status()
|
response.status()
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
match convert_response::<callers::login::response::LoginResponse>(response).await {
|
match util::convert_response::<callers::login::response::LoginResponse>(
|
||||||
|
response,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
if response.data.len() > 0 {
|
if response.data.len() > 0 {
|
||||||
let user = response.data[0].clone();
|
let user = response.data[0].clone();
|
||||||
@@ -241,7 +343,7 @@ async fn login_user(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn register_service_user(
|
pub async fn register_service_user(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
) -> Result<textsender_models::user::ServiceUser, std::io::Error> {
|
) -> Result<textsender_models::user::ServiceUser, std::io::Error> {
|
||||||
match requests::register_service_user(&app).await {
|
match requests::register_service_user(&app).await {
|
||||||
@@ -252,9 +354,9 @@ async fn register_service_user(
|
|||||||
response.status()
|
response.status()
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
match convert_response::<callers::register::response::RegisterServiceUserResponse>(
|
match util::convert_response::<
|
||||||
response,
|
callers::register::response::RegisterServiceUserResponse,
|
||||||
)
|
>(response)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
@@ -273,7 +375,7 @@ async fn register_service_user(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn login_service_user(
|
pub async fn login_service_user(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
username: &str,
|
username: &str,
|
||||||
passphrase: &str,
|
passphrase: &str,
|
||||||
@@ -286,7 +388,7 @@ async fn login_service_user(
|
|||||||
response.status()
|
response.status()
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
match convert_response::<callers::login::response::ServiceUserLoginResponse>(
|
match util::convert_response::<callers::login::response::ServiceUserLoginResponse>(
|
||||||
response,
|
response,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
@@ -307,6 +409,119 @@ async fn login_service_user(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn refresh_token(
|
||||||
|
app: &axum::Router,
|
||||||
|
access_token: &str,
|
||||||
|
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||||
|
match requests::refresh_token(app, access_token).await {
|
||||||
|
Ok(response) => {
|
||||||
|
if axum::http::StatusCode::OK != response.status() {
|
||||||
|
Err(std::io::Error::other(format!(
|
||||||
|
"Status code is off {:?}",
|
||||||
|
response.status()
|
||||||
|
)))
|
||||||
|
} else {
|
||||||
|
match util::convert_response::<callers::login::response::RefreshTokenResponse>(
|
||||||
|
response,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
if response.data.len() > 0 {
|
||||||
|
let login_result = response.data[0].clone();
|
||||||
|
Ok(login_result)
|
||||||
|
} else {
|
||||||
|
Err(std::io::Error::other("No data returned"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_password(
|
||||||
|
app: &axum::Router,
|
||||||
|
user_id: &uuid::Uuid,
|
||||||
|
current_password: &str,
|
||||||
|
updated_password: &str,
|
||||||
|
confirmed_password: &str,
|
||||||
|
) -> Result<uuid::Uuid, std::io::Error> {
|
||||||
|
match requests::update_password(
|
||||||
|
app,
|
||||||
|
user_id,
|
||||||
|
current_password,
|
||||||
|
updated_password,
|
||||||
|
confirmed_password,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
if axum::http::StatusCode::OK != response.status() {
|
||||||
|
Err(std::io::Error::other(format!(
|
||||||
|
"Status code is off {:?}",
|
||||||
|
response.status()
|
||||||
|
)))
|
||||||
|
} else {
|
||||||
|
match util::convert_response::<callers::login::response::UpdatePasswordResponse>(
|
||||||
|
response,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
if response.data.len() > 0 {
|
||||||
|
let id = response.data[0].clone();
|
||||||
|
Ok(id)
|
||||||
|
} else {
|
||||||
|
Err(std::io::Error::other("No data returned"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_name_of_user(
|
||||||
|
app: &axum::Router,
|
||||||
|
user_id: &uuid::Uuid,
|
||||||
|
firstname: &str,
|
||||||
|
lastname: &str,
|
||||||
|
) -> Result<textsender_models::user::User, std::io::Error> {
|
||||||
|
match requests::update_name_of_user(app, user_id, firstname, lastname).await {
|
||||||
|
Ok(response) => {
|
||||||
|
if axum::http::StatusCode::OK != response.status() {
|
||||||
|
Err(std::io::Error::other(format!(
|
||||||
|
"Status code is off {:?}",
|
||||||
|
response.status()
|
||||||
|
)))
|
||||||
|
} else {
|
||||||
|
match util::convert_response::<callers::login::response::UserUpdateNameResponse>(
|
||||||
|
response,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
if response.data.len() > 0 {
|
||||||
|
let user = response.data[0].clone();
|
||||||
|
Ok(user)
|
||||||
|
} else {
|
||||||
|
Err(std::io::Error::other("No data returned"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_register_user() {
|
async fn test_register_user() {
|
||||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||||
@@ -327,7 +542,7 @@ async fn test_register_user() {
|
|||||||
|
|
||||||
let app = init::routes().await.layer(axum::Extension(pool));
|
let app = init::routes().await.layer(axum::Extension(pool));
|
||||||
|
|
||||||
match register_user(&app).await {
|
match flow::register_user(&app).await {
|
||||||
Ok(returned_user) => {
|
Ok(returned_user) => {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TEST_USERNAME, returned_user.username,
|
TEST_USERNAME, returned_user.username,
|
||||||
@@ -367,8 +582,8 @@ async fn test_login_user() {
|
|||||||
|
|
||||||
let app = init::routes().await.layer(axum::Extension(pool));
|
let app = init::routes().await.layer(axum::Extension(pool));
|
||||||
|
|
||||||
match register_user(&app).await {
|
match flow::register_user(&app).await {
|
||||||
Ok(user) => match login_user(&app, &user.username, TEST_PASSWORD).await {
|
Ok(user) => match flow::login_user(&app, &user.username, TEST_PASSWORD).await {
|
||||||
Ok(login_result) => {
|
Ok(login_result) => {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
false,
|
false,
|
||||||
@@ -415,7 +630,7 @@ async fn test_register_service_user() {
|
|||||||
|
|
||||||
match requests::register_service_user(&app).await {
|
match requests::register_service_user(&app).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
match convert_response::<callers::register::response::RegisterServiceUserResponse>(
|
match util::convert_response::<callers::register::response::RegisterServiceUserResponse>(
|
||||||
response,
|
response,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
@@ -466,14 +681,16 @@ async fn test_login_service_user() {
|
|||||||
|
|
||||||
let app = init::routes().await.layer(axum::Extension(pool));
|
let app = init::routes().await.layer(axum::Extension(pool));
|
||||||
|
|
||||||
match register_service_user(&app).await {
|
match flow::register_service_user(&app).await {
|
||||||
Ok(user) => {
|
Ok(user) => {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
false,
|
false,
|
||||||
user.id.is_nil(),
|
user.id.is_nil(),
|
||||||
"The service user id should not be nil"
|
"The service user id should not be nil"
|
||||||
);
|
);
|
||||||
match login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE).await {
|
match flow::login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(login_result) => {
|
Ok(login_result) => {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
false,
|
false,
|
||||||
@@ -498,3 +715,277 @@ async fn test_login_service_user() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_refresh_token() {
|
||||||
|
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));
|
||||||
|
|
||||||
|
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"
|
||||||
|
);
|
||||||
|
match flow::refresh_token(&app, &login_result.access_token).await {
|
||||||
|
Ok(refresh_login_result) => {
|
||||||
|
assert_eq!(
|
||||||
|
false,
|
||||||
|
refresh_login_result.access_token.is_empty(),
|
||||||
|
"Refreshed access token should not be empty"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match flow::register_service_user(&app).await {
|
||||||
|
Ok(user) => {
|
||||||
|
assert_eq!(
|
||||||
|
false,
|
||||||
|
user.id.is_nil(),
|
||||||
|
"The service user id should not be nil"
|
||||||
|
);
|
||||||
|
match flow::login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(login_result) => {
|
||||||
|
assert_eq!(
|
||||||
|
false,
|
||||||
|
login_result.access_token.is_empty(),
|
||||||
|
"Access token is empty when it should not be"
|
||||||
|
);
|
||||||
|
|
||||||
|
match flow::refresh_token(&app, &login_result.access_token).await {
|
||||||
|
Ok(refresh_login_result) => {
|
||||||
|
assert_eq!(
|
||||||
|
false,
|
||||||
|
refresh_login_result.access_token.is_empty(),
|
||||||
|
"Refreshed access token should not be empty"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_update_password() {
|
||||||
|
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));
|
||||||
|
|
||||||
|
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"
|
||||||
|
);
|
||||||
|
|
||||||
|
match flow::update_password(
|
||||||
|
&app,
|
||||||
|
&user.id,
|
||||||
|
TEST_PASSWORD,
|
||||||
|
TEST_UPDATED_PASSWORD,
|
||||||
|
TEST_UPDATED_PASSWORD,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(id) => {
|
||||||
|
assert_eq!(id, user.id, "Ids do not match");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match flow::register_service_user(&app).await {
|
||||||
|
Ok(service_user) => {
|
||||||
|
assert_eq!(
|
||||||
|
false,
|
||||||
|
service_user.id.is_nil(),
|
||||||
|
"The service user id should not be nil"
|
||||||
|
);
|
||||||
|
match flow::login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(login_result) => {
|
||||||
|
assert_eq!(
|
||||||
|
false,
|
||||||
|
login_result.access_token.is_empty(),
|
||||||
|
"Access token is empty when it should not be"
|
||||||
|
);
|
||||||
|
|
||||||
|
match flow::update_password(
|
||||||
|
&app,
|
||||||
|
&service_user.id,
|
||||||
|
TEST_SERVICE_PASSPHRASE,
|
||||||
|
TEST_SERVICE_UPDATED_PASSPHRASE,
|
||||||
|
TEST_SERVICE_UPDATED_PASSPHRASE,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(id) => {
|
||||||
|
assert_eq!(id, service_user.id, "Ids do not match");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_update_name_of_password() {
|
||||||
|
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));
|
||||||
|
|
||||||
|
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"
|
||||||
|
);
|
||||||
|
|
||||||
|
match flow::update_name_of_user(
|
||||||
|
&app,
|
||||||
|
&user.id,
|
||||||
|
TEST_UPDATED_FIRSTNAME,
|
||||||
|
TEST_UPDATED_LASTNAME,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(user) => {
|
||||||
|
assert_eq!(
|
||||||
|
TEST_UPDATED_FIRSTNAME, user.firstname,
|
||||||
|
"Firstname do not match"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
TEST_UPDATED_LASTNAME, user.lastname,
|
||||||
|
"Lastname do not match"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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