-
Notifications
You must be signed in to change notification settings - Fork 29
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
chore(deps): update dependency jose to v5 - abandoned #3540
base: main
Are you sure you want to change the base?
Changes from 6 commits
81ce5d0
3955bc1
2fa5b8c
5bf99e5
e5d049c
9f6151d
122a07f
b928a14
3429819
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,49 @@ | ||
import { JWT, JWK, JWKS } from 'jose'; | ||
import { | ||
exportJWK, | ||
generateKeyPair, | ||
type KeyLike, | ||
SignJWT, | ||
type JWK, | ||
} from 'jose'; | ||
|
||
const keyRS256 = JWK.generateSync('RSA', 2048, { use: 'sig', alg: 'RS256' }); | ||
let keyRS256: KeyLike; | ||
let jwksStore: { keys: JWK[] }; | ||
|
||
const jwksStore = new JWKS.KeyStore([keyRS256]); | ||
async function initialize() { | ||
// Generate RSA key pair with 2048 bits for the RS256 algorithm | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also check how we initialize this in our MC services ( Anyway, if it does the job all good. |
||
const { publicKey, privateKey } = await generateKeyPair('RS256', { | ||
modulusLength: 2048, | ||
}); | ||
keyRS256 = privateKey; | ||
|
||
const createToken = (options: { issuer: string; audience: string }) => | ||
JWT.sign( | ||
{ | ||
sub: 'user-id', | ||
iss: options.issuer, | ||
aud: options.audience, | ||
[`${options.issuer}/claims/project_key`]: 'project-key', | ||
}, | ||
keyRS256, | ||
{ algorithm: 'RS256' } | ||
); | ||
// Export the public key to JWK format | ||
const publicJWK: JWK = await exportJWK(publicKey); | ||
|
||
export { jwksStore, createToken }; | ||
// Add the necessary properties for the JWKS | ||
publicJWK.use = 'sig'; // Signature | ||
publicJWK.alg = 'RS256'; // Algorithm | ||
publicJWK.kid = 'example-key-id'; // Key ID | ||
|
||
jwksStore = { | ||
keys: [publicJWK], | ||
}; | ||
} | ||
|
||
const createToken = (options: { issuer: string; audience: string }) => { | ||
if (!keyRS256) { | ||
throw new Error( | ||
'Key not initialized. Please call the "initialize" function first.' | ||
); | ||
} | ||
|
||
return new SignJWT({ | ||
[`${options.issuer}/claims/project_key`]: 'project-key', | ||
}) | ||
.setAudience(options.audience) | ||
.setIssuer(options.issuer) | ||
.setProtectedHeader({ alg: 'RS256' }) | ||
.setSubject('user-id') | ||
.sign(keyRS256); | ||
}; | ||
|
||
export { initialize, jwksStore, createToken }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,10 @@ module.exports = { | |
'process.env': { | ||
NODE_ENV: 'test', | ||
}, | ||
// This is required for the `jose` library to work in the test environment. | ||
// We use it in the packages-backend/express package. | ||
// Reference: https://github.com/jestjs/jest/issues/4422#issuecomment-770274099 | ||
Uint8Array: Uint8Array, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the trickiest part as I was having a very weird error but finally got some help in the linked GitHub issue. |
||
}, | ||
moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'json'], | ||
moduleDirectories: ['src', 'node_modules'], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ const modulesWithFaultyExports = [ | |
'@react-hook/resize-observer', | ||
'@react-hook/passive-layout-effect', | ||
'@react-hook/latest', | ||
'jose', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed as |
||
]; | ||
|
||
// https://jestjs.io/docs/configuration#resolver-string | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I had to rebuild the fixtures because most of the
jose
APIs have changed.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.
Thanks!