Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
226380d
api updated and documented
dadrus Oct 29, 2026
b699c8b
readme updated
dadrus Oct 29, 2026
5dc6d4c
compiling regex only once
dadrus Oct 29, 2026
43620aa
reducing allocs while creating ecdsa signature
dadrus Oct 29, 2026
a7ff4ca
not cloning []byte params any more
dadrus Oct 29, 2026
2f621b1
made signature base usage more efficient
dadrus Oct 29, 2026
04c19d9
new WithAlg option
dadrus Oct 29, 2026
6c6635b
not allowing empty tag for the WithRequiredTag option
dadrus Oct 29, 2026
2ec2d85
timestamp related TODOs removed
dadrus Oct 31, 2026
59815af
options refactored
dadrus Nov 2, 2026
45673c0
NonceChecker and the corresponding option removed. Both are obsolete …
dadrus Nov 2, 2026
d1b3349
golangci settings pdated
dadrus Nov 2, 2026
6f7c6a5
options consolidated
dadrus Nov 2, 2026
52aaf2b
memoization of url query parameters
dadrus Nov 3, 2026
8e270a0
precalculating components for signature base
dadrus Nov 3, 2026
bd64af5
using known params length while creating the corresponding map
dadrus Nov 3, 2026
b0c9d2e
linter warnings resolved
dadrus Nov 3, 2026
0e436f6
asserting metadataoptions independent from key algorithm and more opt…
dadrus Nov 3, 2026
b18422a
signature and verification internals updated/simplified
dadrus Nov 3, 2026
5a6a846
expectations renamed to verificationPolicy; propagating configuration…
dadrus Nov 4, 2026
07c8ef8
WithExpiredTimestamp renamed to WithExpiresTimestamp
dadrus Nov 4, 2026
1211e60
nil pointer fix for signature parameters
dadrus Nov 4, 2026
b287809
avoid panic on malformed Signature in addSignature
dadrus Nov 4, 2026
bc52857
better test coverage for signatureInput.parameters
dadrus Nov 4, 2026
f7ea980
better test coverage for message.addSignature
dadrus Nov 4, 2026
68f8f8d
small test restructuring
dadrus Nov 4, 2026
7e2d842
further test reformatting
dadrus Nov 4, 2026
4c3749d
type-safe signature parameter access
dadrus Nov 4, 2026
c04ed97
further api consolitdation
dadrus Nov 4, 2026
ca76d42
code formatting
dadrus Nov 4, 2026
06e11cf
readme updated
dadrus Nov 4, 2026
c63ee96
Comment extended to cover the order of the verified signatures
dadrus Nov 4, 2026
3e301ea
further readme updates
dadrus Nov 4, 2026
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
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ linters:
# deprecated
- wsl
- depguard
- gomodguard
settings:
cyclop:
max-complexity: 11
Expand Down Expand Up @@ -75,6 +76,7 @@ linters:
- mnd
- goconst
- varnamelen
- gocyclo
path: (.+)_test\.go

formatters:
Expand Down
1 change: 0 additions & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ packages:
github.com/dadrus/httpsig:
interfaces:
NonceGetter: {}
NonceChecker: {}
KeyResolver: {}
408 changes: 337 additions & 71 deletions README.md

Large diffs are not rendered by default.

171 changes: 63 additions & 108 deletions accept_signature_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,123 +8,45 @@ import (
"github.com/dunglas/httpsfv"
)

type AcceptSignatureOption func(*AcceptSignatureBuilder) error

func WithExpectedKey(key Key) AcceptSignatureOption {
return func(asb *AcceptSignatureBuilder) error {
asb.keyID = key.KeyID
asb.keyAlgorithm = key.Algorithm

return nil
}
}

func WithExpectedNonce(ng NonceGetter) AcceptSignatureOption {
return func(asb *AcceptSignatureBuilder) error {
if ng != nil {
asb.nonceGetter = ng
}

return nil
}
}

func WithExpectedLabel(label string) AcceptSignatureOption {
return func(asb *AcceptSignatureBuilder) error {
if len(label) != 0 {
asb.label = label
}

return nil
}
}

func WithExpectedComponents(identifiers ...string) AcceptSignatureOption {
return func(asb *AcceptSignatureBuilder) error {
identifiers, err := toComponentIdentifiers(identifiers)
if err != nil {
return err
}

asb.setIdentifiers(identifiers)

return nil
}
}

func WithContentDigestAlgorithmPreferences(prefs ...AlgorithmPreference) AcceptSignatureOption {
return func(asb *AcceptSignatureBuilder) error {
if len(prefs) == 0 {
return nil
}

algPrefs := make([]string, len(prefs))

for i, pref := range prefs {
if pref.Algorithm == "" {
return fmt.Errorf("%w: digest algorithm preference requires a non-empty algorithm", ErrParameter)
}

algPrefs[i] = pref.String()
}

asb.cdAlgPrefs = algPrefs

return nil
}
}

func WithExpectedTag(tag string) AcceptSignatureOption {
return func(asb *AcceptSignatureBuilder) error {
asb.tag = tag

return nil
}
}

func WithExpectedCreatedTimestamp(flag bool) AcceptSignatureOption {
return func(asb *AcceptSignatureBuilder) error {
asb.addCreatedTS = flag

return nil
}
}

func WithExpectedExpiresTimestamp(flag bool) AcceptSignatureOption {
return func(asb *AcceptSignatureBuilder) error {
asb.addExpiresTS = flag

return nil
}
}

// AlgorithmPreference associates a Content-Digest algorithm with a numeric
// preference value for the Want-Content-Digest header.
type AlgorithmPreference struct {
Algorithm DigestAlgorithm
// Algorithm is the desired digest algorithm.
Algorithm DigestAlgorithm
// Preference is the preference value serialized alongside the algorithm.
Preference int
}

// String formats the preference as "algorithm=preference".
func (p AlgorithmPreference) String() string {
return fmt.Sprintf("%s=%d", p.Algorithm, p.Preference)
}

// AcceptSignatureBuilder constructs an Accept-Signature request from the
// configured parameters and component requirements. Use NewAcceptSignature
// to create a builder with defaults, then call Build to add headers.
type AcceptSignatureBuilder struct {
keyAlgorithm SignatureAlgorithm
keyID string
nonceGetter NonceGetter
label string
identifiers []*componentIdentifier
cdAlgPrefs []string
tag string
addCreatedTS bool
addExpiresTS bool
wantContentDigest bool
signatureAlgorithm SignatureAlgorithm
keyID string
nonceGetter NonceGetter
label string
identifiers []*componentIdentifier
parameters []parameterOption
cdAlgPrefs []string
tag string
addCreatedTS bool
addExpiresTS bool
wantContentDigest bool
}

// NewAcceptSignature creates an AcceptSignatureBuilder for requesting a
// signature from a peer. By default, it uses label "sig", requests created
// and expires parameters, and does not request a nonce. If Content-Digest
// is requested, the default digest preferences are SHA-256 and SHA-512.
func NewAcceptSignature(opts ...AcceptSignatureOption) (*AcceptSignatureBuilder, error) {
asb := &AcceptSignatureBuilder{
addCreatedTS: true,
addExpiresTS: true,
nonceGetter: nonceGetter{},
label: "sig",
cdAlgPrefs: []string{
AlgorithmPreference{Algorithm: Sha256, Preference: 5}.String(), //nolint: mnd
Expand All @@ -133,18 +55,43 @@ func NewAcceptSignature(opts ...AcceptSignatureOption) (*AcceptSignatureBuilder,
}

for _, opt := range opts {
if err := opt(asb); err != nil {
if err := opt.applyAcceptSignatureOption(asb); err != nil {
return nil, err
}
}

return asb, nil
}

func (asb *AcceptSignatureBuilder) Build(ctx context.Context, header http.Header) error {
nonce, err := asb.nonceGetter.GetNonce(ctx)
if err != nil {
return err
// Build appends an Accept-Signature header to header using the configured
// signature requirements. If Content-Digest is requested, it also appends
// Want-Content-Digest preferences. Additional extension parameters can be
// supplied with WithParameter. A non-empty nonce may be provided for this
// call with WithParameter("nonce", value), overriding a configured
// WithNonceGetter without invoking it. Otherwise, the configured getter is
// used if present; if neither is provided, no nonce is requested. Options
// do not modify the builder. The caller must provide a writable, non-nil
// http.Header.
func (asb *AcceptSignatureBuilder) Build(
ctx context.Context,
header http.Header,
opts ...AcceptSignatureBuildOption,
) error {
callOptions := acceptSignatureBuildOptions{staticParameters: asb.parameters}
for _, opt := range opts {
if err := opt.applyAcceptSignatureBuildOption(&callOptions); err != nil {
return err
}
}

nonce := callOptions.nonce
if !callOptions.nonceSet && asb.nonceGetter != nil {
var err error

nonce, err = asb.nonceGetter.GetNonce(ctx)
if err != nil {
return err
}
}

sigReqs := newSignatureRequirements(
Expand All @@ -153,9 +100,17 @@ func (asb *AcceptSignatureBuilder) Build(ctx context.Context, header http.Header
asb.tag,
nonce,
asb.keyID,
asb.keyAlgorithm,
asb.signatureAlgorithm,
asb.identifiers)

for _, param := range asb.parameters {
sigReqs.Params.Add(string(param.name), param.value)
}

for _, param := range callOptions.parameters {
sigReqs.Params.Add(string(param.name), param.value)
}

dict := httpsfv.NewDictionary()
dict.Add(asb.label, sigReqs.InnerList)

Expand Down
Loading
Loading