unjs / uncrypto

Single API for Web Crypto API and Crypto Subtle working in Node.js, Browsers and other runtimes
MIT License
182 stars 8 forks source link

feat: `signJWT`, `verifyJWT` and `decodeJWT` utils #23

Closed johannschopplich closed 4 months ago

johannschopplich commented 11 months ago

๐Ÿ”— Linked issue

17

โ“ Type of change

๐Ÿ“š Description

Resolves #17.

The following JWT utilities will be available with this PR (migrated from unjwt):

I have kept the code as simple as possible to cover the basic needs for JWT signing, verification and decoding. Method parameters have a balance between sensible defaults and customization.

Example usage:

import { decodeJWT, signJWT, verifyJWT } from 'uncrypto/jwt'

interface JWTUserClaims {
  email: string
}

const secret = 'secret'
const issuer = 'https://domain.com'

// Sign a JWT
const accessToken = await signJWT<JWTUserClaims>({
  payload: {
    email: 'user@domain.com'
  },
  secret,
  issuer,
  audience: issuer,
})

// Verify a JWT
try {
  const verifiedAccessToken = await verifyJWT({
    token: accessToken,
    secret,
    issuer,
    audience: issuer
  })
}
catch (error) {
  // Handle error
  console.error(error)
}

// Decode a JWT โ€“ does not verify the signature
const decodedAccessToken = await decodeJWT<JWTUserClaims>(accessToken)
console.log(decodedAccessToken.email)

Please verify if the general direction of this PR makes sense to you. If you, I'm willing to add tests as best as I can.

Notes

Questions

๐Ÿ“ Checklist

johannschopplich commented 11 months ago

@pi0 Suggestions implemented. Thanks for the guidance!

pi0 commented 4 months ago

@johannschopplich i think it is a still good idea sorry didn't have to check on it yet. Any reason you preferred to close?

johannschopplich commented 4 months ago

Hi there, @pi0! I didn't mean to close this PR... I deleted some forked repos from my GitHub account and wasn't aware that this PR would be closed as a result.

No problem. I know you're working on a lot of projects and I don't have any expectations when contributing to OSS. ๐Ÿ™‚

Having said that, I'm now using oslo/jwt for JWT handling. It's runtime-agnostic and feels a bit like an UnJS project.

Should I open this PR again?

pi0 commented 4 months ago

Surely if you like to reopen i can handle the updates to merge as soon as could ๐Ÿ‘๐Ÿผ I'm aware about oslo but kinda have some plans here that are unique ;) (Also we have OTP coming: #37)