goa icon indicating copy to clipboard operation
goa copied to clipboard

JWT middleware usage example

Open radekdymacz opened this issue 10 years ago • 6 comments

Would it be possible to have example of how to use JWT middleware ? CORS was quite easy and there are docs on the site but I can't get my head around how you use JWT

radekdymacz avatar Dec 15 '15 13:12 radekdymacz

Thanks for raising this, here is an example until docs get added:

    spec := &jwt.Specification{
        AllowParam:       false,
        AuthOptions:      false,
        TTLMinutes:       1440,
        Issuer:           "api.xordataexchange.com",
        KeySigningMethod: jwt.RSA256,
        SigningKeyFunc:   privateKey,
        ValidationFunc:   pubKey,
    }
    service.Use(jwt.Middleware(spec))

    tm := jwt.NewTokenManager(spec)

You can then use tm to create tokens. Obviously this is just an example, your code may be organized differently.

raphael avatar Dec 15 '15 14:12 raphael

I presume service.Use(jwt.Middleware(spec)) will wrap all API endpoints in jwt and would require token. How would I exclude i.e. /auth/login which would be used for authenticating and then generating tokens for client?

radekdymacz avatar Dec 15 '15 14:12 radekdymacz

That's correct. You can mount middlewares on specific controllers instead of service wide.

raphael avatar Dec 15 '15 14:12 raphael

Brilliant thanks ! BTW great project !

radekdymacz avatar Dec 15 '15 14:12 radekdymacz

Thank you! I'll leave this issue opened until we have proper docs for the JWT middleware.

raphael avatar Dec 15 '15 14:12 raphael

The package is now properly documented. Thanks for the report.

raphael avatar Dec 17 '15 13:12 raphael