Adds headless login support for the cli app.
This commit is contained in:
@@ -45,7 +45,6 @@ impl NextcloudClient {
|
||||
|
||||
// Set Basic Auth header if credentials are provided
|
||||
if let Some(ref creds) = config.credentials {
|
||||
use reqwest::header::HeaderValue;
|
||||
let raw_auth = format!("{}:{}", creds.username, creds.app_password);
|
||||
let encoded = base64_encode(raw_auth.as_bytes());
|
||||
let mut auth_val = HeaderValue::from_str(&format!("Basic {encoded}"))
|
||||
@@ -126,6 +125,32 @@ impl NextcloudClient {
|
||||
Ok(status)
|
||||
}
|
||||
|
||||
/// Test server connectivity and verify user credentials via WebDAV PROPFIND.
|
||||
pub async fn test_connection(&self) -> Result<ServerStatus> {
|
||||
let status = self.check_status().await?;
|
||||
|
||||
if let Some(username) = self.username() {
|
||||
let root_dav = self.webdav_url("")?;
|
||||
let res = self
|
||||
.http
|
||||
.request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), root_dav)
|
||||
.header("Depth", "0")
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if res.status() == reqwest::StatusCode::UNAUTHORIZED {
|
||||
return Err(NextcloudError::AuthenticationFailed {
|
||||
username: username.to_string(),
|
||||
message: "Invalid credentials or unauthorized access".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
res.error_for_status()?;
|
||||
}
|
||||
|
||||
Ok(status)
|
||||
}
|
||||
|
||||
/// High-level orchestration method that uploads a file from disk and optionally generates a public share link.
|
||||
pub async fn upload_and_share<P: AsRef<Path>>(
|
||||
&self,
|
||||
|
||||
@@ -105,6 +105,18 @@ impl ClientConfig {
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a new `ClientConfig` with username and app password credentials.
|
||||
pub fn with_credentials(
|
||||
server_url_str: &str,
|
||||
username: impl Into<String>,
|
||||
app_password: impl Into<String>,
|
||||
) -> Result<Self> {
|
||||
Self::new(
|
||||
server_url_str,
|
||||
Some(AccountCredentials::new(username, app_password)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Set a custom request timeout.
|
||||
pub fn with_timeout(mut self, timeout: Duration) -> Self {
|
||||
self.timeout = timeout;
|
||||
|
||||
Reference in New Issue
Block a user