From 351d6b00ff1de1905974016df80c657a93974c7d Mon Sep 17 00:00:00 2001
From: Klaus Fischer <klaus.fischer@eloware.com>
Date: Sat, 17 Sep 2022 17:08:25 +0200
Subject: [PATCH] Exporting public key via callback

---
 DemoRunner/SenderDemo.cs                |  5 +++--
 FitConnect/Interfaces/Sender/ISender.cs |  8 ++++++--
 FitConnect/Sender.cs                    | 13 ++++++++-----
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/DemoRunner/SenderDemo.cs b/DemoRunner/SenderDemo.cs
index 1dfcf0e8..ad1027e5 100644
--- a/DemoRunner/SenderDemo.cs
+++ b/DemoRunner/SenderDemo.cs
@@ -1,3 +1,4 @@
+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("")
diff --git a/FitConnect/Interfaces/Sender/ISender.cs b/FitConnect/Interfaces/Sender/ISender.cs
index cbd6bfa5..816d365d 100644
--- a/FitConnect/Interfaces/Sender/ISender.cs
+++ b/FitConnect/Interfaces/Sender/ISender.cs
@@ -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);
 }
diff --git a/FitConnect/Sender.cs b/FitConnect/Sender.cs
index 2ed34984..01a67d0e 100644
--- a/FitConnect/Sender.cs
+++ b/FitConnect/Sender.cs
@@ -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,
-- 
GitLab