feat(photos): unlock hidden albums with system login

This commit is contained in:
nvrl committed 2026-11-19 20:55:26 +01:00
1 parent 1e6043d12a
commit cc020bfb54
23 files changed
+175 -129

No files matched your search

-1
View File
@@ -10,7 +10,6 @@ homepage.workspace = true
description = "Core auth/session/config for the Proton Drive Linux client"
[dependencies]
argon2 = "0.5"
proton-sdk.workspace = true
proton-drive-rs.workspace = true
chrono.workspace = true
-42
View File
@@ -128,48 +128,6 @@ fn with_keyring<T>(op: impl Fn(&Entry) -> keyring_core::Result<T>) -> Result<T>
with_keyring_entry(KEYRING_SERVICE, KEYRING_USER, op)
}
/// Verify the machine-local Gallery passphrase, setting it on first use.
pub fn unlock_gallery(username: &str, passphrase: &str) -> std::result::Result<(), String> {
use argon2::password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString};
use std::io::Read;
if passphrase.is_empty() || passphrase.len() > 1024 {
return Err("Enter your Gallery passphrase.".into());
}
static VERIFY: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
let _verify = VERIFY.lock();
let service = "io.narl.protongallery";
let user = format!("hidden-albums:{username}");
let argon = argon2::Argon2::default();
match with_keyring_entry(service, &user, Entry::get_password) {
Ok(stored) => {
let hash = PasswordHash::new(&stored)
.map_err(|_| "The saved Gallery passphrase is unreadable.")?;
argon
.verify_password(passphrase.as_bytes(), &hash)
.map_err(|_| "The Gallery passphrase is incorrect.".into())
}
Err(Error::Keyring(keyring_core::Error::NoEntry)) => {
if passphrase.chars().count() < 12 {
return Err("Choose a Gallery passphrase with at least 12 characters.".into());
}
let mut bytes = [0; 16];
std::fs::File::open("/dev/urandom")
.and_then(|mut file| file.read_exact(&mut bytes))
.map_err(|_| "Couldn’t generate the passphrase salt.")?;
let salt = SaltString::encode_b64(&bytes)
.map_err(|_| "Couldn’t generate the passphrase salt.")?;
let hash = argon
.hash_password(passphrase.as_bytes(), &salt)
.map_err(|_| "Couldn’t protect the Gallery passphrase.")?
.to_string();
with_keyring_entry(service, &user, |entry| entry.set_password(&hash))
.map_err(|_| "Couldn’t save the Gallery passphrase in the system keyring.".into())
}
Err(_) => Err("The system keyring is locked or unavailable.".into()),
}
}
/// Run `op` on the entry `service`/`user`, reconnecting once if the Secret
/// Service fails the call.
///
+1 -3
View File
@@ -2019,9 +2019,7 @@ pub struct MappingProgress {
/// Actions on saved Drive folder galleries.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum GalleryAction {
Unlock {
passphrase: String,
},
AuthenticateHidden,
Lock,
List {
album: Option<GalleryPlace>,
+1
View File
@@ -27,6 +27,7 @@ tokio.workspace = true
tracing.workspace = true
fuser.workspace = true
libc.workspace = true
rustix = { version = "1", features = ["net", "process"] }
serde.workspace = true
serde_json.workspace = true
base64.workspace = true
+7 -2
View File
@@ -396,8 +396,8 @@ fn touched_topics(request: &CtlRequest) -> &'static [Topic] {
use CtlRequest as R;
match request {
R::Pin { .. } | R::Unpin { .. } => &[Topic::Files],
// Unlock affects private requests only. Publishing the lock notification
// here for Unlock as well could close the view that just opened.
// Only locking invalidates a hidden view. Authentication does not
// change the public catalog and must not emit this notification.
R::DriveGallery {
action: pdfs_core::control::GalleryAction::Lock,
} => &[Topic::GalleryVisibility],
@@ -952,6 +952,11 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
},
None => CtlResponse::error(CoreError::invalid(format!("bad uid: {uid}"))),
},
Ok(CtlRequest::DriveGallery {
action: pdfs_core::control::GalleryAction::AuthenticateHidden,
}) => core
.authenticate_hidden_gallery(&stream)
.unwrap_or_else(CtlResponse::error),
Ok(CtlRequest::DriveGallery { action }) => core
.drive_gallery(action)
.unwrap_or_else(CtlResponse::error),
+32 -12
View File
@@ -431,20 +431,40 @@ impl Core {
.ok_or_else(|| CoreError::remote("The Gallery session is unavailable"))
}
pub(super) fn authenticate_hidden_gallery(
&self,
stream: &std::os::unix::net::UnixStream,
) -> CoreResult<Response> {
let gallery = self.gallery_host()?;
let access = gallery.access.load(Ordering::SeqCst);
let peer = rustix::net::sockopt::socket_peercred(stream)
.map_err(|_| CoreError::invalid("Couldn’t identify the authentication requester."))?;
if peer.uid != rustix::process::geteuid() {
return Err(CoreError::invalid(
"Use your Linux sign-in to unlock Hidden albums.",
));
}
let subject = pg_app::desktop_auth::Subject::new(
peer.pid.as_raw_nonzero().get() as u32,
peer.uid.as_raw(),
)
.map_err(gallery_error)?;
self.rt
.block_on(subject.authenticate("io.narl.proton-drive-linux.unlock-hidden-albums"))
.map_err(gallery_error)?;
if gallery.cancel.is_cancelled() {
return Err(CoreError::invalid("The Gallery session has ended."));
}
gallery
.access
.compare_exchange(access, access | 1, Ordering::SeqCst, Ordering::SeqCst)
.map_err(|_| CoreError::invalid("Hidden albums were locked"))?;
Ok(done())
}
pub(super) fn drive_gallery(&self, action: GalleryAction) -> CoreResult<Response> {
let gallery = self.gallery_host()?;
let action = match action {
GalleryAction::Unlock { passphrase } => {
let passphrase = zeroize::Zeroizing::new(passphrase);
let access = gallery.access.load(Ordering::SeqCst);
pdfs_core::auth::unlock_gallery(gallery.session.username(), &passphrase)
.map_err(gallery_error)?;
gallery
.access
.compare_exchange(access, access | 1, Ordering::SeqCst, Ordering::SeqCst)
.map_err(|_| CoreError::invalid("Hidden albums were locked"))?;
return Ok(done());
}
GalleryAction::Lock => {
gallery.lock();
return Ok(done());
@@ -457,7 +477,7 @@ impl Core {
.ensure_visibility(self, app, matches!(action, GalleryAction::Refresh))
.await?;
match action {
GalleryAction::Unlock { .. } | GalleryAction::Lock => unreachable!(),
GalleryAction::AuthenticateHidden | GalleryAction::Lock => unreachable!(),
GalleryAction::List {
album,
offset,
@@ -31,6 +31,7 @@ pub(crate) struct Browser {
root_reload: Cell<bool>,
preview: glib::WeakRef<adw::Dialog>,
unlock_pending: Cell<bool>,
unlock_verified: Cell<bool>,
rows: gio::ListStore,
scroll: gtk4::ScrolledWindow,
title: adw::WindowTitle,
@@ -75,7 +76,9 @@ impl Browser {
self.next_request();
self.loading.set(false);
self.total.set(0);
if self.unlocked.replace(false) || self.unlock_pending.get() {
let was_unlocked = self.unlocked.replace(false);
let was_verified = self.unlock_verified.replace(false);
if was_unlocked || was_verified || self.unlock_pending.get() {
self.lock();
}
if let Some(preview) = self.preview.upgrade() {
@@ -120,6 +123,15 @@ impl Browser {
if !self.active() || self.chooser.borrow().is_some() || self.unlock_pending.get() {
return;
}
if self.unlock_verified.get() {
if locked {
self.close();
}
return;
}
if !self.unlocked.get() && self.current().is_none() {
return;
}
if let Some(preview) = self.preview.upgrade() {
preview.force_close();
}
@@ -393,7 +405,6 @@ impl Browser {
}
fn change(self: &Rc<Self>, action: GalleryAction) {
let unlock = matches!(action, GalleryAction::Unlock { .. });
let update_root = matches!(
action,
GalleryAction::Add { .. }
@@ -401,9 +412,6 @@ impl Browser {
| GalleryAction::Refresh
| GalleryAction::Cover { .. }
);
if unlock && self.unlock_pending.replace(true) {
return;
}
self.unlock.set_visible(false);
let generation = self.next_request();
self.status.set_label(&gettext("Updating gallery…"));
@@ -414,25 +422,11 @@ impl Browser {
let browser = self.clone();
glib::spawn_future_local(async move {
let result = rx.recv().await;
if unlock {
browser.unlock_pending.set(false);
browser.unlock.set_visible(
browser.active()
&& browser.generation.get() == generation
&& !matches!(result, Ok(Ok(Response::Ok { .. }))),
);
}
if browser.generation.get() != generation {
if unlock && matches!(result, Ok(Ok(Response::Ok { .. }))) {
browser.lock();
}
return;
}
match result {
Ok(Ok(Response::Ok { .. })) => {
if unlock {
browser.unlocked.set(true);
}
let added = browser.chooser.borrow().is_some();
if update_root {
browser.load_root();
@@ -467,36 +461,58 @@ impl Browser {
}
fn prompt_unlock(self: &Rc<Self>) {
if self.unlock_pending.get() {
if self.unlock_pending.replace(true) {
return;
}
let Some(parent) = ui_window(&self.ui) else {
return;
};
let dialog = adw::AlertDialog::builder()
.heading(gettext("Unlock Hidden Albums"))
.body(gettext("Enter your Gallery passphrase. On first use, choose one with at least 12 characters."))
.build();
let password = gtk4::PasswordEntry::builder().show_peek_icon(true).build();
dialog.set_extra_child(Some(&password));
dialog.add_response("cancel", &gettext("Cancel"));
dialog.add_response("unlock", &gettext("Unlock"));
dialog.set_response_appearance("unlock", adw::ResponseAppearance::Suggested);
dialog.set_default_response(Some("unlock"));
let browser = Rc::downgrade(self);
dialog.connect_response(None, move |_, response| {
let passphrase = password.text().to_string();
password.set_text("");
if response == "unlock"
&& let Some(browser) = browser.upgrade()
{
browser.enter();
browser.more.set_visible(false);
browser.title.set_title(&gettext("Hidden albums"));
browser.change(GalleryAction::Unlock { passphrase });
self.enter();
self.more.set_visible(false);
self.unlock.set_visible(false);
self.title.set_title(&gettext("Hidden albums"));
self.status
.set_label(&gettext("Use your Linux sign-in to unlock Hidden albums."));
let generation = self.next_request();
let rx = spawn_request(
self.ui.dirs.control_socket(),
Request::DriveGallery {
action: GalleryAction::AuthenticateHidden,
},
);
let browser = self.clone();
glib::spawn_future_local(async move {
let result = rx.recv().await;
browser.unlock_pending.set(false);
if browser.generation.get() != generation {
if matches!(result, Ok(Ok(Response::Ok { .. }))) {
browser.lock();
}
return;
}
match result {
Ok(Ok(Response::Ok { .. })) => {
browser.unlock_verified.set(true);
browser.reveal_verified();
}
Ok(Ok(Response::Error { message, .. })) => {
browser.status.set_label(&message);
browser.unlock.set_visible(true);
}
_ => {
browser
.status
.set_label(&gettext("Couldn't reach Proton Drive."));
browser.unlock.set_visible(true);
}
}
});
dialog.present(Some(&parent));
}
fn reveal_verified(self: &Rc<Self>) {
if ui_window(&self.ui).is_some_and(|window| window.is_active())
&& self.unlock_verified.replace(false)
{
self.unlocked.set(true);
self.load(0, 0.0);
}
}
pub(crate) fn back(self: &Rc<Self>) {
@@ -713,6 +729,7 @@ pub(crate) fn wire(
root_loading: Cell::new(false),
preview: glib::WeakRef::new(),
unlock_pending: Cell::new(false),
unlock_verified: Cell::new(false),
rows,
scroll: scroll.clone(),
title,
@@ -737,10 +754,12 @@ pub(crate) fn wire(
*ui.drive_galleries.borrow_mut() = Rc::downgrade(&browser);
let weak = Rc::downgrade(&browser);
window.connect_is_active_notify(move |window| {
if !window.is_active()
&& let Some(browser) = weak.upgrade()
&& (browser.unlocked.get() || browser.unlock_pending.get())
{
let Some(browser) = weak.upgrade() else {
return;
};
if window.is_active() {
browser.reveal_verified();
} else if browser.unlocked.get() {
browser.close();
}
});