Skip to content
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

How to safe the CryptoKey for the djwt #80

Open
MielkeDaniel opened this issue Jan 10, 2023 · 1 comment
Open

How to safe the CryptoKey for the djwt #80

MielkeDaniel opened this issue Jan 10, 2023 · 1 comment

Comments

@MielkeDaniel
Copy link

MielkeDaniel commented Jan 10, 2023

Im currently implementing a login system in deno using the djwt library (https://deno.land/x/[email protected]).
It wants me to generate a cryptokey like so:

const key = await crypto.subtle.generateKey(
  { name: "HMAC", hash: "SHA-512" },
  true,
  ["sign", "verify"],
);

Problem is, I cant safe that key in my env variables. If I logout the value of the key it looks like that:

CryptoKey {
  type: "secret",
  extractable: true,
  algorithm: { name: "HMAC", hash: { name: "SHA-512" }, length: 1024 },
  usages: [ "sign", "verify" ]
}

When generating the jwt, the create function wants to have such a CryptoKey object instead of a string, like back in the days (
const jwt = await create({ alg: "HS512", typ: "JWT" }, payload, key);
).
How can I consistently safe that cryptokey, so it doesnt change on each restart of my deno app?
Because obviously if i want to verify the old sessions i also need the old key again...

Clad for any help I can get!

@jpsSO
Copy link

jpsSO commented Jan 10, 2023

you can use
const exportedKey = await crypto.subtle.exportKey("raw", key);
to export the key (base64 encode it to store it) and later use importKey to get it back.

instead of a string, like back in the days

or even just import a plain string as shown in the link.
You can see an example of the import on https://stackoverflow.com/questions/64494966/jwt-authentication-with-deno

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants