square / go-jose

An implementation of JOSE standards (JWE, JWS, JWT) in Go
1.98k stars 278 forks source link

Protected header in NewMultiEncrypter #374

Closed haggj closed 1 year ago

haggj commented 1 year ago

Dear go-jose Team,

I noticed that instantiating a NewMultiEncrypter does not recognize the extra headers specified in the passed EncrypterOptions. In the following code I would expect the protected header to contain the specified extra header sharedHeader. However, the resulting protected header only contains enc parameter:

var options jose.EncrypterOptions
options.WithHeader("sharedHeader", "some public data")

encrypter, err := jose.NewMultiEncrypter(jose.A256GCM, recipients, &options)
if err != nil {
    return "", err
}

jwe, err := encrypter.Encrypt([]byte("confidential data"))
if err != nil {
    return "", err
}
fmt.Println(jwe.FullSerialize())

A possible solution would simply set the extra header when instantiating a NewMultiEncrypter. Instead of:

...
if opts != nil {
    encrypter.compressionAlg = opts.Compression
}
...

This would work:

...
if opts != nil {
    encrypter.compressionAlg = opts.Compression
    encrypter.extraHeaders = opts.ExtraHeaders
}
...

Am I something missing here? I require this functionality to produce tokens which are compatible with the Python library jwcrypto (https://pypi.org/project/jwcrypto/) and the JS library jose(https://www.npmjs.com/package/jose).

Thanks for your help :-)