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

canhaz

v0.1.0

Published

Simple and to-the-point access control.

Downloads

4

Readme

canhaz - Simple access control.

Build Status Coverage Status

No assumptions about user model and method of persistence. Classify users into roles. Grant permissions. Check them directly or through a middleware.

Deals with authorization, and nothing else. Plays well with a small-ish number of roles / permissions that doesn't change often -- which is not a very rare case.

Status

  • [x] Works. Grant and check simple permissions.
  • [ ] Check permissions with wildcards in the middle. e.g. a.**.b.*.c

Example (as middleware)

var canhaz = require('canhaz')({
    'somebody': ['stuff']
});

canhaz.role('somebody', function(req) { return !!req.user; });

app.get('/stuff', canhaz('stuff'), function(req, res, next) {
    if (req.authorized)
        res.status(200).send('Hi!');
    else
        res.status(401).send('Pfft.');
});

Config Object

roles

A PlainObject, associating role names with their permissions. i.e. { RoleName: PermissionObject, ... }

Where PermissionObject is either an array consisting of permissions names, or a PlainObject associating permission names with null, indicating no sub-permissions, or a PermissionObject describing sub-permissions:

PermissionObject = PermissionArray | PermissionPlainObject
PermissionArray = [ Name | [ Name, PermissionObject ], ... ]
PermissionPlainObject = { Name: null | PermissionObject, ... }

When the key roles cannot be found in the config object, the object passed is assumed to be roles, and all the flags are set to their default value, as demonstrated in the example above.

Kitchen sink:

var config = {
    roles: {
        'nobody': [],
        'user': {
            'post': {
                'read': null,                 // post.read
                'create': null,               // post.create
                'comment': ['read', 'create'] // post.comment.read/create
            }
        },
        'editor': [['post', ['**.*']]],       // post.**.*
        'admin': ['**.*']                     // **.*
    }
};

allowMiddleWildcards: false

Enables checking of wildcards in the middle.

This is NOT implemented yet!

By default, only single wildcards for the subpermission name, and double wildcards in place for the last resource name are allowed. e.g. 'a.b.c.', 'a.b..d' and 'a.b..', but not 'a.**.b.*.c'.

Most of the time, this is all you need. However, more complex patterns could sometimes be useful. This flag allows that. Note that it turns the O(L) lookup into a O(NL) search operation, where L equals the '.' level of the query, and N equals the number of defined permission patterns.

memoize: true

Allows memoization of queries.

A naive in-memory store is used. In a typical case with a small-ish number of permissions, the memory usage should not be a problem.

API

require('canhaz')(config, [field], [classifier])

Creates a canhaz middleware factory instance.

config: PlainObject|String -> [String], Used to get eligible roles by permission. When object, should be a permission config object. When function, should return all eligible roles by permission.

field: String, Field name on request object to fill when used as middleware. Defaults to 'authorized'.

classifier: Classify, When not omitted, use the provided Classify instance instead of a new one.

Returns: function, The canhaz middleware factory.

canhaz(...names)

Creates a middleware instance.

names: ...String, Permissions to check for.

Returns: Middleware, Connect/Express middleware that checks for the specified permissions.

canhaz.query(permissions, context, next)

Manually query whether a set of permissions are given.

permissions: [String], Permissions to check for.

context: *, Context for role classification.

next: Boolean ->, Callback function.

canhaz.role(name, [inherits], criteria)

Adds a role to the factory instance. If a role of the name already exists, appends the new criteria to it.

name: String, Name of the role.

inherits: [String], Names of prerequisite roles.

criteria: * -> Boolean|*, (Boolean ->) ->, Criteria function that is either synchronous or with a callback.

canhaz.removeRole(name, [inherits], criteria)

Removes a role from the instance.

name: String, Name of the role.

License

The MIT License.