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 string jwt #2666

Merged
merged 11 commits into from
Apr 23, 2024
Merged

Add string jwt #2666

merged 11 commits into from
Apr 23, 2024

Conversation

danilomourelle
Copy link

@danilomourelle danilomourelle commented Aug 18, 2023

Add a .jwt() validator in string type.

It expects for a string with 3 parts splited by . with the first part being a base64 encoded string that represents a objetc wit a property type with value JWT.

It validates just format NOT signature.

Resolves #2946

@netlify
Copy link

netlify bot commented Aug 18, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Name Link
🔨 Latest commit 00ba486
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/662842d236cd16000958338b
😎 Deploy Preview https://deploy-preview-2666--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@hudson-bruno
Copy link

Just a reminder that you left some console.logs in the code

@danilomourelle
Copy link
Author

Ooopss... thanks man, already removed.

}

const [header] = tokensParts;
const parsedHeader = JSON.parse(Buffer.from(header, "base64").toString());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should works both in node.js and modern browsers, so you can't use Buffer. You can take a look at the code. https://github.com/validatorjs/validator.js/blob/master/src/lib/isJWT.js

Copy link
Author

@danilomourelle danilomourelle Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not a front-end guy 🙈.

The link you've passed, they do not decode base64 as I intended to do. Replace Buffer for atob are supposed to work.

Also added option to pass algorithm used in JWT.

@nikelborm
Copy link

nikelborm commented Jan 26, 2024

Why wasn't this PR merged yet? It will be very useful for my use case! Does it need some improvements?

@danilomourelle
Copy link
Author

Hey guys, anything else I should do to it be accept?

@danilomourelle
Copy link
Author

Hello @colinhacks

I believe this PR are good to go. Let me know if need anything else.

danilomourelle and others added 11 commits April 23, 2024 16:12
Validate string in JWT format. NOT validate signature
Validate all possibilities for validation function
Add z.string().jwt() to String validations list
…validation

atob method is compatible with Node and modern browsers
Can pass a algorithm option to jwt method to check the algorithm of token. If not pass, no check is done for alg.
Fix tests with false positive and add tests to check algorithm validation
Add info that jwt method accepts algorithm as option
@colinhacks colinhacks changed the base branch from master to v4 April 23, 2024 23:27
@colinhacks
Copy link
Owner

Thanks, this is excellent stuff! Apologies for the long turnaround time. I'm hesitant to merge features like this into Zod 3 for bundle size reasons. Zod 4 will have better tree shaking characteristics so I'm merging this into the v4 branch. Some version of this will land in Zod 4, but (as a heads up) it may be a while, and this feature/API will likely undergo some changes in the interim.

@colinhacks colinhacks merged commit 12eca8e into colinhacks:v4 Apr 23, 2024
20 checks passed
colinhacks added a commit to ytsunekawa/zod that referenced this pull request May 3, 2024
* feat(string): add jwt validator type

Validate string in JWT format. NOT validate signature

* test(string): add tests to validate jwt parse

Validate all possibilities for validation function

* docs(main): update main README

Add z.string().jwt() to String validations list

* fix(types): remove forgotten console.log

* refactor(types-string): replace Buffer.from method for atob into JWT validation

atob method is compatible with Node and modern browsers

* fix(types-string): header property was misspelled

Property is 'typ' and not 'type'

* feat(types-string): add algorithm option to jwt

Can pass a algorithm option to jwt method to check the algorithm of token. If not pass, no check is done for alg.

* test(string): update tests to check jwt method

Fix tests with false positive and add tests to check algorithm validation

* docs(README): update readme.md

Add info that jwt method accepts algorithm as option

* Tweak API and docs

* Fix tests

---------

Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
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.

Add support for jwt
5 participants