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

Exporting public key via callback

parent 0b7e36ed
No related branches found
No related tags found
3 merge requests!18Feature/651 outsource crypto,!17Feature/638 xml data,!15Feature/580 encrypted data
using System.Threading.Channels;
using FitConnect;
using FitConnect.Models;
using Microsoft.Extensions.Configuration;
......@@ -29,9 +30,9 @@ public static class SenderDemo {
var destinationId = config["FitConnect:Sender:DestinationId"];
var leikaKey = config["FitConnect:Sender:LeikaKey"];
Client
var sender = Client
.GetSender(FitConnectEnvironment.Testing, clientId, clientSecret, logger)
.WithDestination(destinationId)
.WithDestination(destinationId, (key) => Console.WriteLine(key))
.WithServiceType("FIT Connect Demo", leikaKey)
.WithEncryptedAttachments(new Dictionary<string, string>())
.WithEncryptedMetaData("")
......
......@@ -10,10 +10,12 @@ public interface ISender : IFitConnectClient {
/// <param name="ags"></param>
/// <param name="ars"></param>
/// <param name="areaId"></param>
/// <param name="receivedPublicKey">Callback for presenting the public key</param>
/// <returns></returns>
public ISenderWithDestination FindDestinationId(string leiaKey, string? ags = null,
string? ars = null,
string? areaId = null);
string? areaId = null,
Action<string>? receivedPublicKey = null);
/// <summary>
/// Configures the client for the given destination and loads the public key
......@@ -22,6 +24,8 @@ public interface ISender : IFitConnectClient {
/// unique identifier of the clients destination
/// <para>eg: 00000000-0000-0000-0000-000000000000</para>
/// </param>
/// <param name="receivedPublicKey">Callback for presenting the public key</param>
/// <returns>the upload step for attachments</returns>
public ISenderWithDestination WithDestination(string destinationId);
public ISenderWithDestination WithDestination(string destinationId,
Action<string>? receivedPublicKey = null);
}
......@@ -30,7 +30,7 @@ namespace FitConnect;
/// </example>
public partial class Sender : FitConnectClient, ISender, ISenderWithDestination,
ISenderWithAttachments, ISenderWithData, ISenderWithService, IWithEncryptedAttachments,
ISenderWithEncryptedMetaData, ISenderWithEncryptedData{
ISenderWithEncryptedMetaData, ISenderWithEncryptedData {
public string? PublicKey { get; set; }
public Submission? Submission { get; set; }
......@@ -46,22 +46,25 @@ public partial class Sender : FitConnectClient, ISender, ISenderWithDestination,
public ISenderWithDestination FindDestinationId(string leiaKey, string? ags = null,
string? ars = null,
string? areaId = null) {
string? areaId = null, Action<string>? receivedPublicKey = null) {
if (ags == null && ars == null && areaId == null)
throw new ArgumentException("One of the following must be provided: ags, ars, areaId");
var destinationId = RouteService.GetDestinationIdAsync(leiaKey, ags, ars, areaId).Result;
Logger?.LogInformation("Received destinations: {Destinations}",
destinationId.Select(d => d.DestinationId).Aggregate((a, b) => a + "," + b));
return WithDestination(destinationId.First().DestinationId);
return WithDestination(destinationId.First().DestinationId, receivedPublicKey);
}
public ISenderWithDestination WithDestination(string destinationId) {
public ISenderWithDestination WithDestination(string destinationId,
Action<string>? receivedPublicKey = null) {
if (!Regex.IsMatch(destinationId,
"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"))
throw new ArgumentException("The destination must be a valid GUID");
Submission = CreateSubmission(destinationId).Result;
if (PublicKey != null)
receivedPublicKey?.Invoke(PublicKey!);
return this;
}
......@@ -183,7 +186,7 @@ public partial class Sender : FitConnectClient, ISender, ISenderWithDestination,
return submission;
}
internal async Task<string> GetPublicKeyFromDestination(string destinationId) {
public async Task<string> GetPublicKeyFromDestination(string destinationId) {
var publicKey = await DestinationService.GetPublicKey(destinationId);
var keyIsValid = new CertificateHelper(Logger).ValidateCertificate(publicKey,
......
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