use uri for request

This commit is contained in:
yggverse 2025-01-23 12:50:53 +02:00
parent 24adba9065
commit 9a7984f345
6 changed files with 37 additions and 49 deletions

View file

@ -12,9 +12,9 @@ use std::rc::Rc;
pub fn default( pub fn default(
action: (&Rc<BrowserAction>, &Rc<WindowAction>), action: (&Rc<BrowserAction>, &Rc<WindowAction>),
profile: &Rc<Profile>, profile: &Rc<Profile>,
auth_uri: &Uri, request: &Uri,
) -> Default { ) -> Default {
Default::build(action, profile, auth_uri) Default::build(action, profile, request)
} }
/// Create new identity widget for unknown request /// Create new identity widget for unknown request

View file

@ -2,10 +2,7 @@ mod widget;
use widget::{form::list::item::value::Value, Widget}; use widget::{form::list::item::value::Value, Widget};
use super::{BrowserAction, Profile, WindowAction}; use super::{BrowserAction, Profile, WindowAction};
use gtk::{ use gtk::{glib::Uri, prelude::IsA};
glib::{Regex, RegexCompileFlags, RegexMatchFlags, Uri},
prelude::IsA,
};
use std::rc::Rc; use std::rc::Rc;
pub struct Default { pub struct Default {
@ -20,31 +17,13 @@ impl Default {
pub fn build( pub fn build(
(browser_action, window_action): (&Rc<BrowserAction>, &Rc<WindowAction>), (browser_action, window_action): (&Rc<BrowserAction>, &Rc<WindowAction>),
profile: &Rc<Profile>, profile: &Rc<Profile>,
auth_uri: &Uri, request: &Uri,
) -> Self { ) -> Self {
// Init scope
let auth_url = auth_uri.to_string();
let scope = match Regex::split_simple(
r"^\w+://(.*)",
&auth_url,
RegexCompileFlags::DEFAULT,
RegexMatchFlags::DEFAULT,
)
.get(1)
{
Some(postfix) => postfix.to_string(),
None => auth_url, // @TODO warn?
}
.trim()
.trim_end_matches("/")
.to_lowercase();
// Init widget // Init widget
let widget = Rc::new(Widget::build( let widget = Rc::new(Widget::build(
(browser_action, window_action), (browser_action, window_action),
profile, profile,
&scope, request,
)); ));
// Init events // Init events
@ -55,6 +34,7 @@ impl Default {
widget.on_apply({ widget.on_apply({
let profile = profile.clone(); let profile = profile.clone();
let request = request.clone();
let widget = widget.clone(); let widget = widget.clone();
let window_action = window_action.clone(); let window_action = window_action.clone();
move |response| { move |response| {
@ -68,13 +48,13 @@ impl Default {
.make(None, &widget.form.name.value().unwrap()) .make(None, &widget.form.name.value().unwrap())
{ {
Ok(profile_identity_id) => profile_identity_id, Ok(profile_identity_id) => profile_identity_id,
Err(e) => todo!("{}", e.to_string()), Err(e) => todo!("{e}"),
}, },
), ),
Value::ImportPem => Some( Value::ImportPem => Some(
match profile.identity.add(&widget.form.file.pem.take().unwrap()) { match profile.identity.add(&widget.form.file.pem.take().unwrap()) {
Ok(profile_identity_id) => profile_identity_id, Ok(profile_identity_id) => profile_identity_id,
Err(e) => todo!("{}", e.to_string()), Err(e) => todo!("{e}"),
}, },
), ),
}; };
@ -83,14 +63,18 @@ impl Default {
match option { match option {
// Activate identity for `scope` // Activate identity for `scope`
Some(profile_identity_id) => { Some(profile_identity_id) => {
if let Err(e) = profile.identity.auth.apply(profile_identity_id, &scope) { if let Err(e) = profile
todo!("{}", e.to_string()) .identity
.auth
.apply(profile_identity_id, &request.to_string())
{
todo!("{e}")
}; };
} }
// Remove all identity auths for `scope` // Remove all identity auths for `scope`
None => { None => {
if let Err(e) = profile.identity.auth.remove_scope(&scope) { if let Err(e) = profile.identity.auth.remove_scope(&request.to_string()) {
todo!("{}", e.to_string()) todo!("{e}")
}; };
} }
} }

View file

@ -12,7 +12,7 @@ use adw::{
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual}, prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
AlertDialog, ResponseAppearance, AlertDialog, ResponseAppearance,
}; };
use gtk::prelude::IsA; use gtk::{glib::Uri, prelude::IsA};
use std::rc::Rc; use std::rc::Rc;
// Defaults // Defaults
@ -39,7 +39,7 @@ impl Widget {
pub fn build( pub fn build(
(browser_action, window_action): (&Rc<BrowserAction>, &Rc<WindowAction>), (browser_action, window_action): (&Rc<BrowserAction>, &Rc<WindowAction>),
profile: &Rc<Profile>, profile: &Rc<Profile>,
scope: &str, request: &Uri,
) -> Self { ) -> Self {
// Init actions // Init actions
let widget_action = Rc::new(WidgetAction::new()); let widget_action = Rc::new(WidgetAction::new());
@ -48,7 +48,7 @@ impl Widget {
let form = Rc::new(Form::build( let form = Rc::new(Form::build(
(browser_action, window_action, &widget_action), (browser_action, window_action, &widget_action),
profile, profile,
scope, request,
)); ));
// Init main widget // Init main widget

View file

@ -17,7 +17,7 @@ use crate::{
app::browser::{action::Action as BrowserAction, window::action::Action as WindowAction}, app::browser::{action::Action as BrowserAction, window::action::Action as WindowAction},
Profile, Profile,
}; };
use gtk::{prelude::BoxExt, Box, Orientation}; use gtk::{glib::Uri, prelude::BoxExt, Box, Orientation};
use std::rc::Rc; use std::rc::Rc;
pub struct Form { pub struct Form {
@ -29,7 +29,7 @@ pub struct Form {
pub name: Rc<Name>, pub name: Rc<Name>,
pub save: Rc<Save>, pub save: Rc<Save>,
pub g_box: Box, pub g_box: Box,
scope: String, request: Uri,
profile: Rc<Profile>, profile: Rc<Profile>,
} }
@ -44,10 +44,10 @@ impl Form {
&Rc<WidgetAction>, &Rc<WidgetAction>,
), ),
profile: &Rc<Profile>, profile: &Rc<Profile>,
scope: &str, request: &Uri,
) -> Self { ) -> Self {
// Init components // Init components
let list = Rc::new(List::build(widget_action, profile, scope)); let list = Rc::new(List::build(widget_action, profile, request));
let file = Rc::new(File::build(widget_action)); let file = Rc::new(File::build(widget_action));
let name = Rc::new(Name::build(widget_action)); let name = Rc::new(Name::build(widget_action));
let save = Rc::new(Save::build(profile, &list)); let save = Rc::new(Save::build(profile, &list));
@ -56,7 +56,7 @@ impl Form {
(browser_action, widget_action), (browser_action, widget_action),
profile, profile,
&list, &list,
scope, request,
)); ));
// Init main container // Init main container
@ -79,7 +79,7 @@ impl Form {
name, name,
save, save,
g_box, g_box,
scope: scope.to_string(), request: request.clone(),
profile: profile.clone(), profile: profile.clone(),
} }
} }
@ -112,7 +112,7 @@ impl Form {
.identity .identity
.auth .auth
.memory .memory
.match_scope(&self.scope) .match_scope(&self.request.to_string())
.is_some_and(|auth| auth.profile_identity_id == profile_identity_id), .is_some_and(|auth| auth.profile_identity_id == profile_identity_id),
); );
self.save.update(true); self.save.update(true);

View file

@ -8,6 +8,7 @@ use adw::{
AlertDialog, ResponseAppearance, AlertDialog, ResponseAppearance,
}; };
use gtk::{ use gtk::{
glib::Uri,
prelude::{ButtonExt, WidgetExt}, prelude::{ButtonExt, WidgetExt},
Button, Button,
}; };
@ -36,7 +37,7 @@ impl Exit {
(browser_action, widget_action): (&Rc<BrowserAction>, &Rc<WidgetAction>), (browser_action, widget_action): (&Rc<BrowserAction>, &Rc<WidgetAction>),
profile: &Rc<Profile>, profile: &Rc<Profile>,
list: &Rc<List>, list: &Rc<List>,
scope: &str, request: &Uri,
) -> Self { ) -> Self {
// Init main widget // Init main widget
let button = Button::builder() let button = Button::builder()
@ -48,11 +49,11 @@ impl Exit {
// Init events // Init events
button.connect_clicked({ button.connect_clicked({
let scope = scope.to_string();
let browser_action = browser_action.clone(); let browser_action = browser_action.clone();
let button = button.clone(); let button = button.clone();
let list = list.clone(); let list = list.clone();
let profile = profile.clone(); let profile = profile.clone();
let request = request.clone();
let widget_action = widget_action.clone(); let widget_action = widget_action.clone();
move |_| { move |_| {
// Get selected identity from holder // Get selected identity from holder
@ -83,16 +84,17 @@ impl Exit {
// Connect confirmation event // Connect confirmation event
alert_dialog.connect_response(Some(RESPONSE_CONFIRM.0), { alert_dialog.connect_response(Some(RESPONSE_CONFIRM.0), {
let scope = scope.clone(); let browser_action = browser_action.clone();
let button = button.clone(); let button = button.clone();
let list = list.clone(); let list = list.clone();
let profile = profile.clone(); let profile = profile.clone();
let browser_action = browser_action.clone(); let request = request.clone();
let widget_action = widget_action.clone(); let widget_action = widget_action.clone();
move |_, _| { move |_, _| {
match profile.identity.auth.remove_ref(profile_identity_id) { match profile.identity.auth.remove_ref(profile_identity_id) {
Ok(_) => { Ok(_) => {
match list.selected().update(&profile, &scope.to_string()) { match list.selected().update(&profile, &request.to_string())
{
Ok(_) => { Ok(_) => {
button.set_css_classes(&["success"]); button.set_css_classes(&["success"]);
button button

View file

@ -11,6 +11,7 @@ use gtk::{
prelude::{Cast, CastNone}, prelude::{Cast, CastNone},
ListStore, ListStore,
}, },
glib::Uri,
prelude::{BoxExt, ListItemExt, ObjectExt, WidgetExt}, prelude::{BoxExt, ListItemExt, ObjectExt, WidgetExt},
Align, Box, DropDown, Image, Label, ListItem, Orientation, SignalListItemFactory, Align, Box, DropDown, Image, Label, ListItem, Orientation, SignalListItemFactory,
}; };
@ -24,7 +25,7 @@ impl List {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn build(widget_action: &Rc<WidgetAction>, profile: &Rc<Profile>, scope: &str) -> Self { pub fn build(widget_action: &Rc<WidgetAction>, profile: &Rc<Profile>, request: &Uri) -> Self {
// Init dropdown items // Init dropdown items
let guest_session = Item::new_guest_session(); let guest_session = Item::new_guest_session();
let generate_pem = Item::new_generate_pem(); let generate_pem = Item::new_generate_pem();
@ -41,7 +42,8 @@ impl List {
Ok(identities) => { Ok(identities) => {
let mut is_guest_session = true; let mut is_guest_session = true;
for identity in identities { for identity in identities {
match Item::new_profile_identity_id(profile, identity.id, scope) { match Item::new_profile_identity_id(profile, identity.id, &request.to_string())
{
Ok(item) => { Ok(item) => {
if item.is_active() { if item.is_active() {
is_guest_session = false; is_guest_session = false;