mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 08:25:16 +00:00
Upgrade user tokens system
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
/// API client information
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct APIClient {
|
||||
pub id: u32,
|
||||
pub id: u64,
|
||||
pub name: String,
|
||||
pub token: String,
|
||||
pub domain: Option<String>,
|
||||
pub comment: Option<String>,
|
||||
pub default_expiration_time: u64,
|
||||
}
|
@ -15,11 +15,12 @@ use crate::controllers::routes::RequestResult;
|
||||
use crate::data::comment::Comment;
|
||||
use crate::data::conversation::ConvID;
|
||||
use crate::data::custom_emoji::CustomEmoji;
|
||||
use crate::data::error::{ExecError, ResultBoxError};
|
||||
use crate::data::error::{ExecError, Res, ResultBoxError};
|
||||
use crate::data::group::GroupAccessLevel;
|
||||
use crate::data::group_id::GroupID;
|
||||
use crate::data::post::{Post, PostAccessLevel};
|
||||
use crate::data::user::UserID;
|
||||
use crate::data::user_token::UserAccessToken;
|
||||
use crate::helpers::{account_helper, comments_helper, conversations_helper, custom_emojies_helper, friends_helper, groups_helper, posts_helper, user_helper, virtual_directory_helper};
|
||||
use crate::helpers::virtual_directory_helper::VirtualDirType;
|
||||
use crate::utils::pdf_utils::is_valid_pdf;
|
||||
@ -58,8 +59,13 @@ pub trait BaseRequestHandler {
|
||||
/// Get remote IP address
|
||||
fn remote_ip(&self) -> String;
|
||||
|
||||
/// Current user access token
|
||||
fn user_access_token(&self) -> Option<&UserAccessToken>;
|
||||
|
||||
/// Current user ID
|
||||
fn user_id_opt_ref(&self) -> Option<&UserID>;
|
||||
fn user_id_opt_ref(&self) -> Option<&UserID> {
|
||||
self.user_access_token().map(|u| &u.user_id)
|
||||
}
|
||||
|
||||
|
||||
/// Success message
|
||||
@ -144,6 +150,17 @@ pub trait BaseRequestHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwrap an option, returning an error if none is returned
|
||||
fn some_or_internal_error<E>(&mut self, opt: Option<E>, msg: &str) -> Res<E> {
|
||||
match opt {
|
||||
None => {
|
||||
self.internal_error(ExecError::boxed_new(msg))?;
|
||||
unreachable!()
|
||||
}
|
||||
Some(e) => Ok(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Get a user ID, if available
|
||||
fn user_id_opt(&self) -> Option<UserID> {
|
||||
@ -189,6 +206,18 @@ pub trait BaseRequestHandler {
|
||||
self.post_string_opt(name, 1, true)
|
||||
}
|
||||
|
||||
/// Get a post string with a given name. If the value is not found, attempt to get the value
|
||||
/// with another name
|
||||
///
|
||||
/// This function is useful to upgrade system
|
||||
fn post_string_with_fallback(&mut self, name: &str, fallback: &str) -> Res<String> {
|
||||
if self.has_post_parameter(name) {
|
||||
self.post_string(name)
|
||||
} else {
|
||||
self.post_string(fallback)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a post string, specifying minimum length
|
||||
fn post_string_opt(&mut self, name: &str, min_length: usize, required: bool)
|
||||
-> ResultBoxError<String> {
|
||||
|
@ -11,8 +11,8 @@ use crate::controllers::routes::RequestResult;
|
||||
use crate::data::api_client::APIClient;
|
||||
use crate::data::base_request_handler::{BaseRequestHandler, RequestValue};
|
||||
use crate::data::config::conf;
|
||||
use crate::data::error::ResultBoxError;
|
||||
use crate::data::user::UserID;
|
||||
use crate::data::error::{Res, ResultBoxError};
|
||||
use crate::data::user_token::UserAccessToken;
|
||||
use crate::helpers::{account_helper, api_helper};
|
||||
|
||||
/// Http request handler
|
||||
@ -25,7 +25,7 @@ pub struct HttpRequestHandler {
|
||||
response: Option<web::HttpResponse>,
|
||||
headers: HashMap<String, String>,
|
||||
client: Option<APIClient>,
|
||||
curr_user_id: Option<UserID>,
|
||||
curr_user_token: Option<UserAccessToken>,
|
||||
}
|
||||
|
||||
impl HttpRequestHandler {
|
||||
@ -37,7 +37,7 @@ impl HttpRequestHandler {
|
||||
response: None,
|
||||
headers: HashMap::new(),
|
||||
client: None,
|
||||
curr_user_id: None,
|
||||
curr_user_token: None,
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,11 +78,10 @@ impl HttpRequestHandler {
|
||||
|
||||
/// Check API client tokens
|
||||
pub fn check_client_token(&mut self) -> RequestResult {
|
||||
let api_name = self.post_string("serviceName")?;
|
||||
let api_token = self.post_string("serviceToken")?;
|
||||
let client_name = self.post_string_with_fallback("client", "serviceName")?;
|
||||
|
||||
let client = self.ok_or_bad_request(
|
||||
api_helper::get_client(&api_name, &api_token),
|
||||
api_helper::get_client(&client_name),
|
||||
"Client not recognized!",
|
||||
)?;
|
||||
|
||||
@ -111,13 +110,17 @@ impl HttpRequestHandler {
|
||||
}
|
||||
|
||||
/// Check login token
|
||||
pub fn check_user_token(&mut self) -> RequestResult {
|
||||
pub fn check_user_token(&mut self) -> Res {
|
||||
let token = self.post_string("userToken1")?;
|
||||
|
||||
// Find user
|
||||
match account_helper::get_user_by_login_token(&token, self.api_client()) {
|
||||
Ok(id) => {
|
||||
self.curr_user_id = Some(id);
|
||||
match account_helper::find_user_by_login_token(&token, self.api_client()) {
|
||||
Ok(token) => {
|
||||
if token.need_refresh() {
|
||||
account_helper::refresh_access_token(&token)?;
|
||||
}
|
||||
|
||||
self.curr_user_token = Some(token);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -177,7 +180,7 @@ impl BaseRequestHandler for HttpRequestHandler {
|
||||
ip
|
||||
}
|
||||
|
||||
fn user_id_opt_ref(&self) -> Option<&UserID> {
|
||||
self.curr_user_id.as_ref()
|
||||
fn user_access_token(&self) -> Option<&UserAccessToken> {
|
||||
self.curr_user_token.as_ref()
|
||||
}
|
||||
}
|
@ -50,6 +50,12 @@ impl PartialEq<&UserID> for UserID {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<UserID> for &UserID {
|
||||
fn eq(&self, other: &UserID) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum UserPageStatus {
|
||||
OPEN,
|
||||
|
@ -1,11 +1,29 @@
|
||||
use crate::constants::USER_ACCESS_TOKEN_ACTIVITY_REFRESH;
|
||||
use crate::data::user::UserID;
|
||||
use crate::utils::date_utils::time;
|
||||
|
||||
/// User access token information
|
||||
///
|
||||
/// Author : Pierre Hubert
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UserAccessToken {
|
||||
pub id: u64,
|
||||
pub client_id: u64,
|
||||
pub user_id: UserID,
|
||||
pub client_id: u32,
|
||||
pub token: String
|
||||
pub token: String,
|
||||
pub last_refresh: u64,
|
||||
pub timeout: u64,
|
||||
}
|
||||
|
||||
impl UserAccessToken {
|
||||
/// Check out whether access token should be refreshed
|
||||
pub fn need_refresh(&self) -> bool {
|
||||
self.last_refresh + USER_ACCESS_TOKEN_ACTIVITY_REFRESH.as_secs() < time()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<UserAccessToken> for UserAccessToken {
|
||||
fn eq(&self, other: &UserAccessToken) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ use crate::controllers::user_ws_controller::WsSession;
|
||||
use crate::data::conversation::ConvID;
|
||||
use crate::data::post::PostID;
|
||||
use crate::data::user::UserID;
|
||||
use crate::data::user_token::UserAccessToken;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ActiveCall {
|
||||
@ -14,8 +15,7 @@ pub struct ActiveCall {
|
||||
/// This structure contains information about an active connection
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UserWsConnection {
|
||||
pub user_id: UserID,
|
||||
pub client_id: u32,
|
||||
pub user_token: UserAccessToken,
|
||||
pub remote_ip: String,
|
||||
pub session: actix::Addr<WsSession>,
|
||||
pub incognito: bool,
|
||||
@ -25,6 +25,11 @@ pub struct UserWsConnection {
|
||||
}
|
||||
|
||||
impl UserWsConnection {
|
||||
pub fn user_id(&self) -> &UserID {
|
||||
&self.user_token.user_id
|
||||
}
|
||||
|
||||
|
||||
/// Check out whether a connection is being used to make a call in a specific conversation or not
|
||||
pub fn is_having_call_with_conversation(&self, conv_id: &ConvID) -> bool {
|
||||
if let Some(call_info) = &self.active_call {
|
||||
|
@ -8,7 +8,7 @@ use crate::api_data::http_error::HttpError;
|
||||
use crate::controllers::routes::RequestResult;
|
||||
use crate::data::base_request_handler::{BaseRequestHandler, RequestValue};
|
||||
use crate::data::error::ResultBoxError;
|
||||
use crate::data::user::UserID;
|
||||
use crate::data::user_token::UserAccessToken;
|
||||
use crate::data::user_ws_connection::UserWsConnection;
|
||||
|
||||
pub enum UserWsResponseType {
|
||||
@ -88,11 +88,7 @@ impl BaseRequestHandler for UserWsRequestHandler {
|
||||
self.connection.remote_ip.to_string()
|
||||
}
|
||||
|
||||
fn user_id_opt_ref(&self) -> Option<&UserID> {
|
||||
Some(&self.connection.user_id)
|
||||
}
|
||||
|
||||
fn user_id(&self) -> ResultBoxError<UserID> {
|
||||
Ok(self.connection.user_id.clone())
|
||||
fn user_access_token(&self) -> Option<&UserAccessToken> {
|
||||
Some(&self.connection.user_token)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user