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

How to get token from unprotected route #178

Closed
neurosnap opened this issue Dec 5, 2020 · 3 comments
Closed

How to get token from unprotected route #178

neurosnap opened this issue Dec 5, 2020 · 3 comments

Comments

@neurosnap
Copy link

Hey all, I have routes that are unprotected but I still want to grab the user information if they provide an Authorization token, how could this be accomplished using this library? It seems like I either have to protect the routes to get the ctx.state.user object or leave them unprotected and not have access to the state user object.

Thanks!

@sdd
Copy link
Collaborator

sdd commented Dec 6, 2020

You can initialize koajwt with the option passthrough:true to ensure that downstream middleware is called even when the auth fails. Then you can create a further middleware that returns a 401 if ctx.state.user is empty and use that for the authenticated routes.

@sdd
Copy link
Collaborator

sdd commented Dec 6, 2020

Actually now that I think about it, this won't work. Why do you want to do this? You could do this by using the jsonwebtoken library's jwt.decode function directly for the unprotected routes. See https://www.npmjs.com/package/jsonwebtoken#jwtdecodetoken--options

@neurosnap
Copy link
Author

Why do you want to do this?

I have an entity that can be publicly visible but depending on the record property (entity.public: true/false) it determines whether the person requesting the data has access to it.

So for example, let's say I'm rebuilding github and I want to fetch a specific repository. I want that endpoint to be public because the system allows for public repos that don't require authorization. But if the resource the user is trying to fetch is private then I need to reject the request. Does that make sense?

Yeah that was my thinking as well: rebuild the parts of koa-jwt that extract the authorization header and manually add a ctx.state.user object for those unprotected routes.

@sdd sdd closed this as completed Apr 2, 2021
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

No branches or pull requests

2 participants