up
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
SDK Publish & Sign / sdk-publish (push) Has been cancelled
sdk-generator-smoke / sdk-smoke (push) Has been cancelled
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
SDK Publish & Sign / sdk-publish (push) Has been cancelled
sdk-generator-smoke / sdk-smoke (push) Has been cancelled
This commit is contained in:
73
src/Attestor/StellaOps.Attestation/DsseEnvelopeExtensions.cs
Normal file
73
src/Attestor/StellaOps.Attestation/DsseEnvelopeExtensions.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using StellaOps.Attestor.Envelope;
|
||||
|
||||
namespace StellaOps.Attestation;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting between <see cref="DsseEnvelope"/> domain types
|
||||
/// and API DTO representations.
|
||||
/// </summary>
|
||||
public static class DsseEnvelopeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DsseEnvelope"/> to a JSON-serializable dictionary
|
||||
/// suitable for API responses.
|
||||
/// </summary>
|
||||
public static Dictionary<string, object> ToSerializableDict(this DsseEnvelope envelope)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(envelope);
|
||||
|
||||
return new Dictionary<string, object>
|
||||
{
|
||||
["payloadType"] = envelope.PayloadType,
|
||||
["payload"] = Convert.ToBase64String(envelope.Payload.Span),
|
||||
["signatures"] = envelope.Signatures.Select(s => new Dictionary<string, object?>
|
||||
{
|
||||
["keyid"] = s.KeyId,
|
||||
["sig"] = s.Signature
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="DsseEnvelope"/> from base64-encoded payload and signature data.
|
||||
/// </summary>
|
||||
/// <param name="payloadType">The DSSE payload type URI.</param>
|
||||
/// <param name="payloadBase64">Base64-encoded payload bytes.</param>
|
||||
/// <param name="signatures">Collection of signature data as (keyId, signatureBase64) tuples.</param>
|
||||
/// <returns>A new <see cref="DsseEnvelope"/> instance.</returns>
|
||||
public static DsseEnvelope FromBase64(
|
||||
string payloadType,
|
||||
string payloadBase64,
|
||||
IEnumerable<(string? KeyId, string SignatureBase64)> signatures)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(payloadType);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(payloadBase64);
|
||||
ArgumentNullException.ThrowIfNull(signatures);
|
||||
|
||||
var payloadBytes = Convert.FromBase64String(payloadBase64);
|
||||
var dsseSignatures = signatures.Select(s => new DsseSignature(s.SignatureBase64, s.KeyId));
|
||||
|
||||
return new DsseEnvelope(payloadType, payloadBytes, dsseSignatures);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the payload as a UTF-8 string.
|
||||
/// </summary>
|
||||
public static string GetPayloadString(this DsseEnvelope envelope)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(envelope);
|
||||
return System.Text.Encoding.UTF8.GetString(envelope.Payload.Span);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the payload as a base64-encoded string.
|
||||
/// </summary>
|
||||
public static string GetPayloadBase64(this DsseEnvelope envelope)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(envelope);
|
||||
return Convert.ToBase64String(envelope.Payload.Span);
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ public static class DsseHelper
|
||||
var keyId = await signer.GetKeyIdAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var dsseSignature = DsseSignature.FromBytes(signatureBytes, keyId);
|
||||
return new DsseEnvelope(statement.Type, payloadBytes, new[] { dsseSignature });
|
||||
var payloadType = statement.Type ?? "https://in-toto.io/Statement/v1";
|
||||
return new DsseEnvelope(payloadType, payloadBytes, new[] { dsseSignature });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user