fixes for Nextcloud

This commit is contained in:
2023-10-02 15:25:16 +02:00
parent c7f5de4974
commit b2ef1e5cce
21 changed files with 233 additions and 101 deletions

View File

@@ -17,9 +17,19 @@ namespace Pilz.Networking.CloudProviders.Nextcloud
public class NextcloudClient : IDisposable
{
private readonly List<ClientBase> clients = new();
private NextcloudLogin? currentLogin;
public OcsApi Ocs { get; init; } = new();
public NextcloudLogin? CurrentLogin { get; private set; }
public NextcloudLogin? CurrentLogin
{
get => currentLogin;
private set
{
currentLogin = value;
Ocs.BaseUrl = value?.Server ?? string.Empty;
}
}
public CloudClient Cloud => GetClient<CloudClient>();
@@ -52,16 +62,16 @@ namespace Pilz.Networking.CloudProviders.Nextcloud
return instance;
}
public async Task<UserInfo?> Login(NextcloudLogin login)
public UserInfo? Login(NextcloudLogin login)
{
// Ensure we are logged out
await Logout();
Logout(false);
// Temporary set user login
CurrentLogin = login;
// Try get user info & check if user is enabled
var userInfo = await Cloud.GetUserInfo();
var userInfo = Cloud.GetUserInfo();
var isValid = userInfo != null && userInfo.Enabled;
// If invalid, reset login credentials
@@ -71,13 +81,13 @@ namespace Pilz.Networking.CloudProviders.Nextcloud
return userInfo;
}
public async Task<NextcloudLogin?> Login(string baseUrl, CancellationToken cancellationToken)
public NextcloudLogin? Login(string baseUrl, CancellationToken cancellationToken)
{
// Ensure we are logged out
await Logout();
Logout(false);
// Init the login process
var initResponse = await Ocs.LoginFlowV2.Init(baseUrl);
var initResponse = Ocs.LoginFlowV2.Init(baseUrl);
if (!string.IsNullOrEmpty(initResponse?.LoginUrl) && initResponse.Poll != null)
{
@@ -97,7 +107,7 @@ namespace Pilz.Networking.CloudProviders.Nextcloud
// Poll the credentials
if (!cancellationToken.IsCancellationRequested)
pollResponse = await Ocs.LoginFlowV2.Poll(initResponse.Poll);
pollResponse = Ocs.LoginFlowV2.Poll(initResponse.Poll);
}
// Check login credentials
@@ -108,18 +118,18 @@ namespace Pilz.Networking.CloudProviders.Nextcloud
return CurrentLogin;
}
public Task Logout()
public void Logout()
{
return Logout(true);
Logout(true);
}
public async Task Logout(bool logoutOnServer)
public void Logout(bool logoutOnServer)
{
if (CurrentLogin != null)
{
// Delete currently used app password
if (logoutOnServer)
await Ocs.Core.DeleteAppPassword();
Ocs.Core.DeleteAppPassword();
// Reset current login infos
CurrentLogin = null;