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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@apio/authentication-utils

v1.1.0

Published

A lightweight utility library for handling role-based authorization checks in Apio IoT applications.

Readme

@apio/authentication-utils

A lightweight utility library for handling role-based authorization checks in Apio IoT applications.

Table of Contents

Installation

npm install @apio/authentication-utils

or using yarn:

yarn add @apio/authentication-utils

Usage

This package supports both CommonJS and ES Module imports.

CommonJS

const { hasAuthorization } = require('@apio/authentication-utils');

ES Modules

import { hasAuthorization } from '@apio/authentication-utils';

API Reference

hasAuthorization(wantedAuthorizations, authorizations, projectId)

Checks if a user has the required permissions for a specific project.

Parameters

  • wantedAuthorizations {Array<String>|String} - The permission(s) to check for. Can be a single string or an array of strings.
  • authorizations {Array<Role>} - Array of role objects containing user's permissions for different projects.
  • projectId {String} - The ID of the project to check permissions for.

Returns

  • {Boolean} - Returns true if the user has all the requested permissions, false otherwise.

Authorization System

Permission Format

Permissions follow a dot-notation format, typically structured as:

domain.resource.action

Examples:

  • apio.core.plants.read
  • apio.core.plants.write
  • apio.admin.users.delete

Wildcard Permissions

The system supports wildcard permissions using the * character:

  • apio.core.* - Grants all permissions under apio.core
  • apio.core.plants.* - Grants all actions on plants
  • * - Grants all permissions (superadmin)

Negated Permissions

Permissions can be negated by prefixing them with -. Negated permissions take precedence over positive permissions:

permissions: [
  'apio.core.*',        // Grants all core permissions
  '-apio.core.plants.delete'  // Except deleting plants
]

Examples

Basic Usage

const hasAuthorization = require('@apio/authentication-utils');

const userRoles = [
  {
    projectId: 'project-123',
    permissions: ['apio.core.plants.read', 'apio.core.plants.write']
  },
  {
    projectId: 'project-456',
    permissions: ['apio.core.*', '-apio.core.users.delete']
  }
];

// Check single permission
const canRead = hasAuthorization('apio.core.plants.read', userRoles, 'project-123');
console.log(canRead); // true

// Check multiple permissions
const canManage = hasAuthorization(
  ['apio.core.plants.read', 'apio.core.plants.write'],
  userRoles,
  'project-123'
);
console.log(canManage); // true

// Check permission that doesn't exist
const canDelete = hasAuthorization('apio.core.plants.delete', userRoles, 'project-123');
console.log(canDelete); // false

Wildcard Example

const adminRoles = [
  {
    projectId: 'project-789',
    permissions: ['apio.core.*']  // Has all core permissions
  }
];

Negated Permissions Example

const limitedAdminRoles = [
  {
    projectId: 'project-999',
    permissions: [
      'apio.*',           // All permissions
      '-apio.admin.*',    // Except admin permissions
      '-apio.core.users.delete'  // And cannot delete users
    ]
  }
];

const canDeleteUsers = hasAuthorization(
  'apio.core.users.delete',
  limitedAdminRoles,
  'project-999'
);
console.log(canDeleteUsers); // false

const canReadPlants = hasAuthorization(
  'apio.core.plants.read',
  limitedAdminRoles,
  'project-999'
);
console.log(canReadPlants); // true

Type Definitions

Role Object Structure

interface Role {
  projectId: string;
  permissions: string[];
}

Function Signature

function hasAuthorization(
  wantedAuthorizations: string | string[],
  authorizations: Role[],
  projectId: string
): boolean;

Best Practices

  1. Use specific permissions when possible rather than wildcards for better security control.
  2. Order matters for negated permissions - they always take precedence.
  3. Case-insensitive - All permission checks are case-insensitive for consistency.
  4. Validate inputs - The function filters out non-string permissions automatically.

Error Handling

The function handles edge cases gracefully:

  • Returns false if no role is found for the specified project
  • Filters out invalid (non-string) permissions
  • Handles single string or array input for wantedAuthorizations

Contributing

Contributions are welcome! Please feel free to submit a Pull Request to the GitHub repository.

License

ISC © Apio IoT