Created
November 22, 2021 00:21
-
-
Save MeetMartin/9745d34f47d56c671db1ec9063248526 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { SignJWT, jwtVerify, importSPKI, importPKCS8 } from 'jose'; | |
const privateKey = | |
`-----BEGIN PRIVATE KEY----- | |
MC4CAQAwBQYDK2VwBCIEIMpFEKC3T8wWYu2e+63MuicRSt4ddWXGIZFXw4vnk+aL | |
-----END PRIVATE KEY-----`; | |
const publicKey = | |
`-----BEGIN PUBLIC KEY----- | |
MCowBQYDK2VwAyEA+L7HHlAU8Zviz0MCX4VSY1xRnX0UTSwb2bQPF6Oqh0g= | |
-----END PUBLIC KEY-----`; | |
const data = { | |
iss: 'https://domain.tld', | |
sub: '[email protected]', | |
aud: 'https://domain.tld', | |
exp: Math.floor(Date.now() / 1000) + (60 * 60), // 1 hour | |
nbf: Math.floor(Date.now() / 1000), | |
jti: 'asfasgsadg', | |
data: { some: 'information' } | |
}; | |
(async () => { | |
const importedPrivateKey = await importPKCS8(privateKey, 'EdDSA'); | |
const jwt = await new SignJWT(data).setProtectedHeader({ alg: 'EdDSA' }).sign(importedPrivateKey); | |
console.log('JWT', jwt); | |
const importedPublicKey = await importSPKI(publicKey, 'EdDSA'); | |
const { payload, protectedHeader } = await jwtVerify(jwt, importedPublicKey, { | |
issuer: 'https://domain.tld', | |
audience: 'https://domain.tld' | |
}); | |
console.log('protected header', protectedHeader); | |
console.log('jwt payload', payload); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment