Skip to content

Commit be123e6

Browse files
committed
add login mask in TUI
1 parent 35ef9e0 commit be123e6

8 files changed

Lines changed: 322 additions & 47 deletions

File tree

coman/src/app/ids.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ pub enum Id {
66
Menu,
77
InfoPopup,
88
ErrorPopup,
9+
LoginPopup,
910
}

coman/src/app/messages.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ pub enum ErrorPopupMsg {
1616
Closed,
1717
}
1818

19+
#[derive(Debug, PartialEq)]
20+
pub enum LoginPopupMsg {
21+
Opened,
22+
Closed,
23+
LoginDone(String, String),
24+
}
25+
1926
#[derive(Debug, PartialEq)]
2027
pub enum CscsMsg {
21-
Login,
28+
Login(String, String),
2229
}
2330

2431
#[derive(Debug, PartialEq)]
@@ -27,6 +34,7 @@ pub enum Msg {
2734
Menu(MenuMsg),
2835
InfoPopup(InfoPopupMsg),
2936
ErrorPopup(ErrorPopupMsg),
37+
LoginPopup(LoginPopupMsg),
3038
Error(String),
3139
Info(String),
3240
Cscs(CscsMsg),

coman/src/app/model.rs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use openidconnect::core::CoreDeviceAuthorizationResponse;
1+
use eyre::{Context, Report};
22
use tuirealm::{
33
Application, Update,
44
ratatui::layout::{Constraint, Direction, Layout},
@@ -9,11 +9,14 @@ use tuirealm::{
99
use crate::{
1010
app::{
1111
ids::Id,
12-
messages::{CscsMsg, ErrorPopupMsg, InfoPopupMsg, MenuMsg, Msg},
12+
messages::{CscsMsg, ErrorPopupMsg, InfoPopupMsg, LoginPopupMsg, MenuMsg, Msg},
1313
user_events::UserEvent,
1414
},
15-
components::{error_popup::ErrorPopup, info_popup::InfoPopup, workload_menu::WorkloadMenu},
16-
cscs::oauth2::start_cscs_device_login,
15+
components::{
16+
error_popup::ErrorPopup, info_popup::InfoPopup, login_popup::LoginPopup,
17+
workload_menu::WorkloadMenu,
18+
},
19+
cscs::cli::cscs_login,
1720
trace_dbg,
1821
util::ui::draw_area_in_absolute,
1922
};
@@ -32,8 +35,6 @@ where
3235
/// Used to draw to terminal
3336
pub terminal: TerminalBridge<T>,
3437

35-
/// Tokio channel sender for starting device code flow in Port
36-
pub cscs_device_flow_tx: mpsc::Sender<(CoreDeviceAuthorizationResponse, String)>,
3738
///Used to allow sending errors from tokio::spawn async jobs
3839
pub error_tx: mpsc::Sender<String>,
3940
}
@@ -45,15 +46,13 @@ where
4546
pub fn new(
4647
app: Application<Id, Msg, UserEvent>,
4748
bridge: TerminalBridge<T>,
48-
cscs_device_flow_tx: mpsc::Sender<(CoreDeviceAuthorizationResponse, String)>,
4949
error_tx: mpsc::Sender<String>,
5050
) -> Self {
5151
Self {
5252
app,
5353
quit: false,
5454
redraw: true,
5555
terminal: bridge,
56-
cscs_device_flow_tx,
5756
error_tx,
5857
}
5958
}
@@ -88,11 +87,36 @@ where
8887
let popup = draw_area_in_absolute(f.area(), 10);
8988
f.render_widget(Clear, popup);
9089
self.app.view(&Id::InfoPopup, f, popup);
90+
} else if self.app.mounted(&Id::LoginPopup) {
91+
let popup = draw_area_in_absolute(f.area(), 10);
92+
f.render_widget(Clear, popup);
93+
self.app.view(&Id::LoginPopup, f, popup);
9194
}
9295
})
9396
.is_ok()
9497
);
9598
}
99+
fn handle_login_popup_msg(&mut self, msg: LoginPopupMsg) -> Option<Msg> {
100+
match msg {
101+
LoginPopupMsg::Opened => {
102+
assert!(
103+
self.app
104+
.mount(Id::LoginPopup, Box::new(LoginPopup::new()), vec![])
105+
.is_ok()
106+
);
107+
assert!(self.app.active(&Id::LoginPopup).is_ok());
108+
None
109+
}
110+
LoginPopupMsg::Closed => {
111+
assert!(self.app.umount(&Id::LoginPopup).is_ok());
112+
None
113+
}
114+
LoginPopupMsg::LoginDone(client_id, client_secret) => {
115+
assert!(self.app.umount(&Id::LoginPopup).is_ok());
116+
Some(Msg::Cscs(CscsMsg::Login(client_id, client_secret)))
117+
}
118+
}
119+
}
96120
fn handle_error_popup_msg(&mut self, msg: ErrorPopupMsg) -> Option<Msg> {
97121
match msg {
98122
ErrorPopupMsg::Opened(error_msg) => {
@@ -148,7 +172,7 @@ where
148172
}
149173
MenuMsg::CscsLogin => {
150174
assert!(self.app.umount(&Id::Menu).is_ok());
151-
Some(Msg::Cscs(CscsMsg::Login))
175+
Some(Msg::LoginPopup(LoginPopupMsg::Opened))
152176
}
153177
}
154178
}
@@ -177,19 +201,25 @@ where
177201
Msg::Menu(menu_msg) => self.handle_menu_msg(menu_msg),
178202
Msg::ErrorPopup(popup_msg) => self.handle_error_popup_msg(popup_msg),
179203
Msg::InfoPopup(popup_msg) => self.handle_info_popup_msg(popup_msg),
180-
Msg::Cscs(CscsMsg::Login) => {
181-
let device_flow_tx = self.cscs_device_flow_tx.clone();
204+
Msg::Cscs(CscsMsg::Login(client_id, client_secret)) => {
182205
let error_tx = self.error_tx.clone();
183206
tokio::spawn(async move {
184-
match start_cscs_device_login().await {
185-
Ok(result) => device_flow_tx.send(result).await.unwrap(),
186-
Err(e) => error_tx.send(format!("{:?}", e)).await.unwrap(),
187-
}
207+
match cscs_login(client_id, client_secret).await {
208+
Ok(_) => {}
209+
Err(e) => error_tx
210+
.send(format!(
211+
"{:?}",
212+
Err::<(), Report>(e)
213+
.wrap_err("Login failed with supplied credentials")
214+
))
215+
.await
216+
.unwrap(),
217+
};
188218
});
189219
None
190220
}
191-
192221
Msg::None => None,
222+
Msg::LoginPopup(msg) => self.handle_login_popup_msg(msg),
193223
}
194224
} else {
195225
None

0 commit comments

Comments
 (0)