GithubHelp home page GithubHelp logo

Support JWT custom claims about goyave HOT 6 CLOSED

go-goyave avatar go-goyave commented on June 25, 2024
Support JWT custom claims

from goyave.

Comments (6)

Morishiri avatar Morishiri commented on June 25, 2024 1

Yes, I'm interested ;)

from goyave.

System-Glitch avatar System-Glitch commented on June 25, 2024

Hello and thank you for your proposal! I was planning on adding a auth.GenerateTokenWithClaims() function in the near future, which would let you add custom claims.

Can you elaborate on how you planned on implementing this feature? I think this would be a great addition.

Do you need your JWT in other applications? You probably don't need to add anything else than the ID of the user since the authenticator will fetch the user for you in the database, so I'm not sure a struct tag auth:"jwtInclude" would be really useful (and it would require some reflection).

from goyave.

Morishiri avatar Morishiri commented on June 25, 2024

I was planning on adding a auth.GenerateTokenWithClaims() function in the near future, which would let you add custom claims.

Can you elaborate on how you planned on implementing this feature? I think this would be a great addition.

Adding auth.GenerateTokenWithClaims() would help, my plan was simply not to introduce additional functions, but it actually may be better to do so to not use reflection too much. The idea was to add auth:"jwtInclude" annotation to the fields from the User struct which should be added to the token, then get this values from the user object and simply put them as claims, but as you already said, this would require reflection to get the fields annotations and may slow things down.

Do you need your JWT in other applications? You probably don't need to add anything else than the ID of the user since the authenticator will fetch the user for you in the database, so I'm not sure a struct tag auth:"jwtInclude" would be really useful (and it would require some reflection).

It can be used for e.g. to put user first and last name to the token, so when the user is logged in I can simply look into the token and display his name in the header menu (e.g. avatar button on github, there is Signed in as <nickname> text) without the need to execute a call to /users/me (or some similar route). But it's not critical. The authenticator fetching the user object is really great feature and implementing such /me endpoint is not a big hassle with that.

What I proposed was mainly front-end related to just read the token and not call APIs, may be improvement for some people, but I agree that in this case separate function may be better approach.

from goyave.

System-Glitch avatar System-Glitch commented on June 25, 2024

I understand your use-case. The best approach would be to implement your own Login handler and use this new GenerateTokenWithClaims() function to add everything you need, instead of using the built-in JWTController. But re-implementing that is quite a bit of work and produces duplicate code.

Maybe we could add some features to the JWTController to make it more flexible. What would you think about something like this?

jwtController := auth.NewJWTController()
jwtController.TokenFunc = func(r *goyave.Request, user interface{}) (string, error) {
    return auth.GenerateTokenWithClaims(jwt.MapClaims{
        "name": user.(*model.User).Name
    })
}

from goyave.

Morishiri avatar Morishiri commented on June 25, 2024

Yeah, I think it's good idea, while leaving the default as it is currently. Then we don't have any breaking changes.

from goyave.

System-Glitch avatar System-Glitch commented on June 25, 2024

If you are still interested in the implementation of this, please let me know! Any contribution is very much welcome!

from goyave.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.