Skip to content

Commit

Permalink
refactor: sign.js PROCESS_RECIPIENT
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Aug 10, 2020
1 parent ce6836a commit e0a2d57
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions lib/jws/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,21 @@ class Sign {
recipient.header = unprotectedHeader
recipient.protected = Object.keys(joseHeader.protected).length ? base64url.JSON.encode(joseHeader.protected) : ''

let toBeSigned
if (joseHeader.protected.crit && joseHeader.protected.crit.includes('b64')) {
if (first && !joseHeader.protected.b64) {
if (this._binary) {
this._payload = base64url.decodeToBuffer(this._payload)
} else {
this._payload = base64url.decode(this._payload)
}
if (first && joseHeader.protected.crit && joseHeader.protected.crit.includes('b64') && joseHeader.protected.b64 === false) {
if (this._binary) {
this._payload = base64url.decodeToBuffer(this._payload)
} else {
this._payload = base64url.decode(this._payload)
}

toBeSigned = Buffer.concat([
Buffer.from(recipient.protected || ''),
Buffer.from('.'),
Buffer.isBuffer(this._payload) ? this._payload : Buffer.from(this._payload)
])
} else {
toBeSigned = `${recipient.protected || ''}.${this._payload}`
}

recipient.signature = base64url.encodeBuffer(sign(alg, key, toBeSigned))
const data = Buffer.concat([
Buffer.from(recipient.protected || ''),
Buffer.from('.'),
Buffer.from(this._payload)
])

recipient.signature = base64url.encodeBuffer(sign(alg, key, data))
}

/*
Expand Down

0 comments on commit e0a2d57

Please sign in to comment.