remove headers if recipients = 1; copy protected headers to headers f…#83
remove headers if recipients = 1; copy protected headers to headers f…#83ilya-korotya wants to merge 2 commits into
Conversation
…or jwe flatted message
There was a problem hiding this comment.
Pull Request Overview
This PR modifies the AnoncryptPacker to handle JWE (JSON Web Encryption) messages with single recipients by removing headers and copying protected headers to headers for flattened JWE format.
- Removes headers from JWE messages when there's only one recipient
- Adds logic to copy protected headers to headers field for flattened JWE messages during unpacking
- Updates tests to use proper constructor and verification methods
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packers/anoncrypt.go | Implements header removal for single-recipient messages and protected header copying logic |
| packers/anoncrypt_test.go | Updates test to use constructor, changes verification approach, and comments out second recipient |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if len(recipients) == 1 { | ||
| j := map[string]interface{}{} | ||
| if err := json.Unmarshal(ret, &j); err != nil { | ||
| return nil, errors.Wrap(err, "failed to unmarshal jwe message") | ||
| } | ||
| delete(j, "header") | ||
| ret, err = json.Marshal(j) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to marshal jwe message") | ||
| } | ||
| } |
There was a problem hiding this comment.
[nitpick] The variable name j is ambiguous and doesn't convey its purpose. Consider renaming it to jweMap or jweObject to better reflect that it represents the JWE message structure.
| func (p *AnoncryptPacker) Unpack(envelope []byte) (*iden3comm.BasicMessage, error) { | ||
| firstChar := strings.TrimSpace(string(envelope))[0] | ||
| if firstChar == '{' { | ||
| var j map[string]interface{} |
There was a problem hiding this comment.
[nitpick] The variable name j is ambiguous and doesn't convey its purpose. Consider renaming it to jweMap or jweObject to better reflect that it represents the JWE message structure.
| return nil, errors.New("invalid jwe token: missing protected header") | ||
| } | ||
|
|
||
| protectedHeaders, err := base64.RawURLEncoding.DecodeString(j["protected"].(string)) |
There was a problem hiding this comment.
Type assertion j["protected"].(string) could panic if the value is not a string. Add a type check or use the two-value form of type assertion to handle this safely.
| protectedHeaders, err := base64.RawURLEncoding.DecodeString(j["protected"].(string)) | |
| protectedStr, ok := j["protected"].(string) | |
| if !ok { | |
| return nil, errors.New("invalid jwe token: 'protected' field is not a string") | |
| } | |
| protectedHeaders, err := base64.RawURLEncoding.DecodeString(protectedStr) |
…or jwe flatted message