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

activity-auth

v2.0.0

Published

Activity based authorization library and middleware

Downloads

7

Readme

Activity Authorization

A Javascript package that provides activity based authorization. This package includes a library for determining authorization and an express middleware function for associating an activity to a route for guarding access.

It expects sessions to be available as a cookie variable named token and that the session data was encrypted with "nJwt".

When a user's session is decrypted, their session data is added to the http request context. The library expects that when the user was logged in, that their roles were added to their session as aarray such as ["role1", "role2"].

When a user is authorized, their access data is added to the http request context.

The library can also determine which entities, components and apps a user has access to.

For example, say a program is organized into two sub-applications: content-management and user-management. Within each of these applications there are a few sub-components such as manage-faq and manage-pages. And the data that is managed in the manage-faq component are faq entities.

Why include these extra things? Depending on your application requirements, you may want to use activity based authorization to filter what links are displayed on a menu or which search results to retreive. Since all the user's access data is added to the request context before calling subsequent route handlers, it is easy to utilize this data in business logic.

Usage

Define Configuration Data

const activitiesByRole = {
  role: ["activity1", "activity2"],
  role2: ["activity0", "activity3"],
  role3: ["activity1", "activity3"]
};

// optional
const entitiesByActivity = {
  activity1: ["book", "tree"],
  activity2: ["tree", "car"],
  activity3: ["book", "frog"]
};

// optional
const components = [{id: "book-mananger", entity: "book", appId: "library"}];

// optional
const apps = [{id: "library"}];

// for nJwt to decrypt token from cookie
const signingKey = "XXX";

Create Authorization Factory and Apply Middleware

import { authorizerFactory, middlewareFactory } from "activity-auth";

const activityAuthorizer = authorizerFactory(
  activitiesByRole,
  entitiesByActivity,
  components,
  apps
);

const authMiddleware = middlewareFactory(activityAuthorizer, signingKey);

app.get("/some/path", authMiddleware("activity1"), controller.action);