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

@voilab/vmol-auth

v0.6.3

Published

Authentication and Authorization framework for Moleculer

Downloads

130

Readme

@voilab/vmol-auth NPM version

This package is part of the vmol collection.

Moleculer middlewares providing authentication and authorization features.

Features

  • JWT based authentication
  • Handle user (interactive), application (machine) and anonymous (public) authentication
  • Casl based authorization
  • Mutli roles and multi tenants support
  • Highly configurable
  • Fully tested

Install

npm install @voilab/vmol-auth --save

Authentication Middleware

Authentication Configuration

| Property | Type | Default | Description | |-|-|-|-| | userJWTParamsAction | String | 'authentication.getJWTParams' | The action name to call to get the user JWT params. | | appAuthGetPublicKeyAction | String | 'authentication.getApplicationPublicKey' | The action name to call to get the public key for application authentication. | | jwtClockTolerance | Number | 30 | The clock tolerance for JWT verification. | | jwtParamsTTL | String | '1h' | The time-to-live for the JWT parameters cache. | | applicationKeysCacheSize | Number | 128 | The maximum size of the application keys cache. | | applicationKeysCacheTTL | String | '1d' | The time-to-live for the application keys cache. | | applicationJwtClockTolerance | Number | 30 | The clock tolerance for application JWT verification. | | applicationJwtMaxAge | String | '30m' | The maximum age for application JWT verification. | | slugSeparator | String | ':' | The separator for the authentication slug. | | serviceBlacklist | Array.<String> | [] | The list of services to skip the authentication middleware. The $node service is always added to the blacklist. |

Authorization Middleware

Authorization Configuration

| Property | Type | Default | Description | |-|-|-|-| | abilitiesForRoleAction | String | 'authorization.getAbilitiesForRole' | The action name to call to get the abilities for a role. | | defaultAbilitiesAction | String | 'authorization.getDefaultAbilities' | The action name to call to get the default abilities. | | applicationRolesAction | String | 'authorization.getApplicationRoles' | The action name to call to get the roles for an application. | | applicationRolesActionParam | String | 'id' | The parameter name containing the application ID. | | applicationRolesActionSecret | String | null | The secret to send with the request. | | userRolesAction | String | 'authorization.getUserRoles' | The action name to call to get the roles for a user. | | userRolesActionParam | String | 'id' | The parameter name containing the user ID. | | userRolesActionSecret | String | null | The secret to send with the request. | | anonymousRolesAction | String | 'authorization.getAnonymousRoles' | The action name to call to get the anonymous roles. | | entitiesRolesCacheMax | Number | 128 | The maximum number of entities to cache roles for. | | entitiesRolesCacheTTL | String | '1h' | The time-to-live for the entities roles cache. | | roleAbilitiesCacheMax | Number | 128 | The maximum number of roles to cache abilities for. | | roleAbilitiesCacheTTL | String | '1h' | The time-to-live for the role abilities cache. | | compiledAbilitiesCacheMax | Number | 128 | The maximum number of role combinations to cache compiled abilities for. | | compiledAbilitiesCacheTTL | String | '1h' | The time-to-live for the compiled abilities cache. | | serviceBlacklist | Array.<String> | [] | The list of services to skip the authorization middleware. The $node service is always added to the blacklist. |

v0.6.0 Migration

Both mxins have been replaced by middlewares.

moleculer.config.js

Require the middlewares and add them to the middlewares array. Take care of the order, the authentication middleware must be called before the authorization middleware. Which means that the authentication middleware must be after the authorization middleware in the array. This because the middleware are called in the reverse order.

+const AuthorizationMiddleware = require('@voilab/vmol-auth').Authorization;
+const AuthenticationMiddleware = require('@voilab/vmol-auth').Authentication;

module.exports = {
    // Register custom middlewares
    middlewares: [
+        AuthorizationMiddleware({
+            userRolesAction: 'myservice.getRolesForUser',
+            userRolesActionSecret: process.env.MY_ACTION_SECRET
+        }),
+        AuthenticationMiddleware({
+            userJWTParamsAction: 'myservice.getJWTParamsPublic'
+        })
    ]
}

Services

The old mixins must be removed from the services.

-const { AuthorizationMixin, AuthenticationMixin } = require('@voilab/vmol-auth');
-
module.exports = {
    name: 'myservice',

-    mixins: [AuthorizationMixin, AuthenticationMixin],
-
    settings: {
        foo: 'bar',
-        auth: {
-            tokenService: 'authservice',
-            abilityService: 'authservice'
-        }
    },

    /**
     * Actions
     */
    actions: {}
};

Public actions

Actions that does not require authentication and/or authorization need the disableAuthentication and/or disableAuthorization options.

module.exports = {
    name: 'myservice',

    settings: {
        foo: 'bar'
    },

    actions: {
        publicAction: {
            disableAuthentication: true,
            disableAuthorization: true,

            handler(ctx) {
                return 'Hello world!';
            }
        }
    }
};

Tests

npm run test

License

This project is under MIT License.

Author

This package has been created by Voilab.

Copyright

Copyright (c) 2013-2024, Voilab SNC