Refresh endpoint #7
Generated
+1
-1
@@ -2213,7 +2213,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "textsender_auth"
|
name = "textsender_auth"
|
||||||
version = "0.1.17"
|
version = "0.1.18"
|
||||||
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.18"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.95"
|
rust-version = "1.95"
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, utoipa::ToSchema)]
|
||||||
|
pub struct RefreshTokenRequest {
|
||||||
|
pub access_token: String,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
@@ -57,6 +62,12 @@ 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>,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Endpoint for a user login
|
/// Endpoint for a user login
|
||||||
@@ -337,3 +348,164 @@ 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(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,4 +13,6 @@ 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";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,6 +106,10 @@ 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),
|
||||||
|
)
|
||||||
.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,
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
+289
-116
@@ -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,24 @@ 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test user firstname
|
/// Test user firstname
|
||||||
@@ -170,140 +189,194 @@ const TEST_SERVICE_USERNAME: &str = "swoon";
|
|||||||
/// Test service passphrase
|
/// Test service passphrase
|
||||||
const TEST_SERVICE_PASSPHRASE: &str = "4n5cf349tfy34w857ty39wq45nfdq23";
|
const TEST_SERVICE_PASSPHRASE: &str = "4n5cf349tfy34w857ty39wq45nfdq23";
|
||||||
|
|
||||||
async fn convert_response<T>(response: axum::response::Response) -> Result<T, std::io::Error>
|
mod util {
|
||||||
where
|
pub async fn convert_response<T>(
|
||||||
T: serde::de::DeserializeOwned,
|
response: axum::response::Response,
|
||||||
{
|
) -> Result<T, std::io::Error>
|
||||||
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
where
|
||||||
Ok(body) => {
|
T: serde::de::DeserializeOwned,
|
||||||
let resp: T = serde_json::from_slice(&body).unwrap();
|
{
|
||||||
Ok(resp)
|
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
||||||
|
Ok(body) => {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
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 {
|
||||||
app: &axum::Router,
|
use super::callers;
|
||||||
) -> Result<textsender_models::user::User, std::io::Error> {
|
use super::requests;
|
||||||
match requests::register_user(&app).await {
|
use super::util;
|
||||||
Ok(response) => {
|
|
||||||
if axum::http::StatusCode::CREATED != response.status() {
|
|
||||||
Err(std::io::Error::other(format!(
|
|
||||||
"Status code is off {:?}",
|
|
||||||
response.status()
|
|
||||||
)))
|
|
||||||
} else {
|
|
||||||
match convert_response::<callers::register::response::Response>(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())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn login_user(
|
pub async fn register_user(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
username: &str,
|
) -> Result<textsender_models::user::User, std::io::Error> {
|
||||||
password: &str,
|
match requests::register_user(&app).await {
|
||||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
Ok(response) => {
|
||||||
match requests::login_user(&app, username, password).await {
|
if axum::http::StatusCode::CREATED != response.status() {
|
||||||
Ok(response) => {
|
Err(std::io::Error::other(format!(
|
||||||
if axum::http::StatusCode::OK != response.status() {
|
"Status code is off {:?}",
|
||||||
Err(std::io::Error::other(format!(
|
response.status()
|
||||||
"Status code is off {:?}",
|
)))
|
||||||
response.status()
|
} else {
|
||||||
)))
|
match util::convert_response::<callers::register::response::Response>(response)
|
||||||
} else {
|
.await
|
||||||
match 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();
|
||||||
Ok(user)
|
Ok(user)
|
||||||
} else {
|
} else {
|
||||||
Err(std::io::Error::other("No data returned"))
|
Err(std::io::Error::other("No data returned"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
}
|
}
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async fn register_service_user(
|
pub async fn login_user(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
) -> Result<textsender_models::user::ServiceUser, std::io::Error> {
|
username: &str,
|
||||||
match requests::register_service_user(&app).await {
|
password: &str,
|
||||||
Ok(response) => {
|
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||||
if axum::http::StatusCode::CREATED != response.status() {
|
match requests::login_user(&app, username, password).await {
|
||||||
Err(std::io::Error::other(format!(
|
Ok(response) => {
|
||||||
"Status code is off {:?}",
|
if axum::http::StatusCode::OK != response.status() {
|
||||||
response.status()
|
Err(std::io::Error::other(format!(
|
||||||
)))
|
"Status code is off {:?}",
|
||||||
} else {
|
response.status()
|
||||||
match convert_response::<callers::register::response::RegisterServiceUserResponse>(
|
)))
|
||||||
response,
|
} else {
|
||||||
)
|
match util::convert_response::<callers::login::response::LoginResponse>(
|
||||||
.await
|
response,
|
||||||
{
|
)
|
||||||
Ok(response) => {
|
.await
|
||||||
if response.data.len() > 0 {
|
{
|
||||||
let service_user = response.data[0].clone();
|
Ok(response) => {
|
||||||
Ok(service_user)
|
if response.data.len() > 0 {
|
||||||
} else {
|
let user = response.data[0].clone();
|
||||||
Err(std::io::Error::other("No data returned"))
|
Ok(user)
|
||||||
|
} else {
|
||||||
|
Err(std::io::Error::other("No data returned"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
}
|
}
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async fn login_service_user(
|
pub async fn register_service_user(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
username: &str,
|
) -> Result<textsender_models::user::ServiceUser, std::io::Error> {
|
||||||
passphrase: &str,
|
match requests::register_service_user(&app).await {
|
||||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
Ok(response) => {
|
||||||
match requests::login_service_user(&app, username, passphrase).await {
|
if axum::http::StatusCode::CREATED != response.status() {
|
||||||
Ok(response) => {
|
Err(std::io::Error::other(format!(
|
||||||
if axum::http::StatusCode::OK != response.status() {
|
"Status code is off {:?}",
|
||||||
Err(std::io::Error::other(format!(
|
response.status()
|
||||||
"Status code is off {:?}",
|
)))
|
||||||
response.status()
|
} else {
|
||||||
)))
|
match util::convert_response::<
|
||||||
} else {
|
callers::register::response::RegisterServiceUserResponse,
|
||||||
match convert_response::<callers::login::response::ServiceUserLoginResponse>(
|
>(response)
|
||||||
response,
|
.await
|
||||||
)
|
{
|
||||||
.await
|
Ok(response) => {
|
||||||
{
|
if response.data.len() > 0 {
|
||||||
Ok(response) => {
|
let service_user = response.data[0].clone();
|
||||||
if response.data.len() > 0 {
|
Ok(service_user)
|
||||||
let login_result = response.data[0].clone();
|
} else {
|
||||||
Ok(login_result)
|
Err(std::io::Error::other("No data returned"))
|
||||||
} else {
|
}
|
||||||
Err(std::io::Error::other("No data returned"))
|
|
||||||
}
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn login_service_user(
|
||||||
|
app: &axum::Router,
|
||||||
|
username: &str,
|
||||||
|
passphrase: &str,
|
||||||
|
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||||
|
match requests::login_service_user(&app, username, passphrase).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::ServiceUserLoginResponse>(
|
||||||
|
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 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())),
|
||||||
}
|
}
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,7 +400,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 +440,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 +488,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 +539,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 +573,101 @@ 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:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user