Skip to content

Instantly share code, notes, and snippets.

@preinpost
Created March 9, 2023 06:16
Show Gist options
  • Save preinpost/c6316b2a833ae62b574b3f204b00446b to your computer and use it in GitHub Desktop.
Save preinpost/c6316b2a833ae62b574b3f204b00446b to your computer and use it in GitHub Desktop.
jwt
import hmacSHA256 from "crypto-js/hmac-sha256"
import Base64url from "crypto-js/enc-base64url"
import { Buffer } from "buffer"
export default function generateMockToken() {
const header = {
typ: "JWT",
alg: "HS256"
}
const payload = {
eid: "some_eid",
req_time: new Date().getTime()
}
const headerBase64Url = encode(JSON.stringify(header), "utf-8").replace(/=/gi, "")
const payloadBase64Url = encode(JSON.stringify(payload), "utf-8").replace(/=/gi, "")
const strings = headerBase64Url + "." + payloadBase64Url
const secretKey = "some_secretkey" // 서명값
const hash = Base64url.stringify(hmacSHA256(strings, secretKey))
return strings + "." + hash
}
function encode(str, encoding) {
return Buffer.from(str, encoding || "utf8").toString("base64")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment