Saving changes
This commit is contained in:
+24
-46
@@ -189,10 +189,10 @@ pub async fn user_login(
|
|||||||
let key = textsender_models::envy::environment::get_secret_key()
|
let key = textsender_models::envy::environment::get_secret_key()
|
||||||
.await
|
.await
|
||||||
.value;
|
.value;
|
||||||
let (token_literal, duration) =
|
let cst =
|
||||||
token_stuff::create_token(&key, &user.id).unwrap();
|
token_stuff::create_token(&key, &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 current_time = time::OffsetDateTime::now_utc();
|
||||||
let _ =
|
let _ =
|
||||||
repo::user::update_last_login(&pool, &user, ¤t_time)
|
repo::user::update_last_login(&pool, &user, ¤t_time)
|
||||||
@@ -204,12 +204,12 @@ pub async fn user_login(
|
|||||||
message: String::from("Successful"),
|
message: String::from("Successful"),
|
||||||
data: vec![textsender_models::token::LoginResult {
|
data: vec![textsender_models::token::LoginResult {
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
access_token: token_literal,
|
access_token: cst.access_token,
|
||||||
token_type: String::from(
|
token_type: String::from(
|
||||||
textsender_models::token::TOKEN_TYPE,
|
textsender_models::token::TOKEN_TYPE,
|
||||||
),
|
),
|
||||||
issued_at: duration,
|
issued_at: cst.issued,
|
||||||
..Default::default()
|
expires_in: cst.expires_in,
|
||||||
}],
|
}],
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
@@ -330,10 +330,10 @@ pub async fn service_user_login(
|
|||||||
let key = textsender_models::envy::environment::get_secret_key()
|
let key = textsender_models::envy::environment::get_secret_key()
|
||||||
.await
|
.await
|
||||||
.value;
|
.value;
|
||||||
let (token_literal, duration) =
|
let cst =
|
||||||
token_stuff::create_token(&key, &service_user.id).unwrap();
|
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 current_time = time::OffsetDateTime::now_utc();
|
||||||
let _ = repo::service::update_last_login(
|
let _ = repo::service::update_last_login(
|
||||||
&pool,
|
&pool,
|
||||||
@@ -350,12 +350,12 @@ pub async fn service_user_login(
|
|||||||
),
|
),
|
||||||
data: vec![textsender_models::token::LoginResult {
|
data: vec![textsender_models::token::LoginResult {
|
||||||
user_id: service_user.id,
|
user_id: service_user.id,
|
||||||
access_token: token_literal,
|
access_token: cst.access_token,
|
||||||
token_type: String::from(
|
token_type: String::from(
|
||||||
textsender_models::token::TOKEN_TYPE,
|
textsender_models::token::TOKEN_TYPE,
|
||||||
),
|
),
|
||||||
issued_at: duration,
|
issued_at: cst.issued,
|
||||||
..Default::default()
|
expires_in: cst.expires_in
|
||||||
}],
|
}],
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
@@ -440,10 +440,10 @@ pub async fn refresh_token(
|
|||||||
if token_stuff::verify_token(&key, &payload.access_token) {
|
if token_stuff::verify_token(&key, &payload.access_token) {
|
||||||
match token_stuff::extract_id_from_token(&key, &payload.access_token) {
|
match token_stuff::extract_id_from_token(&key, &payload.access_token) {
|
||||||
Ok(id) => {
|
Ok(id) => {
|
||||||
let generate_service_token = |id, key| -> (Option<String>, Option<i64>) {
|
let generate_service_token = |id, key| -> Option<textsender_models::token::CreateTokenResult> {
|
||||||
match token_stuff::create_service_refresh_token(key, id) {
|
match token_stuff::create_service_refresh_token(key, id) {
|
||||||
Ok((token, issued)) => (Some(token), Some(issued)),
|
Ok(cst) => Some(cst),
|
||||||
Err(_err) => (None, None),
|
Err(_err) => None,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -454,31 +454,21 @@ pub async fn refresh_token(
|
|||||||
|
|
||||||
match repo::user::get_with_id(&pool, &id).await {
|
match repo::user::get_with_id(&pool, &id).await {
|
||||||
Ok(_user) => {
|
Ok(_user) => {
|
||||||
let (refresh_token, issued) = generate_service_token(&id, &key);
|
match generate_service_token(&id, &key) {
|
||||||
match refresh_token {
|
Some(cst) => {
|
||||||
Some(token) => match issued {
|
|
||||||
Some(issued_at) => {
|
|
||||||
response.message =
|
response.message =
|
||||||
String::from(super::messages::SUCCESSFUL_MESSAGE);
|
String::from(super::messages::SUCCESSFUL_MESSAGE);
|
||||||
let lr = textsender_models::token::LoginResult {
|
let lr = textsender_models::token::LoginResult {
|
||||||
user_id: id,
|
user_id: id,
|
||||||
access_token: token,
|
access_token: cst.access_token,
|
||||||
issued_at,
|
issued_at: cst.issued,
|
||||||
|
expires_in: cst.expires_in,
|
||||||
token_type: String::from(
|
token_type: String::from(
|
||||||
textsender_models::token::TOKEN_TYPE,
|
textsender_models::token::TOKEN_TYPE,
|
||||||
),
|
),
|
||||||
..Default::default()
|
|
||||||
};
|
};
|
||||||
response.data.push(lr);
|
response.data.push(lr);
|
||||||
(axum::http::StatusCode::OK, axum::Json(response))
|
(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 => (
|
None => (
|
||||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
@@ -493,21 +483,19 @@ pub async fn refresh_token(
|
|||||||
println!("Unable to find user, checking service user: {err:?}");
|
println!("Unable to find user, checking service user: {err:?}");
|
||||||
match repo::service::get(&pool, &id).await {
|
match repo::service::get(&pool, &id).await {
|
||||||
Ok(_service_user) => {
|
Ok(_service_user) => {
|
||||||
let (refresh_token, issued) = generate_service_token(&id, &key);
|
match generate_service_token(&id, &key) {
|
||||||
match refresh_token {
|
Some(cst) => {
|
||||||
Some(token) => match issued {
|
|
||||||
Some(issued_at) => {
|
|
||||||
response.message = String::from(
|
response.message = String::from(
|
||||||
super::messages::SUCCESSFUL_MESSAGE,
|
super::messages::SUCCESSFUL_MESSAGE,
|
||||||
);
|
);
|
||||||
let lr = textsender_models::token::LoginResult {
|
let lr = textsender_models::token::LoginResult {
|
||||||
user_id: id,
|
user_id: id,
|
||||||
access_token: token,
|
access_token: cst.access_token,
|
||||||
issued_at,
|
issued_at: cst.issued,
|
||||||
|
expires_in: cst.expires_in,
|
||||||
token_type: String::from(
|
token_type: String::from(
|
||||||
textsender_models::token::TOKEN_TYPE,
|
textsender_models::token::TOKEN_TYPE,
|
||||||
),
|
),
|
||||||
..Default::default()
|
|
||||||
};
|
};
|
||||||
response.data.push(lr);
|
response.data.push(lr);
|
||||||
(axum::http::StatusCode::OK, axum::Json(response))
|
(axum::http::StatusCode::OK, axum::Json(response))
|
||||||
@@ -519,17 +507,7 @@ pub async fn refresh_token(
|
|||||||
data: Vec::new(),
|
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) => (
|
Err(err) => (
|
||||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ pub fn get_expiration(issued: &time::OffsetDateTime) -> Result<time::OffsetDateT
|
|||||||
pub fn create_token(
|
pub fn create_token(
|
||||||
provided_key: &String,
|
provided_key: &String,
|
||||||
id: &uuid::Uuid,
|
id: &uuid::Uuid,
|
||||||
) -> Result<(String, i64), josekit::JoseError> {
|
) -> Result<textsender_models::token::CreateTokenResult, josekit::JoseError> {
|
||||||
let resource = textsender_models::token::TokenResource {
|
let resource = textsender_models::token::TokenResource {
|
||||||
message: String::from(MESSAGE),
|
message: String::from(MESSAGE),
|
||||||
issuer: String::from(ISSUER),
|
issuer: String::from(ISSUER),
|
||||||
@@ -32,7 +32,7 @@ pub fn create_token(
|
|||||||
pub fn create_service_token(
|
pub fn create_service_token(
|
||||||
provided: &String,
|
provided: &String,
|
||||||
id: &uuid::Uuid,
|
id: &uuid::Uuid,
|
||||||
) -> Result<(String, i64), josekit::JoseError> {
|
) -> Result<textsender_models::token::CreateTokenResult, josekit::JoseError> {
|
||||||
let resource = textsender_models::token::TokenResource {
|
let resource = textsender_models::token::TokenResource {
|
||||||
message: String::from(SERVICE_SUBJECT),
|
message: String::from(SERVICE_SUBJECT),
|
||||||
issuer: String::from(ISSUER),
|
issuer: String::from(ISSUER),
|
||||||
@@ -45,7 +45,7 @@ pub fn create_service_token(
|
|||||||
pub fn create_service_refresh_token(
|
pub fn create_service_refresh_token(
|
||||||
key: &String,
|
key: &String,
|
||||||
id: &uuid::Uuid,
|
id: &uuid::Uuid,
|
||||||
) -> Result<(String, i64), josekit::JoseError> {
|
) -> Result<textsender_models::token::CreateTokenResult, josekit::JoseError> {
|
||||||
let resource = textsender_models::token::TokenResource {
|
let resource = textsender_models::token::TokenResource {
|
||||||
message: String::from(SERVICE_SUBJECT),
|
message: String::from(SERVICE_SUBJECT),
|
||||||
issuer: String::from(ISSUER),
|
issuer: String::from(ISSUER),
|
||||||
@@ -125,8 +125,8 @@ mod tests {
|
|||||||
.value;
|
.value;
|
||||||
let id = uuid::Uuid::new_v4();
|
let id = uuid::Uuid::new_v4();
|
||||||
match create_token(&special_key, &id) {
|
match create_token(&special_key, &id) {
|
||||||
Ok((token, _duration)) => {
|
Ok(cst) => {
|
||||||
let result = verify_token(&special_key, &token);
|
let result = verify_token(&special_key, &cst.access_token);
|
||||||
assert!(result, "Token not verified");
|
assert!(result, "Token not verified");
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user