introduce a very simple Nextcloud API

This commit is contained in:
2023-09-27 14:12:55 +02:00
parent 7ea59cddb1
commit 2c28feacb5
23 changed files with 927 additions and 1 deletions

View File

@@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pilz.Networking.CloudProviders.Nextcloud;
using Pilz.Networking.CloudProviders.Nextcloud.Model;
using Pilz.Networking.CloudProviders.Nextcloud.OCS;
using Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses.LoginFlowV2;
namespace ConsoleApp9
{
public class NextcloudClient : IDisposable
{
private readonly OcsApi ocs = new();
public NextcloudLogin? CurrentLogin { get; private set; }
public NextcloudClient()
{
ocs.GetOcsApiAuthCredentails += Ocs_GetOcsApiAuthCredentails;
}
private void Ocs_GetOcsApiAuthCredentails(object sender, GetOcsApiAuthCredentailsEventArgs eventArgs)
{
if (sender == ocs)
eventArgs.Credentials = CurrentLogin.ToOcsApiAuthCredentials();
}
public async Task<UserInfo?> LoginAsync(NextcloudLogin login)
{
// Ensure we are logged out
await LogoutAsync();
// Temporary set user login
CurrentLogin = login;
// Try get user info & check if user is enabled
var userInfo = await GetUserInfoAsync();
var isValid = userInfo != null && userInfo.Enabled;
// If invalid, reset login credentials
if (!isValid)
CurrentLogin = null;
return userInfo;
}
public async Task<NextcloudLogin?> LoginAsync(string baseUrl, CancellationToken cancellationToken)
{
// Ensure we are logged out
await LogoutAsync();
// Init the login process
var initResponse = await ocs.LoginFlowV2.Init(baseUrl);
if (!string.IsNullOrEmpty(initResponse?.LoginUrl) && initResponse.Poll != null)
{
// Open the browser
Process.Start(new ProcessStartInfo
{
FileName = initResponse.LoginUrl,
UseShellExecute = true
});
// Poll for credentails in intervals
OcsResponseLoginFlowV2Credentials? pollResponse = null;
while (!cancellationToken.IsCancellationRequested && pollResponse is null)
{
// Wait 5 seconds
Thread.Sleep(5000);
// Poll the credentials
if (!cancellationToken.IsCancellationRequested)
pollResponse = await ocs.LoginFlowV2.Poll(initResponse.Poll);
}
// Check login credentials
if (pollResponse is not null)
CurrentLogin = new(pollResponse);
}
return CurrentLogin;
}
public Task<UserInfo?> GetUserInfoAsync()
{
if (!string.IsNullOrEmpty(CurrentLogin?.LoginName))
return GetUserInfoAsync(CurrentLogin.LoginName);
else
return Task.FromResult<UserInfo?>(null);
}
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)
{
if (CurrentLogin != null)
{
// Delete currently used app password
await ocs.Core.DeleteAppPassword();
// Reset current login infos
CurrentLogin = null;
}
}
public void Dispose()
{
ocs.Dispose();
GC.SuppressFinalize(this);
}
}
}