feat_account: mid commit

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2022-10-20 23:03:54 +08:00
parent 3454454bd5
commit 87e53501e3
8 changed files with 536 additions and 50 deletions

View File

@@ -13,6 +13,8 @@ use hbb_common::{
fs, log,
};
// use crate::hbbs_http::account::AuthResult;
use crate::flutter::{self, SESSIONS};
#[cfg(target_os = "android")]
use crate::start_server;
@@ -1082,6 +1084,14 @@ pub fn install_install_path() -> SyncReturn<String> {
SyncReturn(install_path())
}
pub fn main_account_auth(op: String) {
account_auth(op);
}
pub fn main_account_auth_result() -> String {
account_auth_result()
}
#[cfg(target_os = "android")]
pub mod server_side {
use jni::{

View File

@@ -1,6 +1,6 @@
use super::HbbHttpResponse;
use hbb_common::{config::Config, log, sleep, tokio, tokio::sync::RwLock, ResultType};
use serde_derive::Deserialize;
use serde_derive::{Deserialize, Serialize};
use std::{
collections::HashMap,
sync::Arc,
@@ -16,6 +16,9 @@ lazy_static::lazy_static! {
const QUERY_INTERVAL_SECS: f32 = 1.0;
const QUERY_TIMEOUT_SECS: u64 = 60;
const REQUESTING_ACCOUNT_AUTH: &str = "Requesting account auth";
const WAITING_ACCOUNT_AUTH: &str = "Waiting account auth";
const LOGIN_ACCOUNT_AUTH: &str = "Login account auth";
#[derive(Deserialize, Clone)]
pub struct OidcAuthUrl {
@@ -23,7 +26,7 @@ pub struct OidcAuthUrl {
url: Url,
}
#[derive(Debug, Deserialize, Default, Clone)]
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct UserPayload {
pub id: String,
pub name: String,
@@ -34,34 +37,16 @@ pub struct UserPayload {
pub is_admin: Option<bool>,
}
#[derive(Debug, Deserialize, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthBody {
access_token: String,
token_type: String,
user: UserPayload,
}
#[derive(Copy, Clone)]
pub enum OidcState {
// initial request
OidcRequest = 1,
// initial request failed
OidcRequestFailed = 2,
// request succeeded, loop querying
OidcQuerying = 11,
// loop querying failed
OidcQueryFailed = 12,
// query sucess before
OidcNotExists = 13,
// query timeout
OidcQueryTimeout = 14,
// already login
OidcLogin = 21,
pub access_token: String,
pub token_type: String,
pub user: UserPayload,
}
pub struct OidcSession {
client: reqwest::Client,
state: OidcState,
state_msg: &'static str,
failed_msg: String,
code_url: Option<OidcAuthUrl>,
auth_body: Option<AuthBody>,
@@ -70,11 +55,19 @@ pub struct OidcSession {
query_timeout: Duration,
}
#[derive(Serialize)]
pub struct AuthResult {
pub state_msg: String,
pub failed_msg: String,
pub url: Option<String>,
pub auth_body: Option<AuthBody>,
}
impl OidcSession {
fn new() -> Self {
Self {
client: reqwest::Client::new(),
state: OidcState::OidcRequest,
state_msg: REQUESTING_ACCOUNT_AUTH,
failed_msg: "".to_owned(),
code_url: None,
auth_body: None,
@@ -112,7 +105,7 @@ impl OidcSession {
}
fn reset(&mut self) {
self.state = OidcState::OidcRequest;
self.state_msg = REQUESTING_ACCOUNT_AUTH;
self.failed_msg = "".to_owned();
self.keep_querying = true;
self.running = false;
@@ -136,21 +129,21 @@ impl OidcSession {
OIDC_SESSION
.write()
.await
.set_state(OidcState::OidcRequestFailed, err);
.set_state(REQUESTING_ACCOUNT_AUTH, err);
return;
}
Ok(_) => {
OIDC_SESSION.write().await.set_state(
OidcState::OidcRequestFailed,
"Invalid auth response".to_owned(),
);
OIDC_SESSION
.write()
.await
.set_state(REQUESTING_ACCOUNT_AUTH, "Invalid auth response".to_owned());
return;
}
Err(err) => {
OIDC_SESSION
.write()
.await
.set_state(OidcState::OidcRequestFailed, err.to_string());
.set_state(REQUESTING_ACCOUNT_AUTH, err.to_string());
return;
}
};
@@ -158,7 +151,7 @@ impl OidcSession {
OIDC_SESSION
.write()
.await
.set_state(OidcState::OidcQuerying, "".to_owned());
.set_state(WAITING_ACCOUNT_AUTH, "".to_owned());
OIDC_SESSION.write().await.code_url = Some(code_url.clone());
let begin = Instant::now();
@@ -169,7 +162,7 @@ impl OidcSession {
OIDC_SESSION
.write()
.await
.set_state(OidcState::OidcLogin, "".to_owned());
.set_state(LOGIN_ACCOUNT_AUTH, "".to_owned());
OIDC_SESSION.write().await.auth_body = Some(auth_body);
return;
// to-do, set access-token
@@ -181,7 +174,7 @@ impl OidcSession {
OIDC_SESSION
.write()
.await
.set_state(OidcState::OidcQueryFailed, err);
.set_state(WAITING_ACCOUNT_AUTH, err);
return;
}
}
@@ -200,14 +193,14 @@ impl OidcSession {
OIDC_SESSION
.write()
.await
.set_state(OidcState::OidcQueryTimeout, "timeout".to_owned());
.set_state(WAITING_ACCOUNT_AUTH, "timeout".to_owned());
}
// no need to handle "keep_querying == false"
}
fn set_state(&mut self, state: OidcState, failed_msg: String) {
self.state = state;
fn set_state(&mut self, state_msg: &'static str, failed_msg: String) {
self.state_msg = state_msg;
self.failed_msg = failed_msg;
}
@@ -228,15 +221,16 @@ impl OidcSession {
});
}
fn get_result_(&self) -> (u8, String, Option<AuthBody>) {
(
self.state as u8,
self.failed_msg.clone(),
self.auth_body.clone(),
)
fn get_result_(&self) -> AuthResult {
AuthResult {
state_msg: self.state_msg.to_string(),
failed_msg: self.failed_msg.clone(),
url: self.code_url.as_ref().map(|x| x.url.to_string()),
auth_body: self.auth_body.clone(),
}
}
pub async fn get_result() -> (u8, String, Option<AuthBody>) {
pub async fn get_result() -> AuthResult {
OIDC_SESSION.read().await.get_result_()
}
}

View File

@@ -20,8 +20,7 @@ use hbb_common::{
tokio::{self, sync::mpsc, time},
};
use crate::ipc;
use crate::{common::SOFTWARE_UPDATE_URL, platform};
use crate::{common::SOFTWARE_UPDATE_URL, hbbs_http::account, ipc, platform};
type Message = RendezvousMessage;
@@ -843,6 +842,16 @@ pub(crate) fn check_connect_status(reconnect: bool) -> mpsc::UnboundedSender<ipc
tx
}
#[tokio::main(flavor = "current_thread")]
pub async fn account_auth(op: String) {
account::OidcSession::account_auth(op, get_id(), get_uuid()).await;
}
#[tokio::main(flavor = "current_thread")]
pub async fn account_auth_result() -> String {
serde_json::to_string(&account::OidcSession::get_result().await).unwrap_or_default()
}
// notice: avoiding create ipc connecton repeatly,
// because windows named pipe has serious memory leak issue.
#[tokio::main(flavor = "current_thread")]