Skip to content

Commit

Permalink
allow passing context.Contex to jwe.Decrypt (#1222)
Browse files Browse the repository at this point in the history
* allow passing context.Contex to jwe.Decrypt

* really, this is OK

---------

Co-authored-by: Daisuke Maki <lestrrat+github@users.noreplay.github.com>
  • Loading branch information
lestrrat and Daisuke Maki authored Oct 26, 2024
1 parent 4769e28 commit c1658b3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions jwe/jwe.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ func decrypt(buf []byte, options ...DecryptOption) ([]byte, error) {
var cek *[]byte
var dst *Message
perCallMaxDecompressBufferSize := maxDecompressBufferSize
ctx := context.Background()
//nolint:forcetypeassert
for _, option := range options {
switch option.Ident() {
Expand All @@ -565,6 +566,9 @@ func decrypt(buf []byte, options ...DecryptOption) ([]byte, error) {
cek = option.Value().(*[]byte)
case identMaxDecompressBufferSize{}:
perCallMaxDecompressBufferSize = option.Value().(int64)
case identContext{}:
//nolint:fatcontext
ctx = option.Value().(context.Context)
}
}

Expand Down Expand Up @@ -625,8 +629,6 @@ func decrypt(buf []byte, options ...DecryptOption) ([]byte, error) {
dctx.cek = cek
dctx.maxDecompressBufferSize = perCallMaxDecompressBufferSize

ctx := context.TODO()

errs := make([]error, 0, len(recipients))
for _, recipient := range recipients {
decrypted, err := dctx.try(ctx, recipient, keyUsed)
Expand Down
6 changes: 6 additions & 0 deletions jwe/options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ options:
- ident: KeyProvider
interface: DecryptOption
argument_type: KeyProvider
- ident: Context
interface: DecryptOption
argument_type: context.Context
comment: |
WithContext specifies the context.Context object to use when decrypting a JWE message.
If not provided, context.Background() will be used.
- ident: Serialization
option_name: WithCompact
interface: EncryptOption
Expand Down
12 changes: 12 additions & 0 deletions jwe/options_gen.go

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

1 change: 1 addition & 0 deletions jwe/options_gen_test.go

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

2 changes: 1 addition & 1 deletion tools/cmd/genoptions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func _main() error {
})

if err := genOptions(&objects); err != nil {
return fmt.Errorf(`failed to generate %q`, objects.Output)
return fmt.Errorf(`failed to generate %q: %w`, objects.Output, err)
}

if err := genOptionTests(&objects); err != nil {
Expand Down

0 comments on commit c1658b3

Please sign in to comment.