Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend mottatmelding basert på pullrequest fra ErHaWi #72

Merged
merged 4 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions KS.Fiks.IO.Client/Asic/AsicDecrypter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using KS.Fiks.ASiC_E;
using KS.Fiks.Crypto;
using KS.Fiks.IO.Client.Exceptions;
using KS.Fiks.IO.Client.Models;

namespace KS.Fiks.IO.Client.Asic
{
Expand Down Expand Up @@ -43,5 +47,32 @@ public async Task<Stream> Decrypt(Task<Stream> encryptedZipStream)
throw new FiksIODecryptionException("Unable to decrypt melding. Is your private key correct?", ex);
}
}

public async Task<IEnumerable<IPayload>> DecryptAndExtractPayloads(Task<Stream> encryptedZipStream)
{
var payloads = new List<StreamPayload>();

using (var stream = await Decrypt(encryptedZipStream).ConfigureAwait(false))
{
var asiceReader = new AsiceReader().Read(stream);

foreach (var entry in asiceReader.Entries)
{
using (var entryStream = entry.OpenStream())
{

var memoryStream = new MemoryStream();

await entryStream.CopyToAsync(memoryStream).ConfigureAwait(false);

var payload = new StreamPayload(memoryStream, entry.FileName);

payloads.Add(payload);
}
}
}

return payloads;
}
}
}
5 changes: 5 additions & 0 deletions KS.Fiks.IO.Client/Asic/IAsicDecrypter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using KS.Fiks.IO.Client.Models;

namespace KS.Fiks.IO.Client.Asic
{
Expand All @@ -8,5 +11,7 @@ public interface IAsicDecrypter
Task WriteDecrypted(Task<Stream> encryptedZipStream, string outPath);

Task<Stream> Decrypt(Task<Stream> encryptedZipStream);

Task<IEnumerable<IPayload>> DecryptAndExtractPayloads(Task<Stream> encryptedZipStream);
}
}
7 changes: 5 additions & 2 deletions KS.Fiks.IO.Client/Models/IMottattMelding.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

Expand All @@ -10,12 +11,14 @@ public interface IMottattMelding : IMelding

Guid? SvarPaMelding { get; }

Task<Stream> EncryptedStream { get; }
Task<Stream> EncryptedStream { get; }

Task<Stream> DecryptedStream { get; }
Task<Stream> DecryptedStream { get; }

Task WriteEncryptedZip(string outPath);

Task WriteDecryptedZip(string outPath);

Task<IEnumerable<IPayload>> DecryptedPayloads { get; }
}
}
6 changes: 6 additions & 0 deletions KS.Fiks.IO.Client/Models/MottattMelding.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using KS.Fiks.ASiC_E;
using KS.Fiks.ASiC_E.Model;
using KS.Fiks.IO.Client.Asic;
using KS.Fiks.IO.Client.FileIO;

Expand All @@ -11,6 +14,7 @@ public class MottattMelding : MottattMeldingMetadata, IMottattMelding
private readonly Func<Task<Stream>> _streamProvider;
private readonly IAsicDecrypter _decrypter;
private readonly IFileWriter _fileWriter;
private IEnumerable<IPayload> _payloads;

internal MottattMelding(
bool hasPayload,
Expand Down Expand Up @@ -41,5 +45,7 @@ public async Task WriteDecryptedZip(string outPath)
{
await _decrypter.WriteDecrypted(_streamProvider(), outPath).ConfigureAwait(false);
}

public Task<IEnumerable<IPayload>> DecryptedPayloads => _decrypter.DecryptAndExtractPayloads(_streamProvider());
}
}