|
| 1 | +package httpsig |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "slices" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/dunglas/httpsfv" |
| 9 | +) |
| 10 | + |
| 11 | +type signatureInput struct { |
| 12 | + value httpsfv.InnerList |
| 13 | + identifiers []*componentIdentifier |
| 14 | +} |
| 15 | + |
| 16 | +func newSignatureInput( |
| 17 | + identifiers []*componentIdentifier, |
| 18 | + params *httpsfv.Params, |
| 19 | +) *signatureInput { |
| 20 | + // https://www.rfc-editor.org/rfc/rfc9421.html#name-signature-parameters |
| 21 | + if params == nil { |
| 22 | + params = httpsfv.NewParams() |
| 23 | + } |
| 24 | + |
| 25 | + items := make([]httpsfv.Item, len(identifiers)) |
| 26 | + for i, id := range identifiers { |
| 27 | + items[i] = id.Item |
| 28 | + } |
| 29 | + |
| 30 | + return &signatureInput{ |
| 31 | + value: httpsfv.InnerList{ |
| 32 | + Items: items, |
| 33 | + Params: params, |
| 34 | + }, |
| 35 | + identifiers: identifiers, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func parseSignatureInput(list httpsfv.InnerList) (*signatureInput, error) { |
| 40 | + identifiers := make([]*componentIdentifier, 0, len(list.Items)) |
| 41 | + |
| 42 | + for _, item := range list.Items { |
| 43 | + identifier, err := newComponentIdentifier(item) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + identifiers = append(identifiers, identifier) |
| 49 | + } |
| 50 | + |
| 51 | + if err := validateSignatureInputParameters(list.Params); err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + return &signatureInput{ |
| 56 | + value: list, |
| 57 | + identifiers: identifiers, |
| 58 | + }, nil |
| 59 | +} |
| 60 | + |
| 61 | +func validateSignatureInputParameters(params *httpsfv.Params) error { |
| 62 | + if params == nil { |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + for _, name := range params.Names() { |
| 67 | + value, _ := params.Get(name) |
| 68 | + |
| 69 | + switch SignatureParameter(name) { |
| 70 | + case Created, Expires: |
| 71 | + if _, ok := value.(int64); !ok { |
| 72 | + return fmt.Errorf("%w: %s", ErrMalformedSignatureParameter, name) |
| 73 | + } |
| 74 | + case KeyID, Alg, Nonce, Tag: |
| 75 | + if _, ok := value.(string); !ok { |
| 76 | + return fmt.Errorf("%w: %s", ErrMalformedSignatureParameter, name) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func (si *signatureInput) hasIdentifier(id *componentIdentifier) bool { |
| 85 | + for _, identifier := range si.identifiers { |
| 86 | + if identifier.Value == id.Value && slices.Equal(identifier.Params.Names(), id.Params.Names()) { |
| 87 | + return true |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return false |
| 92 | +} |
| 93 | + |
| 94 | +func (si *signatureInput) parameter(name SignatureParameter) (any, bool) { |
| 95 | + if si.value.Params == nil { |
| 96 | + return nil, false |
| 97 | + } |
| 98 | + |
| 99 | + return si.value.Params.Get(string(name)) |
| 100 | +} |
| 101 | + |
| 102 | +func (si *signatureInput) stringParameter(name SignatureParameter) (string, bool) { |
| 103 | + value, present := si.parameter(name) |
| 104 | + if !present { |
| 105 | + return "", false |
| 106 | + } |
| 107 | + |
| 108 | + return value.(string), true //nolint: forcetypeassert |
| 109 | +} |
| 110 | + |
| 111 | +func (si *signatureInput) timestamp(name SignatureParameter) (time.Time, bool) { |
| 112 | + value, present := si.parameter(name) |
| 113 | + if !present { |
| 114 | + return time.Time{}, false |
| 115 | + } |
| 116 | + |
| 117 | + return time.Unix(value.(int64), 0).UTC(), true //nolint: forcetypeassert |
| 118 | +} |
| 119 | + |
| 120 | +func (si *signatureInput) algorithm() (SignatureAlgorithm, bool) { |
| 121 | + value, present := si.stringParameter(Alg) |
| 122 | + |
| 123 | + return SignatureAlgorithm(value), present |
| 124 | +} |
| 125 | + |
| 126 | +func (si *signatureInput) created() (time.Time, bool) { return si.timestamp(Created) } |
| 127 | +func (si *signatureInput) expires() (time.Time, bool) { return si.timestamp(Expires) } |
| 128 | +func (si *signatureInput) nonce() (string, bool) { return si.stringParameter(Nonce) } |
| 129 | +func (si *signatureInput) keyID() (string, bool) { return si.stringParameter(KeyID) } |
| 130 | +func (si *signatureInput) tag() (string, bool) { return si.stringParameter(Tag) } |
0 commit comments