Skip to content

Commit

Permalink
Remove ctx from jws
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Oct 21, 2023
1 parent 193b1cd commit 4ed2585
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
13 changes: 6 additions & 7 deletions jws/headers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package jws

import (
"context"
"fmt"
)

func (h *stdHeaders) Copy(_ context.Context, dst Headers) error {
func (h *stdHeaders) Copy(dst Headers) error {
for _, k := range h.Keys() {
var v interface{}
if err := h.Get(k, &v); err != nil {
Expand All @@ -21,24 +20,24 @@ func (h *stdHeaders) Copy(_ context.Context, dst Headers) error {
// mergeHeaders merges two headers, and works even if the first Header
// object is nil. This is not exported because ATM it felt like this
// function is not frequently used, and MergeHeaders seemed a clunky name
func mergeHeaders(ctx context.Context, h1, h2 Headers) (Headers, error) {
func mergeHeaders(h1, h2 Headers) (Headers, error) {
h3 := NewHeaders()

if h1 != nil {
if err := h1.Copy(ctx, h3); err != nil {
if err := h1.Copy(h3); err != nil {
return nil, fmt.Errorf(`failed to copy headers from first Header: %w`, err)
}
}

if h2 != nil {
if err := h2.Copy(ctx, h3); err != nil {
if err := h2.Copy(h3); err != nil {
return nil, fmt.Errorf(`failed to copy headers from second Header: %w`, err)
}
}

return h3, nil
}

func (h *stdHeaders) Merge(ctx context.Context, h2 Headers) (Headers, error) {
return mergeHeaders(ctx, h, h2)
func (h *stdHeaders) Merge(h2 Headers) (Headers, error) {
return mergeHeaders(h, h2)
}
5 changes: 2 additions & 3 deletions jws/headers_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions jws/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jws

import (
"bytes"
"context"
"fmt"

"github.com/lestrrat-go/jwx/v3/internal/base64"
Expand Down Expand Up @@ -102,10 +101,7 @@ func (s *Signature) UnmarshalJSON(data []byte) error {
// The second return value s the full three-segment signature
// (e.g. "eyXXXX.XXXXX.XXXX")
func (s *Signature) Sign(payload []byte, signer Signer, key interface{}) ([]byte, []byte, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hdrs, err := mergeHeaders(ctx, s.headers, s.protected)
hdrs, err := mergeHeaders(s.headers, s.protected)
if err != nil {
return nil, nil, fmt.Errorf(`failed to merge headers: %w`, err)
}
Expand Down
4 changes: 2 additions & 2 deletions tools/cmd/genjws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func generateHeaders(obj *codegen.Object) error {
}
}

o.L("Copy(context.Context, Headers) error")
o.L("Merge(context.Context, Headers) (Headers, error)")
o.L("Copy(Headers) error")
o.L("Merge(Headers) (Headers, error)")

// These are used to access a single element by key name
o.L("// Get is used to extract the value of any field, including non-standard fields, out of the header.")
Expand Down

0 comments on commit 4ed2585

Please sign in to comment.