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

hapi-field-auth

v1.0.12

Published

Hapi plug-in for field-level authorization

Downloads

51

Readme

hapi-field-auth

Hapi server plugin for field-level authorization.

main workflow Coverage Status Dependencies Status Maintainability node code style License Status

Tested with

  • Node 14/16, Hapi 18/19/20, Joi 17
  • Node 10, Hapi 18, Joi 16

Install

npm install hapi-field-auth

Purpose

This plugin provides field-level authorization (not authentication) for Hapi routes -- particularly useful for PATCH routes. If the request payload has fields with special constraints in respect to the scope of the authenticated user, this plugin allows restricting access on field-level and adding field validation depending on the scope.

A prerequisite is authentication. Use any authentication plugin, e.g., hapi-auth-basic or hapi-auth-bearer-token. The authentication plugin must properly set request.auth.credentials.scope with the authenticated user's scope for this plugin to work.

Dynamic scopes referring to the request object (query, params, payload, and credentials) are supported, e.g., user-{params.id}. Prefix characters ! and + are not (yet) supported.

Usage

Register the plugin with Hapi server like this:

const Hapi = require('@hapi/hapi');
const hapiAuthBasic = require('@hapi/basic');
const hapiFieldAuth = require('hapi-field-auth');

const server = new Hapi.Server({
  port: 3000,
});

const provision = async () => {
  await server.register([hapiAuthBasic, hapiFieldAuth]);
  // ...
  await server.start();
};

provision();

Your route configuration may look like this:

server.route({
  method: 'PATCH',
  path: '/example',
  options: {
    auth: {
      access: { // route-level auth -> HTTP 401/403
        scope: ['write', 'write.extended'], // multiple scopes on route-level
      },
    },
    validate: {
      payload: ExampleSchema, // Joi schema validation -> HTTP 400
    },
    plugins: {
      'hapi-field-auth': [{ // add field-level authorization -> HTTP 403
        fields: ['myProtectedField'], // request payload properties
        scope: ['write.extended'], // restricted scopes on field-level
      }, {
        fields: ['activeUntil', 'validUntil'],
        scope: ['write.extended'], // restricted scopes on field-level...
        validate: Joi.date().min('now').allow(null), // ...OR additional Joi schema -> HTTP 400
      }],
    },
  },
  handler: function (request, h) {
    // ...
  }
});