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

Add 'validate' option to allow token claim validation #316

Open
2 tasks done
salmanm opened this issue Nov 23, 2023 · 2 comments
Open
2 tasks done

Add 'validate' option to allow token claim validation #316

salmanm opened this issue Nov 23, 2023 · 2 comments

Comments

@salmanm
Copy link
Member

salmanm commented Nov 23, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Can we add a new validate option into the package? It should accept a function similar to the trusted option but allow us to do some custom json-schema based validations on the token payload. While the current 'trusted' option serves a similar purpose, repurposing might not be suitable given the name "trusted" which implies security and not validation.

While external validation (in the callback of request.jwtVerify) is an option, integrating 'validate' directly into @fastify/jwt aligns with JWT operations, offering more convenience and coherence. If the maintainers are on board with this proposal, I'd be more than willing to contribute by creating a Pull Request to implement the 'validate' option.

Motivation

This feature is particularly valuable in scenarios involving external Identity Providers (IDPs) where unexpected modifications to claims can disrupt the application's functionality.

Thanks

Example

fastify.register(jwt, {
  secret: 'supersecret',
  validate: (request, tokenClaims) => {
    // JSON schema-based validation logic
    if (/* condition to check token validity */) {
      return true; // Token claims are valid
    } else {
      throw new Error('Invalid token claims'); // Throw an error for invalid claims
    }
  }
});
@Eomm
Copy link
Member

Eomm commented Dec 7, 2023

It seems a good feature.

I would take into account:

@mcollina
Copy link
Member

mcollina commented Dec 8, 2023

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

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

No branches or pull requests

3 participants