allow using alternate login names

This commit is contained in:
Robin Appelman 2021-01-22 14:59:48 +01:00
Родитель b9bb062320
Коммит 9acde6075c
4 изменённых файлов: 31 добавлений и 23 удалений

Просмотреть файл

@ -34,9 +34,13 @@ return [
'verb' => 'GET',
],
[
'name' => 'PreAuth#preAuth',
'name' => 'Auth#preAuth',
'url' => '/pre_auth',
'verb' => 'POST',
],
[
'name' => 'Auth#getUid',
'url' => '/uid',
],
],
];

Просмотреть файл

@ -30,7 +30,7 @@ use OCP\IRequest;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
class PreAuthController extends Controller {
class AuthController extends Controller {
private $queue;
private $random;
private $userSession;
@ -61,4 +61,13 @@ class PreAuthController extends Controller {
return new DataDisplayResponse($token);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @return DataDisplayResponse
*/
public function getUid() {
return new DataDisplayResponse($this->userSession->getUser()->getUID());
}
}

Просмотреть файл

@ -258,15 +258,9 @@ async fn socket_auth(
}
let client = NC_CLIENT.get().unwrap();
if client
client
.verify_credentials(username, password, forwarded_for)
.await?
{
log::info!("Authenticated socket for {}", username);
Ok(UserId::from(username))
} else {
Err(Report::msg("Invalid credentials"))
}
.await
}
async fn listen(

Просмотреть файл

@ -1,23 +1,18 @@
use crate::UserId;
use color_eyre::{eyre::WrapErr, Report, Result};
use reqwest::{StatusCode, Url};
use std::net::IpAddr;
pub struct Client {
http: reqwest::Client,
dav_url: Url,
base_url: Url,
}
impl Client {
pub fn new(base_url: &str) -> Result<Self> {
let base_url = Url::parse(base_url).wrap_err("Invalid base url")?;
let dav_url = base_url.join("remote.php/dav").unwrap();
let http = reqwest::Client::new();
Ok(Client {
http,
dav_url,
base_url,
})
Ok(Client { http, base_url })
}
pub async fn verify_credentials(
@ -25,10 +20,10 @@ impl Client {
username: &str,
password: &str,
forwarded_for: Vec<IpAddr>,
) -> Result<bool> {
) -> Result<UserId> {
let response = self
.http
.head(self.dav_url.clone())
.get(self.base_url.join("index.php/apps/notify_push/uid")?)
.basic_auth(username, Some(password))
.header(
"x-forwarded-for",
@ -49,8 +44,8 @@ impl Client {
.wrap_err("Error while connecting to nextcloud server")?;
match response.status() {
StatusCode::OK => Ok(true),
StatusCode::UNAUTHORIZED => Ok(false),
StatusCode::OK => Ok(response.text().await?.into()),
StatusCode::UNAUTHORIZED => Err(Report::msg(format!("Invalid credentials"))),
status if status.is_server_error() => {
Err(Report::msg(format!("Server error: {}", status)))
}
@ -64,7 +59,10 @@ impl Client {
pub async fn get_test_cookie(&self) -> Result<u32> {
Ok(self
.http
.get(self.base_url.join("index.php/apps/notify_push/test/cookie")?)
.get(
self.base_url
.join("index.php/apps/notify_push/test/cookie")?,
)
.send()
.await?
.json()
@ -74,7 +72,10 @@ impl Client {
pub async fn test_set_remote(&self, addr: IpAddr) -> Result<IpAddr> {
Ok(self
.http
.get(self.base_url.join("index.php/apps/notify_push/test/remote")?)
.get(
self.base_url
.join("index.php/apps/notify_push/test/remote")?,
)
.header("x-forwarded-for", addr.to_string())
.send()
.await?