Skip to content

Commit aeed528

Browse files
authored
fix: NonceChecker adapter function fixed (#118)
1 parent 762a89c commit aeed528

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Both the `Signer` and `Verifier` respect the `"content-digest"` component identi
109109
>
110110
> For server-side request handling, consider using `http.MaxBytesReader`; for other contexts, use `io.LimitedReader` or an equivalent mechanism. The library restores the body after reading it, so it can still be consumed by subsequent handlers, but it does not impose global body size limits on behalf of the application.
111111
112-
* On the `Verifier` side, verification of the corresponding hash values is done by default with no additional configuration required. If the `"Signature-Input"` header value contains a `"content-digest"` component, the implementation expects the `"Content-Digest"` header to be present and uses the supplied algorithm names and values to calculate the digest over the body and compare these value to the received ones. If the `"Content-Digest"` header is missing, references unsupported hash algorithms (only `sha-256` and `sha-512` are supported), or there is a mismatch between the calculated and provided values, the message verification will fail with an error.
112+
* On the `Verifier` side, verification of the corresponding hash values is done by default with no additional configuration required. If the `"Signature-Input"` header value contains a `"content-digest"` component, the implementation expects the `"Content-Digest"` header to be present and uses the supplied algorithm names and values to calculate the digest over the body and compare these values to the received ones. If the `"Content-Digest"` header is missing, references unsupported hash algorithms (only `sha-256` and `sha-512` are supported), or there is a mismatch between the calculated and provided values, the message verification will fail with an error.
113113

114114
## Signature Negotiation
115115

nonce.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (ng NonceGetterFunc) GetNonce(ctx context.Context) (string, error) { return
2020
//go:generate mockery --name NonceChecker --structname NonceCheckerMock --inpackage --testonly
2121

2222
// NonceChecker is responsible for the verification of the nonce received in a signature,
23-
// e.g. to prevent replay attacks, or to verify that the nonce is the expected one, like
23+
// e.g. to prevent replay attacks, or to verify that the nonce is the expected one, like
2424
// if requested using the Accept-Signature header.
2525
type NonceChecker interface {
2626
CheckNonce(ctx context.Context, nonce NonceValue) error
@@ -31,9 +31,11 @@ type NonceValue struct {
3131
Value string
3232
}
3333

34-
type NonceCheckerFunc func(ctx context.Context, nonce string) error
34+
type NonceCheckerFunc func(ctx context.Context, nonce NonceValue) error
3535

36-
func (nc NonceCheckerFunc) GetNonce(ctx context.Context, nonce string) error { return nc(ctx, nonce) }
36+
func (f NonceCheckerFunc) CheckNonce(ctx context.Context, nonce NonceValue) error {
37+
return f(ctx, nonce)
38+
}
3739

3840
type noopNonceChecker struct{}
3941

nonce_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package httpsig
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestNonceCheckerFuncImplementsNonceChecker(t *testing.T) {
12+
t.Parallel()
13+
14+
var checker NonceChecker = NonceCheckerFunc(func(_ context.Context, nonce NonceValue) error {
15+
assert.True(t, nonce.Present)
16+
assert.Equal(t, "foo", nonce.Value)
17+
18+
return nil
19+
})
20+
21+
err := checker.CheckNonce(context.Background(), NonceValue{Present: true, Value: "foo"})
22+
require.NoError(t, err)
23+
}

0 commit comments

Comments
 (0)