huge work for nextcloud api
This commit is contained in:
@@ -6,39 +6,62 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.Client;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.Model;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.Model.Cloud;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.OCS;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses.LoginFlowV2;
|
||||
|
||||
namespace ConsoleApp9
|
||||
namespace Pilz.Networking.CloudProviders.Nextcloud
|
||||
{
|
||||
public class NextcloudClient : IDisposable
|
||||
{
|
||||
private readonly OcsApi ocs = new();
|
||||
private readonly List<ClientBase> clients = new();
|
||||
|
||||
public OcsApi Ocs { get; init; } = new();
|
||||
public NextcloudLogin? CurrentLogin { get; private set; }
|
||||
|
||||
public CloudClient Cloud => GetClient<CloudClient>();
|
||||
|
||||
public NextcloudClient()
|
||||
{
|
||||
ocs.GetOcsApiAuthCredentails += Ocs_GetOcsApiAuthCredentails;
|
||||
Ocs.GetOcsApiAuthCredentails += Ocs_GetOcsApiAuthCredentails;
|
||||
}
|
||||
|
||||
private void Ocs_GetOcsApiAuthCredentails(object sender, GetOcsApiAuthCredentailsEventArgs eventArgs)
|
||||
{
|
||||
if (sender == ocs)
|
||||
if (sender == Ocs)
|
||||
eventArgs.Credentials = CurrentLogin.ToOcsApiAuthCredentials();
|
||||
}
|
||||
|
||||
public async Task<UserInfo?> LoginAsync(NextcloudLogin login)
|
||||
public TClient GetClient<TClient>() where TClient : ClientBase
|
||||
{
|
||||
var instance = TryGetClient<TClient>();
|
||||
return instance is null ? throw new NullReferenceException() : instance;
|
||||
}
|
||||
|
||||
public TClient? TryGetClient<TClient>() where TClient : ClientBase
|
||||
{
|
||||
TClient? instance = (TClient?)clients.FirstOrDefault(n => n is TClient);
|
||||
|
||||
instance ??= (TClient?)Activator.CreateInstance(typeof(TClient), new object[] { this });
|
||||
|
||||
if (instance is not null)
|
||||
clients.Add(instance);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public async Task<UserInfo?> Login(NextcloudLogin login)
|
||||
{
|
||||
// Ensure we are logged out
|
||||
await LogoutAsync();
|
||||
await Logout();
|
||||
|
||||
// Temporary set user login
|
||||
CurrentLogin = login;
|
||||
|
||||
// Try get user info & check if user is enabled
|
||||
var userInfo = await GetUserInfoAsync();
|
||||
var userInfo = await Cloud.GetUserInfo();
|
||||
var isValid = userInfo != null && userInfo.Enabled;
|
||||
|
||||
// If invalid, reset login credentials
|
||||
@@ -48,13 +71,13 @@ namespace ConsoleApp9
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
public async Task<NextcloudLogin?> LoginAsync(string baseUrl, CancellationToken cancellationToken)
|
||||
public async Task<NextcloudLogin?> Login(string baseUrl, CancellationToken cancellationToken)
|
||||
{
|
||||
// Ensure we are logged out
|
||||
await LogoutAsync();
|
||||
await Logout();
|
||||
|
||||
// Init the login process
|
||||
var initResponse = await ocs.LoginFlowV2.Init(baseUrl);
|
||||
var initResponse = await Ocs.LoginFlowV2.Init(baseUrl);
|
||||
|
||||
if (!string.IsNullOrEmpty(initResponse?.LoginUrl) && initResponse.Poll != null)
|
||||
{
|
||||
@@ -74,7 +97,7 @@ namespace ConsoleApp9
|
||||
|
||||
// Poll the credentials
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
pollResponse = await ocs.LoginFlowV2.Poll(initResponse.Poll);
|
||||
pollResponse = await Ocs.LoginFlowV2.Poll(initResponse.Poll);
|
||||
}
|
||||
|
||||
// Check login credentials
|
||||
@@ -85,35 +108,18 @@ namespace ConsoleApp9
|
||||
return CurrentLogin;
|
||||
}
|
||||
|
||||
public Task<UserInfo?> GetUserInfoAsync()
|
||||
public Task Logout()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(CurrentLogin?.LoginName))
|
||||
return GetUserInfoAsync(CurrentLogin.LoginName);
|
||||
else
|
||||
return Task.FromResult<UserInfo?>(null);
|
||||
return Logout(true);
|
||||
}
|
||||
|
||||
public async Task<UserInfo?> GetUserInfoAsync(string username)
|
||||
{
|
||||
var result = await ocs.Cloud.GetUserMeta(username);
|
||||
|
||||
if (result?.Data != null)
|
||||
return new UserInfo(result.Data);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Task LogoutAsync()
|
||||
{
|
||||
return LogoutAsync(true);
|
||||
}
|
||||
|
||||
public async Task LogoutAsync(bool logoutOnServer)
|
||||
public async Task Logout(bool logoutOnServer)
|
||||
{
|
||||
if (CurrentLogin != null)
|
||||
{
|
||||
// Delete currently used app password
|
||||
await ocs.Core.DeleteAppPassword();
|
||||
if (logoutOnServer)
|
||||
await Ocs.Core.DeleteAppPassword();
|
||||
|
||||
// Reset current login infos
|
||||
CurrentLogin = null;
|
||||
@@ -122,7 +128,7 @@ namespace ConsoleApp9
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ocs.Dispose();
|
||||
Ocs.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user