From ec459ed2f7a67645e3b5aac22d98d0e062c186bd Mon Sep 17 00:00:00 2001 From: Andreas Date: Fri, 1 Nov 2024 11:31:27 +0100 Subject: [PATCH 1/3] Added tests for `Auth` without `ApiKey` This commit adds tests to check if the authentiaction also works if no `ApiKey` was provided, as it did in `1.3.0`. Fixed #137 --- .../MoonrakerSharpWebApi.Test.cs | 60 +++++++++++++++++++ .../MoonrakerClient.Auth.cs | 7 +++ .../MoonrakerClient.Files.cs | 2 +- src/MoonrakerSharpWebApi/MoonrakerClient.cs | 6 -- .../MoonrakerConnectionBuilder.cs | 25 +++++++- 5 files changed, 92 insertions(+), 8 deletions(-) diff --git a/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs b/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs index 4944d06..c40c2a6 100644 --- a/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs +++ b/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs @@ -48,6 +48,66 @@ public void TearDown() client?.Dispose(); } + [Test] + public async Task BuildWitOneShotTokenAsync() + { + try + { + var client = new MoonrakerClient.MoonrakerConnectionBuilder() + .WithName("Test") + .WithServerAddress(_host, _port, _ssl) + .Build(); + KlipperAccessTokenResult? token = await client.GetOneshotTokenAsync(); + + client.OneShotToken = token?.Result ?? string.Empty; + Assert.That(!string.IsNullOrEmpty(client.OneShotToken)); + + KlipperMachineInfo? info = await client.GetMachineSystemInfoAsync(); + Assert.That(info is not null); + } + catch (Exception ex) + { + Assert.Fail(ex.Message); + } + } + + [Test] + public async Task BuildWitUserTokenAsync() + { + try + { + var client = new MoonrakerClient.MoonrakerConnectionBuilder() + .WithName("Test") + .WithServerAddress(_host, _port, _ssl) + .Build(); + string? apiKey = await client.LoginUserForApiKeyAsync("user", "pwd"); + + /* + * Set by the LoginUserAsync() method + * UserToken = queryResult?.Result?.Token ?? string.Empty; + * RefreshToken = queryResult?.Result?.RefreshToken ?? string.Empty; + */ + Assert.That(!string.IsNullOrEmpty(client.UserToken)); + Assert.That(!string.IsNullOrEmpty(client.RefreshToken)); + + // This should be enough to authenticate. + KlipperMachineInfo? info = await client.GetMachineSystemInfoAsync(); + Assert.That(info is not null); + + client.AuthHeaders.Clear(); + // Also try with the api key to verify + client.ApiKey = apiKey ?? string.Empty; + Assert.That(!string.IsNullOrEmpty(client.ApiKey)); + + info = await client.GetMachineSystemInfoAsync(); + Assert.That(info is not null); + } + catch (Exception ex) + { + Assert.Fail(ex.Message); + } + } + [Test] public void SerializeJsonTest() { diff --git a/src/MoonrakerSharpWebApi/MoonrakerClient.Auth.cs b/src/MoonrakerSharpWebApi/MoonrakerClient.Auth.cs index 772ecf5..afc1916 100644 --- a/src/MoonrakerSharpWebApi/MoonrakerClient.Auth.cs +++ b/src/MoonrakerSharpWebApi/MoonrakerClient.Auth.cs @@ -8,6 +8,13 @@ namespace AndreasReitberger.API.Moonraker public partial class MoonrakerClient { #region Auth + /* + [ObservableProperty] + [property: JsonIgnore, System.Text.Json.Serialization.JsonIgnore, XmlIgnore] + new string apiKey = string.Empty; + partial void OnApiKeyChanged(string value) => AddOrUpdateAuthHeader("usertoken", value); + */ + [ObservableProperty] [property: JsonIgnore, System.Text.Json.Serialization.JsonIgnore, XmlIgnore] diff --git a/src/MoonrakerSharpWebApi/MoonrakerClient.Files.cs b/src/MoonrakerSharpWebApi/MoonrakerClient.Files.cs index a8a51e3..269d564 100644 --- a/src/MoonrakerSharpWebApi/MoonrakerClient.Files.cs +++ b/src/MoonrakerSharpWebApi/MoonrakerClient.Files.cs @@ -35,7 +35,7 @@ partial void OnFilesChanged(ObservableCollection value) [ObservableProperty] [property: JsonIgnore, System.Text.Json.Serialization.JsonIgnore, XmlIgnore] - ObservableCollection availableDirectories = new(); + ObservableCollection availableDirectories = []; partial void OnAvailableDirectoriesChanged(ObservableCollection value) { /* diff --git a/src/MoonrakerSharpWebApi/MoonrakerClient.cs b/src/MoonrakerSharpWebApi/MoonrakerClient.cs index d1d6279..f7b61e7 100644 --- a/src/MoonrakerSharpWebApi/MoonrakerClient.cs +++ b/src/MoonrakerSharpWebApi/MoonrakerClient.cs @@ -8,15 +8,12 @@ using AndreasReitberger.API.Print3dServer.Core.Interfaces; using AndreasReitberger.Core.Utilities; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; -using System.Net.Http; using System.Security; -using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading; @@ -961,7 +958,6 @@ public bool CheckIfConfigurationHasChanged(object temp) KlipperAccessTokenResult? resultObject = null; try { - //object cmd = new { name = ScriptName }; string targetUri = $"{MoonrakerCommands.Access}"; result = await SendRestApiRequestAsync( requestTargetUri: targetUri, @@ -969,7 +965,6 @@ public bool CheckIfConfigurationHasChanged(object temp) command: "oneshot_token", jsonObject: null, authHeaders: AuthHeaders, - //urlSegments: urlSegements, cts: default ) .ConfigureAwait(false); @@ -1827,7 +1822,6 @@ await SendRestApiRequestAsync(MoonrakerCommandBase.machine, Method.Post, "servic command: "login", jsonObject: cmd, authHeaders: AuthHeaders, - //urlSegments: urlSegments, cts: default ) .ConfigureAwait(false); diff --git a/src/MoonrakerSharpWebApi/MoonrakerConnectionBuilder.cs b/src/MoonrakerSharpWebApi/MoonrakerConnectionBuilder.cs index 458bbb9..bf7bccc 100644 --- a/src/MoonrakerSharpWebApi/MoonrakerConnectionBuilder.cs +++ b/src/MoonrakerSharpWebApi/MoonrakerConnectionBuilder.cs @@ -1,6 +1,8 @@ -using System; +using AndreasReitberger.API.Moonraker.Models; +using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace AndreasReitberger.API.Moonraker { @@ -35,6 +37,27 @@ public MoonrakerConnectionBuilder WithApiKey(string apiKey) return this; } + /* + public async Task WithUserTokenAsync(string? userToken = null) + { + if (userToken is null) + { + KlipperAccessTokenResult? tokenResult = await _client.GetOneshotTokenAsync(); + userToken = tokenResult?.Result; + } + _client.OneShotToken = userToken ?? string.Empty; + return this; + } + + public async Task WitLoginAsync(string username, string password) + { + string apiToken = await _client.LoginUserForApiKeyAsync(username, password); + _client.ApiKey = apiToken; + await _client.LogoutCurrentUserAsync(); + return this; + } + */ + public MoonrakerConnectionBuilder WithName(string name) { _client.ServerName = name; From 7fecc36145ebfc857236d58eee05c9977046b6f1 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sat, 9 Nov 2024 16:31:13 +0100 Subject: [PATCH 2/3] Added `user` and `pwd` to secrets --- src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs | 5 ++++- src/MoonrakerSharpWebApi.Test/SecretAppSetting.cs | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs b/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs index c40c2a6..79bdd0e 100644 --- a/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs +++ b/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs @@ -19,6 +19,8 @@ public class Tests private readonly string _host = SecretAppSettingReader.ReadSection("TestSetup").Ip ?? ""; private readonly int _port = 80; private readonly string _api = SecretAppSettingReader.ReadSection("TestSetup").ApiKey ?? ""; + private readonly string _user = SecretAppSettingReader.ReadSection("TestSetup").Username ?? ""; + private readonly string _pwd = SecretAppSettingReader.ReadSection("TestSetup").Password ?? ""; private readonly bool _ssl = false; private readonly bool _skipOnlineTests = true; @@ -76,11 +78,12 @@ public async Task BuildWitUserTokenAsync() { try { + Assert.That(!string.IsNullOrEmpty(_user) && !string.IsNullOrEmpty(_pwd), "Provide a user and password in your secrets.json file first!"); var client = new MoonrakerClient.MoonrakerConnectionBuilder() .WithName("Test") .WithServerAddress(_host, _port, _ssl) .Build(); - string? apiKey = await client.LoginUserForApiKeyAsync("user", "pwd"); + string? apiKey = await client.LoginUserForApiKeyAsync(_user, _pwd); /* * Set by the LoginUserAsync() method diff --git a/src/MoonrakerSharpWebApi.Test/SecretAppSetting.cs b/src/MoonrakerSharpWebApi.Test/SecretAppSetting.cs index a3593d1..52b7b3b 100644 --- a/src/MoonrakerSharpWebApi.Test/SecretAppSetting.cs +++ b/src/MoonrakerSharpWebApi.Test/SecretAppSetting.cs @@ -4,5 +4,7 @@ public class SecretAppSetting { public string? ApiKey { get; set; } public string? Ip { get; set; } + public string? Username { get; set; } + public string? Password { get; set; } } } From 05e117f7ad65e024bc9f88000694db1919d05636 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sat, 9 Nov 2024 17:20:55 +0100 Subject: [PATCH 3/3] Updated tests --- .../MoonrakerSharpWebApi.SQLite.csproj | 1 + .../MoonrakerSharpWebApi.Test.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/MoonrakerSharpWebApi.SQLite/MoonrakerSharpWebApi.SQLite.csproj b/src/MoonrakerSharpWebApi.SQLite/MoonrakerSharpWebApi.SQLite.csproj index f42fe09..bf05806 100644 --- a/src/MoonrakerSharpWebApi.SQLite/MoonrakerSharpWebApi.SQLite.csproj +++ b/src/MoonrakerSharpWebApi.SQLite/MoonrakerSharpWebApi.SQLite.csproj @@ -13,6 +13,7 @@ Apache-2.0 README.md MoonrakerSharpWebApi.SQLite + 2fcb2e15-a623-46ce-91ea-5913ca527be9 diff --git a/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs b/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs index 79bdd0e..b28e322 100644 --- a/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs +++ b/src/MoonrakerSharpWebApi.Test/MoonrakerSharpWebApi.Test.cs @@ -96,14 +96,29 @@ public async Task BuildWitUserTokenAsync() // This should be enough to authenticate. KlipperMachineInfo? info = await client.GetMachineSystemInfoAsync(); Assert.That(info is not null); + info = null; + + // Remove the api key to test if the UserToken works as well + var lastHeader = client.AuthHeaders.Last(); + client.AuthHeaders.Remove(lastHeader.Key); + + info = await client.GetMachineSystemInfoAsync(); + Assert.That(info is not null); client.AuthHeaders.Clear(); + client.ApiKey = ""; + // Also try with the api key to verify client.ApiKey = apiKey ?? string.Empty; Assert.That(!string.IsNullOrEmpty(client.ApiKey)); info = await client.GetMachineSystemInfoAsync(); Assert.That(info is not null); + + await client.LogoutCurrentUserAsync(); + + info = await client.GetMachineSystemInfoAsync(); + Assert.That(info is not null); } catch (Exception ex) {