|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// SPDX-FileCopyrightText: 2025 Harald Sitter <sitter@kde.org> |
| 3 | + |
| 4 | +use ashpd::{ActivationToken, WindowIdentifierType}; |
| 5 | +use oo7::dbus::ServiceError; |
| 6 | + |
| 7 | +use serde::Serialize; |
| 8 | +use zbus::{ |
| 9 | + object_server::SignalEmitter, |
| 10 | + zvariant::{self, ObjectPath, Optional, OwnedFd, OwnedObjectPath, Type}, |
| 11 | +}; |
| 12 | + |
| 13 | +use crate::{ |
| 14 | + prompt::{Prompt, PromptRole}, |
| 15 | + service::Service, |
| 16 | +}; |
| 17 | +use tokio::io::AsyncReadExt; |
| 18 | + |
| 19 | +use std::os::fd::AsFd; |
| 20 | + |
| 21 | +#[repr(i32)] |
| 22 | +#[derive(Debug, Type, Serialize)] |
| 23 | +pub enum CallbackAction { |
| 24 | + Dismiss = 0, |
| 25 | + Keep = 1, |
| 26 | +} |
| 27 | + |
| 28 | +#[zbus::proxy( |
| 29 | + default_service = "org.kde.secretprompter", |
| 30 | + interface = "org.kde.secretprompter", |
| 31 | + default_path = "/SecretPrompter", |
| 32 | + gen_blocking = false |
| 33 | +)] |
| 34 | +pub trait PlasmaPrompter { |
| 35 | + fn unlock_collection_prompt( |
| 36 | + &self, |
| 37 | + request: &ObjectPath<'_>, |
| 38 | + window_id: Optional<WindowIdentifierType>, |
| 39 | + activation_token: ActivationToken, |
| 40 | + collection_name: &str, |
| 41 | + ) -> Result<(), ServiceError>; |
| 42 | + fn create_collection_prompt( |
| 43 | + &self, |
| 44 | + request: &ObjectPath<'_>, |
| 45 | + window_id: Optional<WindowIdentifierType>, |
| 46 | + activation_token: ActivationToken, |
| 47 | + collection_name: &str, |
| 48 | + ) -> Result<(), ServiceError>; |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(Debug, Clone)] |
| 52 | +pub struct PlasmaPrompterCallback { |
| 53 | + service: Service, |
| 54 | + prompt_path: OwnedObjectPath, |
| 55 | + path: OwnedObjectPath, |
| 56 | +} |
| 57 | + |
| 58 | +#[zbus::interface(name = "org.kde.secretprompter.request")] |
| 59 | +impl PlasmaPrompterCallback { |
| 60 | + pub async fn accepted(&self, result_fd: OwnedFd) -> Result<CallbackAction, ServiceError> { |
| 61 | + let prompt_path = &self.prompt_path; |
| 62 | + let Some(prompt) = self.service.prompt(prompt_path).await else { |
| 63 | + return Err(ServiceError::NoSuchObject(format!( |
| 64 | + "Prompt '{prompt_path}' does not exist." |
| 65 | + ))); |
| 66 | + }; |
| 67 | + |
| 68 | + tracing::debug!("User accepted the prompt."); |
| 69 | + |
| 70 | + let secret = { |
| 71 | + let borrowed_fd = result_fd.as_fd(); |
| 72 | + let std_stream = std::os::unix::net::UnixStream::from( |
| 73 | + borrowed_fd |
| 74 | + .try_clone_to_owned() |
| 75 | + .expect("Failed to clone fd"), |
| 76 | + ); |
| 77 | + let mut stream = tokio::net::UnixStream::from_std(std_stream).unwrap(); |
| 78 | + let mut buffer = String::new(); |
| 79 | + stream |
| 80 | + .read_to_string(&mut buffer) |
| 81 | + .await |
| 82 | + .expect("error reading secret"); |
| 83 | + buffer |
| 84 | + }; |
| 85 | + |
| 86 | + self.on_reply(&prompt, &secret).await |
| 87 | + } |
| 88 | + |
| 89 | + pub async fn rejected(&self) -> Result<CallbackAction, ServiceError> { |
| 90 | + tracing::debug!("User rejected the prompt."); |
| 91 | + self.prompter_dismissed(self.prompt_path.clone()).await?; |
| 92 | + Ok(CallbackAction::Dismiss) // simply dismiss without further action |
| 93 | + } |
| 94 | + |
| 95 | + pub async fn dismissed(&self) -> Result<(), ServiceError> { |
| 96 | + // This is only does check if the prompt is tracked on Service |
| 97 | + let path = &self.prompt_path; |
| 98 | + if let Some(_prompt) = self.service.prompt(path).await { |
| 99 | + self.service |
| 100 | + .object_server() |
| 101 | + .remove::<Prompt, _>(path) |
| 102 | + .await?; |
| 103 | + self.service.remove_prompt(path).await; |
| 104 | + } |
| 105 | + self.service |
| 106 | + .object_server() |
| 107 | + .remove::<Self, _>(&self.path) |
| 108 | + .await?; |
| 109 | + |
| 110 | + Ok(()) |
| 111 | + } |
| 112 | + |
| 113 | + #[zbus(signal)] |
| 114 | + pub async fn retry(signal_emitter: &SignalEmitter<'_>, reason: &str) -> zbus::Result<()>; |
| 115 | + |
| 116 | + #[zbus(signal)] |
| 117 | + pub async fn dismiss(signal_emitter: &SignalEmitter<'_>) -> zbus::Result<()>; |
| 118 | +} |
| 119 | + |
| 120 | +impl PlasmaPrompterCallback { |
| 121 | + pub async fn new( |
| 122 | + service: Service, |
| 123 | + prompt_path: OwnedObjectPath, |
| 124 | + ) -> Result<Self, oo7::crypto::Error> { |
| 125 | + let index = service.prompt_index().await; |
| 126 | + Ok(Self { |
| 127 | + path: OwnedObjectPath::try_from(format!("/org/plasma/keyring/Prompt/p{index}")) |
| 128 | + .unwrap(), |
| 129 | + service, |
| 130 | + prompt_path, |
| 131 | + }) |
| 132 | + } |
| 133 | + |
| 134 | + pub fn path(&self) -> &ObjectPath<'_> { |
| 135 | + &self.path |
| 136 | + } |
| 137 | + |
| 138 | + async fn on_reply(&self, prompt: &Prompt, reply: &str) -> Result<CallbackAction, ServiceError> { |
| 139 | + let secret = oo7::Secret::from(reply); |
| 140 | + |
| 141 | + // Handle each role differently based on what validation/preparation is needed |
| 142 | + match prompt.role() { |
| 143 | + PromptRole::Unlock => { |
| 144 | + if prompt.on_unlock_collection(secret).await? { |
| 145 | + Ok(CallbackAction::Dismiss) |
| 146 | + } else { |
| 147 | + let emitter = SignalEmitter::from_parts( |
| 148 | + self.service.connection().clone(), |
| 149 | + self.path().clone(), |
| 150 | + ); |
| 151 | + PlasmaPrompterCallback::retry(&emitter, "The unlock password was incorrect") |
| 152 | + .await?; |
| 153 | + |
| 154 | + Ok(CallbackAction::Keep) // we retry |
| 155 | + } |
| 156 | + } |
| 157 | + PromptRole::CreateCollection => { |
| 158 | + prompt.on_create_collection(secret).await?; |
| 159 | + Ok(CallbackAction::Dismiss) |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + async fn prompter_dismissed(&self, prompt_path: OwnedObjectPath) -> Result<(), ServiceError> { |
| 165 | + let signal_emitter = self.service.signal_emitter(prompt_path)?; |
| 166 | + let result = zvariant::Value::new::<Vec<OwnedObjectPath>>(vec![]) |
| 167 | + .try_into_owned() |
| 168 | + .unwrap(); |
| 169 | + |
| 170 | + tokio::spawn(async move { Prompt::completed(&signal_emitter, true, result).await }); |
| 171 | + Ok(()) |
| 172 | + } |
| 173 | +} |
0 commit comments