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

feat: signJWT, verifyJWT and decodeJWT utils #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

johannschopplich
Copy link

πŸ”— Linked issue

Note

I accidentally closed the PR #23. This PR includes the same changes.

#17

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Resolves #17.

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

  • signJWT
  • verifyJWT
  • decodeJWT

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: '[email protected]'
  },
  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

  • I had to rename crypto.web and crypto.node to crypto-web, respectively crypto-node, because unbuild v2 complained about the file extensions.
  • No tests added yet. I have used these utils for a while now in some Nuxt projects and published them as unjwt package – explicitly without uncrypto, since I rely on unenv to resolve the usage von the Web Crypto API.
  • In one project which used jose, I successfully migrated to these new utilities. No user got logged out. πŸ˜‹

Questions

  • atob and btoa are available in latest Node and worker versions, so I haven't added any usage of buffer. Is that OK for you?

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@johannschopplich johannschopplich requested a review from pi0 February 20, 2024 09:22
@johannschopplich
Copy link
Author

@pi0 Here it is, again! Feel free to rename methods, move methods around – I'm happy to learn from your methodology.

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

Successfully merging this pull request may close these issues.

easy creation and validation of JWTs
1 participant