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

loveboat-nested-scopes

v1.0.0

Published

support nested auth scopes in hapi

Downloads

8

Readme

loveboat-nested-scopes

support nested auth scopes in hapi

(a transform written for loveboat)

Build Status Coverage Status

Usage

This loveboat transform allows you to define hierarchical auth scopes on routes and to leverage that hierarchy when writing your route configurations. The core idea is that allowing unprivileged scopes on a route should also permit the higher-privileged scopes.

Imagine a basic-user scope and an admin scope. Every admin is naturally allowed to perform the actions of a basic user. So let's make our app aware of that and start writing this,

// Scopes can be smart!
server.loveboat({
    method: 'get',
    path: '/my/file.gif',
    handler: handler,
    config: {
        auth: {
            access: {
                scope: ['[basic-user]']
                // Becomes ['basic-user', 'admin']
            }
        }
    }
});

To use this transform,

  1. Make sure the loveboat hapi plugin is registered to your server.

  2. Tell loveboat that you'd like to use this transform by calling,

    server.routeTransforms([{
        transform: require('loveboat-nested-scopes'),
        options: {
            scope: 'admin',
            subscopes: ['basic-user']
            // Define your nested scopes
        }
    }]);

    and possibly listing any other transforms you'd like to use.*

  3. Register your routes using server.loveboat() rather than server.route().

* There are other ways to tell loveboat which transforms to use too! Just check-out the readme.

const Hapi = require('hapi');
const Loveboat = require('loveboat');

const server = new Hapi.Server();
server.connection();

// 1. Register loveboat
server.register(Loveboat, (err) => {

    // 2. Tell loveboat to use this transform, providing info about your nested scopes
    server.routeTransforms([{
        transform: require('loveboat-nested-scopes'),
        options: {
            scope: 'admin',
            subscopes: [
                {
                    scope: 'super-user',
                    subscopes: [
                        'api-user',
                        'files-user'
                    ]
                }
            ]
        }
    }]);

    // 3. Configure your routes!
    server.loveboat({
        method: 'get',
        path: '/my/file.gif',
        handler: handler,
        config: {
            auth: {
                access: {
                    scope: ['[files-user]']
                    /*
                     * Use '[]' to indicate that the
                     * scope should be expanded.
                     *
                     * Scope becomes effectively,
                     * [
                     *    'files-user',
                     *    'super-user',
                     *    'admin'
                     * ]
                     */
                }
            }
        }
    });

    // The route above will allow users with scope 'files-user'
    // as well as that scope's parents, 'super-user' and 'admin'
});

API

Options

When the transform is registered, options should be specified to define the hierarchy of your scopes. Options take the form of an item or array of items, each of which is of the format,

  • a string naming a scope, or
  • an object of the format,
    • scope - a string naming a scope.
    • subscopes - (optional) an item or array of items as defined here and above. These items specify scopes that are considered "included" by the scope named in scope.

The same scope cannot be listed twice.

You'll notice that this leads to options that can be arbitrarily deep, since the subscopes parameter may in turn contain an array of objects with their own subscopes parameters. This options object defines a hierarchical tree of scopes. For example, see the following options and scope hierarchy,

/*
 *            admin
 *              |
 *         super-user
 *        /          \
 *       /            \
 *   api-user     files-user
 *
 * An admin is a super-user, api-user and files-user.
 * A super-user is an api-user and files-user.
 */

const scopeHierarchy = [{
    scope: 'admin',
    subscopes: [
        {
            scope: 'super-user',
            subscopes: [
                'api-user',
                'files-user'
            ]
        }
    ]
}];

server.routeTransforms([{
    transform: require('loveboat-nested-scopes'),
    options: scopeHierarchy
}]);

// ...

Route Definition

  • config.auth.access - The standard route config.auth.access configurations are all fully supported by this transformer. Additionally, when a scope is specified with brackets [] around it, it is expanded based upon the scope hierarchy defined in the transform options. The behavior here depends on the scope modifier (+, !, or none),
    • No modifier - the scope '[scope-name]' is expanded to include itself (scope-name) or any of the scopes up the scope hierarchy.
    • Forbidden (!) - the scope '[!scope-name]' is expanded to forbid itself and all scopes up the scope hierarchy.
    • Required (+) - the scope '[+scope-name]' is expanded to require itself and all scopes down the scope hierarchy.