npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

passport-masquerade

v1.0.1

Published

Toolkit to support masquerading as another user.

Downloads

6

Readme

passport-masquerade

"ezpz impersonate another user"

This provides a middleware to allow administrators (or other users - your implementation defines the permissions) to masquerade as other users, for example, to implement a "god view."

From a design perspective, masquerading could be done two fundamental ways:

  • Swap req.user with the temporary user and store some req.realUser. This means code that touches your user elsewhere does not need to be modified.
  • Add a req.appliedUser type field and check all uses against it. This has the advantage of not contaminating your authentication flow.

This library chooses the first method. Note that this means you likely have to adjust any function that needs to know the genuine user.

Masqueraded users have an additional field called masqueradingFrom that contains the original user. This may be used, for example, to show some front end indication that you're masquerading.

Dependencies

A passport session implementation must be available.

Opinionated Usage

There's a helpers object that provides an approach to the integration. See helpers.js.

const passportMasquerade = require("passport-masquerade");
const { middleware, helpers } = passportMasquerade({
  getUserById: id => ({
    id,
    name: "Fake User"
  }),
  canUserMasquerade: (user, requestedId) => {
    // `user` is the current real user.
    // `requestedId` is who the uesr is asking to masquerade as.
    return true;
  }
});

// ...

passport.deserializeUser(helpers.deserializeUser);
passport.serializeUser(helpers.serializeUser);

// ...

app.use(middleware);

// ...

router.post("/masquerade/clear", helpers.unmasqueradeEndpoint);
router.post("/masquerade/:id", helpers.masqueradeEndpoint);

The helpers are opinionated, requiring, for example, fields like :id, full user objects and JSON response blobs. As such, the raw functionality is exposed as follows.

Raw Usage

const passportMasquerade = require("passport-masquerade");
const {
  middleware,
  setMasquerading,
  clearMasquerading,
  getRealUser
} = passportMasquerade({
  serializeMasquerade: i => i,
  deserializeMasquerade: i => i
});

app.use(middleware);

// ...

router.post("/masquerade/clear", (req, res) => {
    clearMasquerading(req);
});

router.post("/masquerade/:id", (req, res) => {
  if (allowedToMasquerade(getRealUser(req.user))) {
    setMasquerading(req, someOtherUser);
  }
};

As appropriate for your use case, you can mix-and-match the opinionated helpers implementation and the less opinionated raw functions.

Gotchas

Passport's serializeUser should serialize the real user.

At the end of a request, passport will call serializeUser with the masqueraded user. To handle this, we provide the getRealUser method to ensure you don't persist a masqueraded state.

passport.serializeUser((potentiallyMasqueradedUser, done) => {
  const user = getRealUser(potentiallyMasqueradedUser);

  done(null, user.id);
});

The helper version of serializeUser handles this for you. If you were to persist your masqueraded user, you will lose your underlying real user session and the behavior should be considered undefined. Open to PRs that improve detection of this behavior to ease developer surprise.

Deserialized users should be (mutable) objects.

This is normally the case, though if you are storing a simple string or ID as a deserialized user, we throw an error.

Specifically, they need to support tacking on the masqueradedFrom key. Open to PRs that generalize this behavior to support other user styles.

If you cannot return a valid user from a requested deserialization, you should throw.

License

Copyright 2019 Giuseppe Burtini. MIT License.