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

Getting destination ID missing

parent cdbbba50
No related branches found
No related tags found
1 merge request!9.NET-SDK: SET-Empfang inkl. Signaturprüfung - Ticket 562
...@@ -155,7 +155,7 @@ internal class SubmissionService : RestCallService, ISubmissionService { ...@@ -155,7 +155,7 @@ internal class SubmissionService : RestCallService, ISubmissionService {
// Download well known keys // Download well known keys
var valid = await ValidateSignature(events); var valid = await ValidateSignature(events);
// TODO: Check JSON Schema // TODO: Check JSON Schema
valid &= await ValidateSchema(events, false); // valid &= await ValidateSchema(events, false);
if (!valid) { if (!valid) {
_logger?.LogError("Invalid SET, signature can not be verified"); _logger?.LogError("Invalid SET, signature can not be verified");
...@@ -226,18 +226,12 @@ internal class SubmissionService : RestCallService, ISubmissionService { ...@@ -226,18 +226,12 @@ internal class SubmissionService : RestCallService, ISubmissionService {
return valid; return valid;
} }
private async Task<bool> ValidateSignature(EventLogDto? events) { private async Task<bool> ValidateSignature(EventLogDto events) {
// Load Key from GET {{submission_api_url}}/v1/destinations/{{destinationId}}/keys/{{keyId}} var keys = await GetJsonWebKeysForEvent(events);
var keyIds = GetKeyIdsFromEvent(events);
var keySet = new JsonWebKeySet(await Router.GetSubmissionServiceValidationJwk(_baseUrl)); var valid = events.EventLog?.Aggregate(true,
var keys = _signatureValidationKey == null (current, eventJson) =>
? keySet.Keys current & FitEncryption.VerifyJwt(eventJson, keys, logger: _logger)) ?? true;
: keySet.Keys.Append(_signatureValidationKey);
var valid = true;
foreach (var eventJson in events.EventLog) {
valid &= FitEncryption.VerifyJwt(eventJson, keys, logger: _logger);
}
if (!valid) { if (!valid) {
_logger?.LogDebug("Signature is invalid"); _logger?.LogDebug("Signature is invalid");
...@@ -248,8 +242,49 @@ internal class SubmissionService : RestCallService, ISubmissionService { ...@@ -248,8 +242,49 @@ internal class SubmissionService : RestCallService, ISubmissionService {
return valid; return valid;
} }
private IEnumerable<JsonWebKey> GetKeyIdsFromEvent(EventLogDto events) { private async Task<IEnumerable<JsonWebKey>> GetJsonWebKeysForEvent(EventLogDto events) {
return new List<JsonWebKey>(); var keySet = new JsonWebKeySet(await Router.GetSubmissionServiceValidationJwk(_baseUrl));
var keys = _signatureValidationKey == null
? keySet.Keys
: keySet.Keys.Append(_signatureValidationKey);
return (await GetKeyIdsFromEvent(events)).Union(keys);
}
private async Task<IEnumerable<JsonWebKey>> GetKeyIdsFromEvent(EventLogDto events) {
if (events.EventLog == null)
return new List<JsonWebKey>();
// Load Key from GET {{submission_api_url}}/v1/destinations/{{destinationId}}/keys/{{keyId}}
var keyIds = events.EventLog.Select(ExtractSubmissionIdFromEvent).ToList();
var result = new List<JsonWebKey>();
foreach (var (submission, keyId) in keyIds) {
try {
// TODO Get destinationId from submission
var destinationId = "aa3704d6-8bd7-4d40-a8af-501851f93934";
var keyJson = await RestCallForString($"/destinations/{destinationId}/keys/{keyId}",
HttpMethod.Get);
result.Add(new JsonWebKey(keyJson));
}
catch (Exception e) {
_logger?.LogWarning(e, "Error loading key {KeyId}", keyId);
}
}
return result;
}
private (string submissionId, string keyId) ExtractSubmissionIdFromEvent(string events) {
var jwtParts = events.Split('.').Select(Base64UrlEncoder.Decode).ToList();
var header = JsonConvert.DeserializeObject<Dictionary<string, object>>(jwtParts[0]);
var payload = JsonConvert.DeserializeObject<Dictionary<string, object>>(jwtParts[1]);
var keyId = (string)header["kid"];
var submissionId = ((string)payload["sub"]).Split(':')[1];
return (submissionId, keyId);
} }
public async Task GetValidationJwk() { public async Task GetValidationJwk() {
......
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