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

Header decode compatibility issue #2

Closed
duclm2609 opened this issue Dec 1, 2020 · 9 comments
Closed

Header decode compatibility issue #2

duclm2609 opened this issue Dec 1, 2020 · 9 comments
Labels
enhancement New feature or request

Comments

@duclm2609
Copy link

duclm2609 commented Dec 1, 2020

I currently have a problem with header decode. I have 2 services, one in Golang and one in Java. The JWT token is created on Java service using jjwt library (https://github.com/jwtk/jjwt). The header after decode is, for e.g: {"typ":"JWT","alg":"RS256"} but currently in this library you are hardcode it as following:

func createHeaderRaw(alg string) []byte {
	if header := fixedHeaders[alg]; header != nil {
		return header.raw
	}

	return []byte(`{"alg":"` + alg + `","typ":"JWT"}`)
}

which produce output: {"alg":"RS256","typ":"JWT"}. Clearly the content are the same but the check is failed since you are compare them byte by byte. I would suggest another check which using map. I can create a pull request if it's possible.
Thank you for your great work.

@kataras
Copy link
Owner

kataras commented Dec 1, 2020

Hello @duclm2609, for known algorithms (like RS256) the fixedHeaders are used (for performance boost), we can just add the typ:..., alg:.. forms too :) There is no need to support custom header/map at the moment (although it's not hard to do it).

EDIT: I think your issue with the java libray is that it accepts the golang one, but this golang jwt library does not accept the java's one because the order is different, right? If that's the case, then the issue is not on the createHeaderRaw itself but it's on the decodeToken (which calls the createHeaderRaw, we should modify that part to be able to understand the opossite form too). Note that, normally, this is the "correct" JWT specification form, that our kataras/jwt library uses.

@kataras kataras added the enhancement New feature or request label Dec 1, 2020
@kataras kataras closed this as completed in 933b4a7 Dec 2, 2020
@geordee
Copy link

geordee commented Dec 5, 2020

On the header decode compatibility issue, the typ header is optional as per the specs.

I have a provider which creates JWT without the typ header, and some additional parameters (JWS). Is it possible to make the call to compareHeader optional using a flag?

@cfeeling
Copy link

I also met this issue when verifying the JWT token which was created by some library in other language. They added some additional keys in that header, and I must not modify the implementation in that library.

On my side, I would like to use my custom function to compare header of token, which is done by build-in compareHeader (

jwt/token.go

Line 159 in 9f23c50

func compareHeader(alg string, headerDecoded []byte) bool {
) right now, if it is possible.

Thanks for your work, it already makes my work more easier!

@kataras
Copy link
Owner

kataras commented Dec 25, 2020

Sorry for the delay @cfeeling, @geordee. I've just pushed a commit and a new tag 0.0.9 which exports the CompareHeader so you can modify it globally.

Merry Christmas 🎄 🎅🏽 🤶🏽

@cfeeling
Copy link

Sorry for the delay @cfeeling, @geordee. I've just pushed a commit and a new tag 0.0.9 which exports the CompareHeader so you can modify it globally.

Merry Christmas 🎅🏽 🤶🏽

Thanks, it is one of nice things in this holiday!

I could use it as resetting the public variable CompareHeader right now, thats great!

Happy New Year, dear @kataras @geordee

@kataras
Copy link
Owner

kataras commented Jan 1, 2021

Thanks @cfeeling, exactly! Happy new year 😊🙌🥳

kataras added a commit that referenced this issue Feb 11, 2021
kataras added a commit that referenced this issue Feb 11, 2021
@kataras
Copy link
Owner

kataras commented Feb 11, 2021

Hello @duclm2609 and @cfeeling.

I've just pushed v0.1.0, get it with:

$ go get -u github.com/kataras/jwt@latest

which contains new more methods: SignWithHeader and VerifyWithHeaderValidator to support custom headers and public key validation per-token (e.g jwt key ids/amazon and e.t.c.). Please take a look at the complete example at: https://github.com/kataras/jwt/blob/main/_examples/custom-header/main.go

@cfeeling
Copy link

Hello @duclm2609 and @cfeeling.

I've just pushed v0.1.0, get it with:

$ go get -u github.com/kataras/jwt@latest

which contains new more methods: SignWithHeader and VerifyWithHeaderValidator to support custom headers and public key validation per-token (e.g jwt key ids/amazon and e.t.c.). Please take a look at the complete example at: https://github.com/kataras/jwt/blob/main/_examples/custom-header/main.go

Hi @kataras , I received your message now, but I'm busy recently. I will be back and response ASAP when I was free, thanks for your work.

@kataras
Copy link
Owner

kataras commented Feb 27, 2021

Hi @cfeeling no worries, take your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants