mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-19 01:55:04 +01:00
init, scaffhold + fuse
This commit is contained in:
commit
e9c8ddd18b
12 files changed
+5580
No files matched your search
@@ -0,0 +1,132 @@
|
||||
//! Login, session persistence, and Drive-client construction.
|
||||
//!
|
||||
//! A successful login is persisted to the OS keyring as a single JSON blob
|
||||
//! holding the resumable session tokens plus the mailbox password (needed to
|
||||
//! rebuild the key chain on resume). The daemon resumes from this blob with no
|
||||
//! interactive step; the refresh token auto-renews via the HTTP client's 401
|
||||
//! path, so no fresh 2FA is required until the refresh token itself expires.
|
||||
|
||||
use keyring::Entry;
|
||||
use proton_drive_rs::ProtonDriveClient;
|
||||
use proton_sdk::config::ProtonClientConfiguration;
|
||||
use proton_sdk::session::{PasswordMode, ProtonApiSession, ResumeParameters};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::config::{APP_VERSION, KEYRING_SERVICE, USER_AGENT};
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
/// Fixed keyring account name for the single stored session blob.
|
||||
const KEYRING_USER: &str = "session";
|
||||
|
||||
/// Everything needed to resume a session unattended, persisted to the keyring.
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct StoredSession {
|
||||
pub session_id: String,
|
||||
pub username: String,
|
||||
pub user_id: String,
|
||||
pub access_token: String,
|
||||
pub refresh_token: String,
|
||||
pub scopes: Vec<String>,
|
||||
/// `1` = single password, `2` = dual (Proton wire value).
|
||||
pub password_mode: u8,
|
||||
/// Mailbox (data) password — required by `ProtonDriveClient::new` to derive
|
||||
/// the key chain. Lives only in the OS keyring, never on disk in cleartext.
|
||||
pub mailbox_password: String,
|
||||
}
|
||||
|
||||
impl StoredSession {
|
||||
fn into_params(&self) -> ResumeParameters {
|
||||
ResumeParameters {
|
||||
session_id: self.session_id.clone().into(),
|
||||
username: self.username.clone(),
|
||||
user_id: self.user_id.clone().into(),
|
||||
access_token: self.access_token.clone(),
|
||||
refresh_token: self.refresh_token.clone(),
|
||||
scopes: self.scopes.clone(),
|
||||
is_waiting_for_second_factor_code: false,
|
||||
password_mode: match self.password_mode {
|
||||
1 => PasswordMode::Single,
|
||||
_ => PasswordMode::Dual,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn client_config() -> ProtonClientConfiguration {
|
||||
ProtonClientConfiguration::new(APP_VERSION).with_user_agent(USER_AGENT)
|
||||
}
|
||||
|
||||
fn keyring_entry() -> Result<Entry> {
|
||||
Ok(Entry::new(KEYRING_SERVICE, KEYRING_USER)?)
|
||||
}
|
||||
|
||||
/// Run an interactive SRP + (optional) 2FA login and persist the session.
|
||||
///
|
||||
/// `get_totp` is only invoked when the account requires a second factor, so
|
||||
/// callers can defer prompting until it is actually needed.
|
||||
pub async fn login(
|
||||
username: &str,
|
||||
password: &str,
|
||||
get_totp: impl FnOnce() -> Result<String>,
|
||||
) -> Result<()> {
|
||||
let mut session =
|
||||
ProtonApiSession::begin(client_config(), username, password.as_bytes()).await?;
|
||||
|
||||
if session.is_waiting_for_second_factor() {
|
||||
let code = get_totp()?;
|
||||
session.apply_second_factor_code(code.trim()).await?;
|
||||
}
|
||||
|
||||
save(&session, password).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Persist the session's current tokens + mailbox password to the keyring.
|
||||
pub async fn save(session: &ProtonApiSession, mailbox_password: &str) -> Result<()> {
|
||||
let tokens = session.current_tokens().await;
|
||||
let stored = StoredSession {
|
||||
session_id: session.session_id().as_str().to_owned(),
|
||||
username: session.username().to_owned(),
|
||||
user_id: session.user_id().as_str().to_owned(),
|
||||
access_token: tokens.access_token,
|
||||
refresh_token: tokens.refresh_token,
|
||||
scopes: session.scopes().to_vec(),
|
||||
password_mode: match session.password_mode() {
|
||||
PasswordMode::Single => 1,
|
||||
PasswordMode::Dual => 2,
|
||||
},
|
||||
mailbox_password: mailbox_password.to_owned(),
|
||||
};
|
||||
let json = serde_json::to_string(&stored)?;
|
||||
keyring_entry()?.set_password(&json)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Load the persisted session blob, or `Error::NotLoggedIn` if absent.
|
||||
pub fn load() -> Result<StoredSession> {
|
||||
let entry = keyring_entry()?;
|
||||
match entry.get_password() {
|
||||
Ok(json) => Ok(serde_json::from_str(&json)?),
|
||||
Err(keyring::Error::NoEntry) => Err(Error::NotLoggedIn),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Forget the persisted session (best-effort; absent entry is not an error).
|
||||
pub fn logout() -> Result<()> {
|
||||
match keyring_entry()?.delete_credential() {
|
||||
Ok(()) | Err(keyring::Error::NoEntry) => Ok(()),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Resume a persisted session and build an authenticated Drive client.
|
||||
///
|
||||
/// Re-persists tokens afterward in case the refresh path rotated them. Returns
|
||||
/// `Error::NotLoggedIn` when no session has been saved.
|
||||
pub async fn resume_client() -> Result<(ProtonDriveClient, ProtonApiSession)> {
|
||||
let stored = load()?;
|
||||
let session = ProtonApiSession::resume(client_config(), stored.into_params())?;
|
||||
let client = ProtonDriveClient::new(&session, stored.mailbox_password.clone().into_bytes());
|
||||
Ok((client, session))
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
//! Static client identity and per-user filesystem paths.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use directories::ProjectDirs;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
/// Proton requires `external-drive-{name}@{semver}-{channel}` (channel ∈
|
||||
/// stable/beta/alpha); a malformed value trips the 422 anti-abuse path.
|
||||
pub const APP_VERSION: &str = "external-drive-linux@0.1.0-alpha";
|
||||
pub const USER_AGENT: &str = "proton-drive-linux/0.1.0";
|
||||
|
||||
/// Keyring service name; one entry per credential kind keyed by username.
|
||||
pub const KEYRING_SERVICE: &str = "proton-drive-linux";
|
||||
|
||||
/// Resolved XDG locations for state, cache, and the default mountpoint.
|
||||
pub struct AppDirs {
|
||||
dirs: ProjectDirs,
|
||||
}
|
||||
|
||||
impl AppDirs {
|
||||
pub fn new() -> Result<Self> {
|
||||
let dirs = ProjectDirs::from("io", "narl", "proton-drive-linux")
|
||||
.ok_or_else(|| Error::Other("cannot resolve home directory".into()))?;
|
||||
Ok(Self { dirs })
|
||||
}
|
||||
|
||||
/// Persistent state: inode map / cache index DB lives here.
|
||||
pub fn state_dir(&self) -> PathBuf {
|
||||
// `state_dir` is Linux-only in `directories`; fall back to data dir.
|
||||
self.dirs
|
||||
.state_dir()
|
||||
.unwrap_or_else(|| self.dirs.data_dir())
|
||||
.to_path_buf()
|
||||
}
|
||||
|
||||
/// Hydrated file-content cache.
|
||||
pub fn cache_dir(&self) -> PathBuf {
|
||||
self.dirs.cache_dir().to_path_buf()
|
||||
}
|
||||
|
||||
/// Default mountpoint when the user does not pass one explicitly.
|
||||
pub fn default_mountpoint(&self) -> PathBuf {
|
||||
directories::UserDirs::new()
|
||||
.map(|u| u.home_dir().join("ProtonDrive"))
|
||||
.unwrap_or_else(|| PathBuf::from("/tmp/ProtonDrive"))
|
||||
}
|
||||
|
||||
/// Create state + cache dirs if missing.
|
||||
pub fn ensure(&self) -> Result<()> {
|
||||
std::fs::create_dir_all(self.state_dir())?;
|
||||
std::fs::create_dir_all(self.cache_dir())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
//! Crate-wide error type.
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[error("proton api: {0}")]
|
||||
Proton(#[from] proton_sdk::ProtonError),
|
||||
|
||||
#[error("keyring: {0}")]
|
||||
Keyring(#[from] keyring::Error),
|
||||
|
||||
#[error("serialization: {0}")]
|
||||
Serde(#[from] serde_json::Error),
|
||||
|
||||
#[error("io: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
/// No persisted session found — caller must run an interactive login.
|
||||
#[error("not logged in (no saved session)")]
|
||||
NotLoggedIn,
|
||||
|
||||
#[error("{0}")]
|
||||
Other(String),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
@@ -0,0 +1,14 @@
|
||||
//! Core building blocks for the Proton Drive Linux client: static client
|
||||
//! identity, XDG paths, and the login/session/keyring layer that every
|
||||
//! front-end (CLI today, daemon + GUI later) shares.
|
||||
|
||||
pub mod auth;
|
||||
pub mod config;
|
||||
pub mod error;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
// Re-export the SDK surface downstream crates need, so they depend on
|
||||
// `pdfs-core` rather than pinning the SDK directly.
|
||||
pub use proton_drive_rs::{self, Node, NodeKind, ProtonDriveClient};
|
||||
pub use proton_sdk;
|
||||
Reference in new issue
Block a user