Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@voilab/vmol-auth

Package Overview
Dependencies
Maintainers
0
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@voilab/vmol-auth

Authentication and Authorization framework for Moleculer

  • 0.6.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

@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

PropertyTypeDefaultDescription
userJWTParamsActionString'authentication.getJWTParams'The action name to call to get the user JWT params.
appAuthGetPublicKeyActionString'authentication.getApplicationPublicKey'The action name to call to get the public key for application authentication.
jwtClockToleranceNumber30The clock tolerance for JWT verification.
jwtParamsTTLString'1h'The time-to-live for the JWT parameters cache.
applicationKeysCacheSizeNumber128The maximum size of the application keys cache.
applicationKeysCacheTTLString'1d'The time-to-live for the application keys cache.
applicationJwtClockToleranceNumber30The clock tolerance for application JWT verification.
applicationJwtMaxAgeString'30m'The maximum age for application JWT verification.
slugSeparatorString':'The separator for the authentication slug.
serviceBlacklistArray.<String>[]The list of services to skip the authentication middleware. The $node service is always added to the blacklist.

Authorization Middleware

Authorization Configuration

PropertyTypeDefaultDescription
abilitiesForRoleActionString'authorization.getAbilitiesForRole'The action name to call to get the abilities for a role.
defaultAbilitiesActionString'authorization.getDefaultAbilities'The action name to call to get the default abilities.
applicationRolesActionString'authorization.getApplicationRoles'The action name to call to get the roles for an application.
applicationRolesActionParamString'id'The parameter name containing the application ID.
applicationRolesActionSecretStringnullThe secret to send with the request.
userRolesActionString'authorization.getUserRoles'The action name to call to get the roles for a user.
userRolesActionParamString'id'The parameter name containing the user ID.
userRolesActionSecretStringnullThe secret to send with the request.
anonymousRolesActionString'authorization.getAnonymousRoles'The action name to call to get the anonymous roles.
entitiesRolesCacheMaxNumber128The maximum number of entities to cache roles for.
entitiesRolesCacheTTLString'1h'The time-to-live for the entities roles cache.
roleAbilitiesCacheMaxNumber128The maximum number of roles to cache abilities for.
roleAbilitiesCacheTTLString'1h'The time-to-live for the role abilities cache.
compiledAbilitiesCacheMaxNumber128The maximum number of role combinations to cache compiled abilities for.
compiledAbilitiesCacheTTLString'1h'The time-to-live for the compiled abilities cache.
serviceBlacklistArray.<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 (c) 2013-2024, Voilab SNC

Keywords

FAQs

Package last updated on 04 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc