golang-jwt / jwt

Go implementation of JSON Web Tokens (JWT).
https://golang-jwt.github.io/jwt/
MIT License
6.98k stars 336 forks source link

How to handle multiple claim types #203

Closed fabiante closed 2 years ago

fabiante commented 2 years ago

Hi,

I might have a a case which is kind of special but maybe there is anyone who has an idea.

I use this great package to issue two types of JWTs:

I can't really figure out how to use jwt.Parse or jwt.ParseWithClaims to allow usage of both claim types.

The only idea I have is using MapClaims but then I'd lose the benefit of having explicit types for the two claim types.

Has anyone experience with this? Sincerely.

oxisto commented 2 years ago

Maybe my line of thinking is too easy but why not have one claim type that works for both and just have the values empty / nil which are not relevant to the other use case? More or less a UserBotClaims? There is not really a "dynamic" way of changing claims based on certain fields unfortunately.

You should also be able to do something like this:

type UserClaims struct {
  EMail string
  Name string
}

type BotClaims struct {
  BotId string
}

type AllMightyClaims struct {
   *BotClaims
   *UserClaims
   *jwt.RegisteredClaims
}
fabiante commented 2 years ago

@oxisto Hm, I like the idea. I might add a TokenType string field, which could directly contain the type, allowing an explicit switch-case for differentiation.