0.6.3 • Published 8 months ago

@voilab/vmol-auth v0.6.3

Weekly downloads
38
License
ISC
Repository
bitbucket
Last release
8 months ago

@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

Copyright (c) 2013-2024, Voilab SNC

0.6.3

8 months ago

0.6.2

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.6.0-beta.7

1 year ago

0.6.0-beta.6

1 year ago

0.6.0-beta.4

1 year ago

0.6.0-beta.5

1 year ago

0.6.0-beta.3

1 year ago

0.6.0-beta.2

1 year ago

0.6.0-beta.1

1 year ago

0.5.0

2 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.4

4 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago