-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SDK-3808] Optionally sign the session store cookie #419
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fafa0e0
Move cookie signing code into separate file to make it easier to share
adamjmcgrath d1c3dc0
Sign the session store cookie based on config
adamjmcgrath 8604065
add test for missing branch
adamjmcgrath fb50938
Update index.d.ts
Widcket 984225d
Update index.d.ts
Widcket e589e01
Update lib/crypto.js
Widcket 41e4b58
Constants for crit and alg
adamjmcgrath 7b2f5c9
Merge remote-tracking branch 'origin/signed-store-cookie' into signed…
adamjmcgrath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
const crypto = require('crypto'); | ||
const { JWKS, JWK, JWS } = require('jose'); | ||
|
||
const BYTE_LENGTH = 32; | ||
const ENCRYPTION_INFO = 'JWE CEK'; | ||
const SIGNING_INFO = 'JWS Cookie Signing'; | ||
const DIGEST = 'sha256'; | ||
|
||
let encryption, signing; | ||
|
||
/** | ||
* | ||
* Derives appropriate sized keys from the end-user provided secret random string/passphrase using | ||
* HKDF (HMAC-based Extract-and-Expand Key Derivation Function) defined in RFC 8569. | ||
* | ||
* @see https://tools.ietf.org/html/rfc5869 | ||
* | ||
*/ | ||
if (crypto.hkdfSync) { | ||
// added in v15.0.0 | ||
encryption = (secret) => | ||
Buffer.from( | ||
crypto.hkdfSync( | ||
DIGEST, | ||
secret, | ||
Buffer.alloc(0), | ||
ENCRYPTION_INFO, | ||
BYTE_LENGTH | ||
) | ||
); | ||
signing = (secret) => | ||
Buffer.from( | ||
crypto.hkdfSync( | ||
DIGEST, | ||
secret, | ||
Buffer.alloc(0), | ||
SIGNING_INFO, | ||
BYTE_LENGTH | ||
) | ||
); | ||
} else { | ||
const hkdf = require('futoin-hkdf'); | ||
encryption = (secret) => | ||
hkdf(secret, BYTE_LENGTH, { info: ENCRYPTION_INFO, hash: DIGEST }); | ||
signing = (secret) => | ||
hkdf(secret, BYTE_LENGTH, { info: SIGNING_INFO, hash: DIGEST }); | ||
} | ||
|
||
const getKeyStore = (secret, forEncryption) => { | ||
let current; | ||
const secrets = Array.isArray(secret) ? secret : [secret]; | ||
let keystore = new JWKS.KeyStore(); | ||
secrets.forEach((secretString, i) => { | ||
const key = JWK.asKey( | ||
forEncryption ? encryption(secretString) : signing(secretString) | ||
); | ||
if (i === 0) { | ||
current = key; | ||
} | ||
keystore.add(key); | ||
}); | ||
return [current, keystore]; | ||
}; | ||
|
||
const header = { alg: 'HS256', b64: false, crit: ['b64'] }; | ||
|
||
const getPayload = (cookie, value) => Buffer.from(`${cookie}=${value}`); | ||
const flattenedJWSFromCookie = (cookie, value, signature) => ({ | ||
protected: Buffer.from(JSON.stringify(header)) | ||
.toString('base64') | ||
.replace(/=/g, '') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_'), | ||
payload: getPayload(cookie, value), | ||
signature, | ||
}); | ||
const generateSignature = (cookie, value, key) => { | ||
const payload = getPayload(cookie, value); | ||
return JWS.sign.flattened(payload, key, header).signature; | ||
}; | ||
const verifySignature = (cookie, value, signature, keystore) => { | ||
try { | ||
return !!JWS.verify( | ||
flattenedJWSFromCookie(cookie, value, signature), | ||
keystore, | ||
{ algorithm: 'HS256', crit: ['b64'] } | ||
); | ||
} catch (err) { | ||
return false; | ||
} | ||
}; | ||
const verifyCookie = (cookie, value, keystore) => { | ||
if (!value) { | ||
return undefined; | ||
} | ||
let signature; | ||
[value, signature] = value.split('.'); | ||
if (verifySignature(cookie, value, signature, keystore)) { | ||
return value; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
const signCookie = (cookie, value, key) => { | ||
const signature = generateSignature(cookie, value, key); | ||
return `${value}.${signature}`; | ||
}; | ||
|
||
module.exports.signCookie = signCookie; | ||
module.exports.verifyCookie = verifyCookie; | ||
|
||
module.exports.getKeyStore = getKeyStore; | ||
module.exports.encryption = encryption; | ||
module.exports.signing = signing; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any way to not duplicate these values that are already used in the
header
constant?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 have moved these into constants
Also, noticed a typo - thanks