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

Question: How to identify type of token passed in a request? #748

Closed
shrihari-prakash opened this issue Jan 2, 2023 · 0 comments
Closed

Comments

@shrihari-prakash
Copy link

I am trying to create a system where I would like to have certain APIs accessible by users and certain APIs only by tokens granted by client_credentials. Here's my current way of identifying this:

In model:

getUserFromClient: (client: Client) => {
  return new Promise<User>((resolve) => {
    resolve({ 
        username: "api-client", 
        role: client.role // DB returns string 'INTERNAL_CLIENT'
    });
  });
},

In my middleware:

const AuthenticateClient = async (
  _: Request,
  res: Response,
  next: NextFunction
) => {
  try {
    res.locals.user = res.locals.oauth.token.user;
    if (res.locals.user.role !== 'INTERNAL_CLIENT') {
      throw statusCodes.unauthorized;
    }
    res.locals.client = res.locals.oauth.token.client;
    return next();
  } catch (err) {
    res
      .status(statusCodes.unauthorized)
      .json(new ErrorResponse(errorMessages.unauthorized));
    return next(new Error(errorMessages.unauthorized));
  }
};

I however feel like this is an anti-pattern to do such a thing. Can someone confirm how this can be done in the library?

@shrihari-prakash shrihari-prakash closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2023
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

1 participant