Giter Site home page Giter Site logo

Unexpected change of JWT Key about djwt HOT 10 CLOSED

so1ve avatar so1ve commented on July 22, 2024
Unexpected change of JWT Key

from djwt.

Comments (10)

timonson avatar timonson commented on July 22, 2024

Does it work if you create a key only once in the deployed app?

from djwt.

chrisyalamov avatar chrisyalamov commented on July 22, 2024

The Web Crypto API has methods for importing and exporting keys (see the mdn web docs for more info)

  1. Generate a key
const key = await crypto.subtle.generateKey(
  { name: "HMAC", hash: "SHA-512" },
  true,
  ["sign", "verify"],
);
  1. Create a JWT
const jwt = await create({ alg: "HS512", typ: "JWT" }, { foo: "bar" }, key);
  1. Export the key
    E.g. write this to a file which you can read later on— it's up to you how you persist this file, depending on your project.
let exportedKey = await crypto.subtle.exportKey("jwk", key)
  1. Reimport the key and confirm everything works
let reimportedKey = await crypto.subtle.importKey(
  "jwk", 
  exportedKey, 
  { name: "HMAC", hash: "SHA-512" }, true, ["sign", "verify"]);

  // Confirm everything works as expected

  console.log(await verify(jwt, reimportedKey))
  // {foo: 'bar'}

from djwt.

so1ve avatar so1ve commented on July 22, 2024

Does it not work if you create a key only once in the whole deployed app?

Yes, I exported it in a file (jwt_key.ts):

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

from djwt.

so1ve avatar so1ve commented on July 22, 2024

The Web Crypto API has methods for importing and exporting keys (see the mdn web docs for more info)

  1. Generate a key
const key = await crypto.subtle.generateKey(
  { name: "HMAC", hash: "SHA-512" },
  true,
  ["sign", "verify"],
);
  1. Create a JWT
const jwt = await create({ alg: "HS512", typ: "JWT" }, { foo: "bar" }, key);
  1. Export the key
    E.g. write this to a file which you can read later on— it's up to you how you persist this file, depending on your project.
let exportedKey = await crypto.subtle.exportKey("jwk", key)
  1. Reimport the key and confirm everything works
let reimportedKey = await crypto.subtle.importKey(
  "jwk", 
  exportedKey, 
  { name: "HMAC", hash: "SHA-512" }, true, ["sign", "verify"]);

  // Confirm everything works as expected

  console.log(await verify(jwt, reimportedKey))
  // {foo: 'bar'}

thank you for your reply! However, how should I implement this?

from djwt.

youwei997 avatar youwei997 commented on July 22, 2024

兄弟,找到解决办法没

from djwt.

so1ve avatar so1ve commented on July 22, 2024

兄弟,找到解决办法没

没呢

目前我的办法是 将JSON化后的JWT Key硬编码在环境变量里()

from djwt.

timonson avatar timonson commented on July 22, 2024

Please talk in English guys!
@so1ve wouldn't you agree that this a deno.com issue and not a djwt issue, if at all? Although, it might be that I have not fully understood the problem yet.

from djwt.

so1ve avatar so1ve commented on July 22, 2024

I'm so sorry...it's actually a deno.com issue. denoland/deploy_feedback#237

from djwt.

timonson avatar timonson commented on July 22, 2024

Oh no problem at all. I was rather curious what the problem was. Thank you!

from djwt.

so1ve avatar so1ve commented on July 22, 2024

I think this is caused by Deno Deploy being deployed on the edge server, which causes my program to run from scratch every time (including the part that generates the key).

from djwt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.