-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 validate JWT tokens supplied by incoming requests #2057
Comments
There is an open issue - #2035 |
I saw that. I only need application-wide middleware. Is that not implemented yet as well? |
@yeager-j In your example, you are showing a route-specific usage of In LoopBack 4, users should use Sequence actions for adding custom request (pre)processing steps. At least that was our original design idea. See Sequence chapter in our documentation to learn more. Does export type CheckJwt = (request: Request): Promise<void>;
class CheckJwtActionProvider implements Provider<CheckJwt> {
constructor(
// does jwt provide a type definition we could use instead of generic object?
@inject('jwt.config') private config: object
) {}
value() {
// Use the lambda syntax to preserve the "this" scope for future calls!
return (request: Request) => {
this.action(request);
};
action(request: Request): Promise<void> {
const config = // merge global config in `this.config` with request-specific config
return jwt.check(request, config);
}
} If it does not, then you can invoke the middleware function too. The difficult part is how to handle the case when the middleware did not call Using the above helper, the sequence action could be implemented as follows: export type CheckJwt = (request: Request, response: Response): Promise<boolean>;
class CheckJwtActionProvider implements Provider<CheckJwt> {
constructor(
// does jwt provide a type definition we could use instead of generic object?
@inject('jwt.config') private config: object
) {
}
value() {
// Use the lambda syntax to preserve the "this" scope for future calls!
return (request: Request, response: Response) => {
this.action(request);
};
action(request: Request, response: Response): Promise<void> {
const config = // merge global config in `this.config` with request-specific config
const checkJwt = jwt(config);
return executeRequestHandler(checkJwt, request, response);
}
} Usage in your custom sequence: class MySequence extends DefaultSequence {
async handle(context: RequestContext) {
const route = this.findRoute(context.request);
const handled = await this.checkJwt();
if (handled) {
// ouch, jwt already sent back a response
return;
}
const params = await this.parseParams(context.request, route);
// etc.
}
} |
hi @bajtos in your sequence example posted above where does this.checkJwt() comes from? Thanks in advance. |
Discussion with @raymondfeng @jannyHou @emonddr: |
Acceptance Criteria
|
We now have the TokenService interface in |
@jannyHou do we have any documentation showing users how to write such custom token service provider? If not, then I am proposing to keep this issue open and set the acceptance criteria to write such documentation. Thoughts? |
@bajtos Yep Dom wrote a tutorial for creating the token service in https://loopback.io/doc/en/lb4/Authentication-Tutorial.html, especially see the section https://loopback.io/doc/en/lb4/Authentication-Tutorial.html#creating-a-custom-sequence-and-adding-the-authentication-action Let us know if you expect to see more details or have any questions. I can add it. |
Thank you @jannyHou for the pointers. I this the section Creating a Token Service is exactly what I was looking for. Let's close this issue as resolved then. |
Description / Steps to reproduce / Feature proposal
I'm attempting to build an app with Auth0 and Loopback. Auth0 requires some Express middleware, such as
express-jwt
. I've searched through GitHub issues and the docs to find a way to use Express middleware in a Loopback app and I've found nothing. Perhaps I'm missing something glaringly obvious.I need a way to do this - from the Auth0 Documentation:
I'm feeling a bit hopeless. I'm pretty new to Loopback and Auth0.
See Reporting Issues for more tips on writing good issues
The text was updated successfully, but these errors were encountered: