@@ -8,123 +8,45 @@ import (
88 "github.com/dunglas/httpsfv"
99)
1010
11- type AcceptSignatureOption func (* AcceptSignatureBuilder ) error
12-
13- func WithExpectedKey (key Key ) AcceptSignatureOption {
14- return func (asb * AcceptSignatureBuilder ) error {
15- asb .keyID = key .KeyID
16- asb .keyAlgorithm = key .Algorithm
17-
18- return nil
19- }
20- }
21-
22- func WithExpectedNonce (ng NonceGetter ) AcceptSignatureOption {
23- return func (asb * AcceptSignatureBuilder ) error {
24- if ng != nil {
25- asb .nonceGetter = ng
26- }
27-
28- return nil
29- }
30- }
31-
32- func WithExpectedLabel (label string ) AcceptSignatureOption {
33- return func (asb * AcceptSignatureBuilder ) error {
34- if len (label ) != 0 {
35- asb .label = label
36- }
37-
38- return nil
39- }
40- }
41-
42- func WithExpectedComponents (identifiers ... string ) AcceptSignatureOption {
43- return func (asb * AcceptSignatureBuilder ) error {
44- identifiers , err := toComponentIdentifiers (identifiers )
45- if err != nil {
46- return err
47- }
48-
49- asb .setIdentifiers (identifiers )
50-
51- return nil
52- }
53- }
54-
55- func WithContentDigestAlgorithmPreferences (prefs ... AlgorithmPreference ) AcceptSignatureOption {
56- return func (asb * AcceptSignatureBuilder ) error {
57- if len (prefs ) == 0 {
58- return nil
59- }
60-
61- algPrefs := make ([]string , len (prefs ))
62-
63- for i , pref := range prefs {
64- if pref .Algorithm == "" {
65- return fmt .Errorf ("%w: digest algorithm preference requires a non-empty algorithm" , ErrParameter )
66- }
67-
68- algPrefs [i ] = pref .String ()
69- }
70-
71- asb .cdAlgPrefs = algPrefs
72-
73- return nil
74- }
75- }
76-
77- func WithExpectedTag (tag string ) AcceptSignatureOption {
78- return func (asb * AcceptSignatureBuilder ) error {
79- asb .tag = tag
80-
81- return nil
82- }
83- }
84-
85- func WithExpectedCreatedTimestamp (flag bool ) AcceptSignatureOption {
86- return func (asb * AcceptSignatureBuilder ) error {
87- asb .addCreatedTS = flag
88-
89- return nil
90- }
91- }
92-
93- func WithExpectedExpiresTimestamp (flag bool ) AcceptSignatureOption {
94- return func (asb * AcceptSignatureBuilder ) error {
95- asb .addExpiresTS = flag
96-
97- return nil
98- }
99- }
100-
11+ // AlgorithmPreference associates a Content-Digest algorithm with a numeric
12+ // preference value for the Want-Content-Digest header.
10113type AlgorithmPreference struct {
102- Algorithm DigestAlgorithm
14+ // Algorithm is the desired digest algorithm.
15+ Algorithm DigestAlgorithm
16+ // Preference is the preference value serialized alongside the algorithm.
10317 Preference int
10418}
10519
20+ // String formats the preference as "algorithm=preference".
10621func (p AlgorithmPreference ) String () string {
10722 return fmt .Sprintf ("%s=%d" , p .Algorithm , p .Preference )
10823}
10924
25+ // AcceptSignatureBuilder constructs an Accept-Signature request from the
26+ // configured parameters and component requirements. Use NewAcceptSignature
27+ // to create a builder with defaults, then call Build to add headers.
11028type AcceptSignatureBuilder struct {
111- keyAlgorithm SignatureAlgorithm
112- keyID string
113- nonceGetter NonceGetter
114- label string
115- identifiers []* componentIdentifier
116- cdAlgPrefs []string
117- tag string
118- addCreatedTS bool
119- addExpiresTS bool
120- wantContentDigest bool
29+ signatureAlgorithm SignatureAlgorithm
30+ keyID string
31+ nonceGetter NonceGetter
32+ label string
33+ identifiers []* componentIdentifier
34+ parameters []parameterOption
35+ cdAlgPrefs []string
36+ tag string
37+ addCreatedTS bool
38+ addExpiresTS bool
39+ wantContentDigest bool
12140}
12241
42+ // NewAcceptSignature creates an AcceptSignatureBuilder for requesting a
43+ // signature from a peer. By default, it uses label "sig", requests created
44+ // and expires parameters, and does not request a nonce. If Content-Digest
45+ // is requested, the default digest preferences are SHA-256 and SHA-512.
12346func NewAcceptSignature (opts ... AcceptSignatureOption ) (* AcceptSignatureBuilder , error ) {
12447 asb := & AcceptSignatureBuilder {
12548 addCreatedTS : true ,
12649 addExpiresTS : true ,
127- nonceGetter : nonceGetter {},
12850 label : "sig" ,
12951 cdAlgPrefs : []string {
13052 AlgorithmPreference {Algorithm : Sha256 , Preference : 5 }.String (), //nolint: mnd
@@ -133,18 +55,43 @@ func NewAcceptSignature(opts ...AcceptSignatureOption) (*AcceptSignatureBuilder,
13355 }
13456
13557 for _ , opt := range opts {
136- if err := opt (asb ); err != nil {
58+ if err := opt . applyAcceptSignatureOption (asb ); err != nil {
13759 return nil , err
13860 }
13961 }
14062
14163 return asb , nil
14264}
14365
144- func (asb * AcceptSignatureBuilder ) Build (ctx context.Context , header http.Header ) error {
145- nonce , err := asb .nonceGetter .GetNonce (ctx )
146- if err != nil {
147- return err
66+ // Build appends an Accept-Signature header to header using the configured
67+ // signature requirements. If Content-Digest is requested, it also appends
68+ // Want-Content-Digest preferences. Additional extension parameters can be
69+ // supplied with WithParameter. A non-empty nonce may be provided for this
70+ // call with WithParameter("nonce", value), overriding a configured
71+ // WithNonceGetter without invoking it. Otherwise, the configured getter is
72+ // used if present; if neither is provided, no nonce is requested. Options
73+ // do not modify the builder. The caller must provide a writable, non-nil
74+ // http.Header.
75+ func (asb * AcceptSignatureBuilder ) Build (
76+ ctx context.Context ,
77+ header http.Header ,
78+ opts ... AcceptSignatureBuildOption ,
79+ ) error {
80+ callOptions := acceptSignatureBuildOptions {staticParameters : asb .parameters }
81+ for _ , opt := range opts {
82+ if err := opt .applyAcceptSignatureBuildOption (& callOptions ); err != nil {
83+ return err
84+ }
85+ }
86+
87+ nonce := callOptions .nonce
88+ if ! callOptions .nonceSet && asb .nonceGetter != nil {
89+ var err error
90+
91+ nonce , err = asb .nonceGetter .GetNonce (ctx )
92+ if err != nil {
93+ return err
94+ }
14895 }
14996
15097 sigReqs := newSignatureRequirements (
@@ -153,9 +100,17 @@ func (asb *AcceptSignatureBuilder) Build(ctx context.Context, header http.Header
153100 asb .tag ,
154101 nonce ,
155102 asb .keyID ,
156- asb .keyAlgorithm ,
103+ asb .signatureAlgorithm ,
157104 asb .identifiers )
158105
106+ for _ , param := range asb .parameters {
107+ sigReqs .Params .Add (string (param .name ), param .value )
108+ }
109+
110+ for _ , param := range callOptions .parameters {
111+ sigReqs .Params .Add (string (param .name ), param .value )
112+ }
113+
159114 dict := httpsfv .NewDictionary ()
160115 dict .Add (asb .label , sigReqs .InnerList )
161116
0 commit comments