Skip to content
Snippets Groups Projects
Commit da89f0ca authored by Klaus Fischer's avatar Klaus Fischer
Browse files

fix: added exception if invalid json keys are passed

parent 93f77aa8
No related branches found
No related tags found
1 merge request!91Feature/1308 attachment from string
......@@ -4,6 +4,7 @@ using FitConnect.Interfaces;
using FitConnect.Interfaces.Subscriber;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json.Linq;
namespace FitConnect;
......@@ -47,6 +48,9 @@ public static class ClientFactory {
List<string> privateDecryptionKeys,
string privateSigningKey,
ILogger? logger = null) {
CheckKeysForValidJsonFormat(privateDecryptionKeys, privateSigningKey);
if (privateDecryptionKeys.Count != 1)
throw new FitConnectInitialisationException(
"Only one private key for decryption is supported");
......@@ -61,6 +65,27 @@ public static class ClientFactory {
}
private static void CheckKeysForValidJsonFormat(List<string> privateDecryptionKeys,
string privateSigningKey) {
foreach (var key in privateDecryptionKeys) {
try {
var json = JObject.Parse(key);
}
catch (Exception e) {
throw new FitConnectInitialisationException(
"Private decryption key is not in valid JSON format", e);
}
}
try {
var json = JObject.Parse(privateSigningKey);
}
catch (Exception e) {
throw new FitConnectInitialisationException(
"Private signing key is not in valid JSON format", e);
}
}
/// <summary>
/// Returns a Router API implementation
/// </summary>
......
using System;
using System.Collections.Generic;
using Autofac;
using FitConnect;
using FitConnect.Exceptions;
using FitConnect.Interfaces;
using FluentAssertions;
using NUnit.Framework;
namespace BasicUnitTest;
[TestFixture]
public class SubscriberTests {
IFitConnectSettings _settings = null!;
[SetUp]
public void Setup() {
var container = MockContainer.Container.Create();
_settings = container.Resolve<IFitConnectSettings>();
}
[Test]
public void CreateSubscriber_ShouldPass() {
// Arrange
var clientId = Guid.NewGuid().ToString();
var clientSecret = "VerySecret";
var privateDecryptionKeys = _settings.PrivateDecryptionKeys;
var publicKey = _settings.PrivateSigningKey;
// Act
var subscriber = FitConnect.ClientFactory.GetSubscriberClient(FitConnectEnvironment.Test,
clientId, clientSecret, privateDecryptionKeys, publicKey);
// Assert
subscriber.Should().NotBeNull();
}
[Test]
public void CreateSubscriber_InvalidKeys_ShouldFail() {
// Arrange
var clientId = Guid.NewGuid().ToString();
var clientSecret = "VerySecret";
var privateDecryptionKeys = new List<string>() { "C:\\temp.key" };
var publicKey = "C:\\temp.key";
// Act & Assert
var exception = Assert.Throws<FitConnectInitialisationException>(() => {
var subscriber = FitConnect.ClientFactory.GetSubscriberClient(FitConnectEnvironment.Test,
clientId, clientSecret, privateDecryptionKeys, publicKey);
});
Console.WriteLine(exception?.Message);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment