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

Introducing functional-style options for the Parser type #108

Merged
merged 5 commits into from Oct 13, 2021
Merged

Conversation

oxisto
Copy link
Collaborator

@oxisto oxisto commented Sep 26, 2021

This PR follows up the discussion in #107 and provides an easier way to configure the parser. This is of utmost importance, because we want people to actually use ValidMethods and currently, the shortcut function Parse in token.go was just using a new Parser type, without any possibility to change its properties.

Note: While this technically does change the function signature of two exported functions Parse and ParseWithClaims, it only adds an variadic argument. This means, that for a user of this library, this change is completely transparent, as the minimum number of variadic arguments is 0, meaning that the function can be called without any additional options. So in my opinion, this would be ok-ish to be considered API stable in terms of semver.

This is heavily inspired by the original release4 branch of @dgrijalva

cc @sebastien-rosset

@oxisto
Copy link
Collaborator Author

oxisto commented Sep 26, 2021

I was also thinking about getting rid of the ParseWithClaims functions all together and moving this to a parser option as well, but I think that may be a larger undertaking.

@oxisto oxisto requested a review from lggomez September 26, 2021 08:08
@mfridman
Copy link
Member

If there is a configurable parser with functional options (via variadic arguments) this opens the opportunity to add behaviour in a backwards compatible way in the current branch.

(I know I sound like a broken record w.r.t backwards compatibility, but that is the one thing above all that I care about in this package)

Do you think we should build a breaking but "nicer" API in a /v5 release, or do we want to continue with /v4 and add non-breaking functionality to it?

The ParseWithClaims was quite handy.

@oxisto
Copy link
Collaborator Author

oxisto commented Sep 29, 2021

Do you think we should build a breaking but "nicer" API in a /v5 release, or do we want to continue with /v4 and add non-breaking functionality to it?

I think we can sort of have both. In my opinion, the approach implemented now is completely backwards compatible and it offers a quite nice way of configuring the options

The ParseWithClaims was quite handy.

I will keep it (for now) and I also added variadic arguments to it, so it can be fed with the same WithXXX functional options as the pure Parse function.

Copy link
Member

@mfridman mfridman left a comment

Choose a reason for hiding this comment

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

overall looks sane. Can you remind me why we're adding functional options? Not against it, just curious.

EDIT: never mind, I found the discussions in the linked issue.

parser.go Outdated Show resolved Hide resolved
mfridman
mfridman previously approved these changes Oct 13, 2021
Copy link
Member

@mfridman mfridman left a comment

Choose a reason for hiding this comment

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

lgtm.


// WithValidMethods is an option to supply algorithm methods that the parser will check. Only those methods will be considered valid.
// It is heavily encouraged to use this option in order to prevent attacks such as https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/.
func WithValidMethods(methods []string) ParserOption {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to have a unit test.

// If populated, only these methods will be considered valid.
//
// In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
ValidMethods []string
Copy link
Member

Choose a reason for hiding this comment

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

Could we live in a world where algorithms are well defined, instead of strings?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Probably, at some future point. We should keep it in mind when de-exporting the field in the future.

@oxisto
Copy link
Collaborator Author

oxisto commented Oct 13, 2021

lgtm.

I added the deprecation notice to the field. Please re-approve ;)

@oxisto oxisto merged commit 65357b9 into main Oct 13, 2021
@oxisto oxisto deleted the parser-options branch October 13, 2021 17:36
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.

None yet

3 participants