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

nest-acl-sdk

v1.0.2

Published

Nestjs Access Control SDK

Downloads

3

Readme

Description

ACL SDK module for Nest. It provides some decorator to define role/permissions for API endpoints base on metadata, and help to automated collect policies of endpoints of microservices then send to api-iam service if need.

Noted:

Nest ACL SDK and Nestjs swagger should be used together!

Installation

$ npm i --save nest-acl-sdk

Usage

ACL SDK provides 2 strategies:

  • RBAC: Role based access control
  • CBAC: Claim based access control (permissions)

Use synchronous config

import {ACLModule, ACLStrategy, ACLModuleOptions, ACLService} from 'nest-acl-sd';
@Module({
  imports: [
    ...
    DiscoveryModule,
    ACLModule.register({
      strategy: ACLStrategy.RBAC,
      serviceBaseUrl: '/api',
      serviceName: 'test-api',
      apiIamBaseUrl: 'api-iam/api/',
      iamChecksumPoliciesUrl: '/policies/',
      iamUpdatePoliciesUrl: '/policies',
      logProcess: true,
      global: true,
    })
  ],
  providers: [...],
})
export class AppModule implements OnApplicationBootstrap {
  constructor(private aclService: ACLService) {}

  async onApplicationBootstrap() {
    const this.aclService: ACLService = this.get<ACLService>(ACLService);
    await this.aclService.updateEndpointPolicies();
    console.log('policies', aclService.getPoliciesData());
  }
}

use with asynchronous config

import {ACLModule, ACLStrategy, ACLModuleOptions, ACLService} from 'nest-acl-sd';
@Module({
  imports: [
    ...
    DiscoveryModule,
    ACLModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          strategy: ACLStrategy.CBAC,
          serviceBaseUrl: config.get<string>('service.baseUrl'),
          serviceName: config.get<string>('service.name'),
          apiIamBaseUrl: config.get<string>('aclSDK.iamBaseUrl'),
          iamChecksumPoliciesUrl: config.get<string>('aclSDK.iamChecksumPoliciesUrl'),
          iamUpdatePoliciesUrl: config.get<string>('aclSDK.iamUpdatePoliciesUrl'),
          logProcess: config.get<boolean>('aclSDK.logProcess'),
          global: true,
        } as ACLModuleOptions;
      }
    })
  ],
  providers: [...],
})
export class AppModule implements OnApplicationBootstrap {
  constructor(private aclService: ACLService) {}

  async onApplicationBootstrap() {
    const aclService: ACLService = this.get<ACLService>(ACLService);
     await aclService.updateEndpointPolicies();
    console.log('policies', aclService.getPoliciesData());
  }
}

If use Claim based access control strategy (CBAC) you can use @Permissions decorator to define policies of endpoint in controller class

import {Controller, Get, Post} from '@nestjs/common';
import {ApiOperation} from '@nestjs/swagger';
import {Permissions} from '../decorators/permission.decorator';

@Controller('test-cbac')
export class TestCbacController {

  @ApiOperation({
    operationId: 'getAbc',
    description: 'Get abc',
  })
  @Permissions(['service_get_abc'])
  @Get('abc')
  async getAbc() {
    return 'abc';
  }

  @ApiOperation({
    operationId: 'getCde',
    description: 'Get cde',
  })
  @Permissions(['service_get_abc', 'index_abc'])
  @Get('cde')
  async getCde() {
    return 'cde';
  }
}

If use Role based access control strategy (RBAC) you can use @Roles decorator to define policies of endpoint in controller class

import {Controller, Get, Post} from '@nestjs/common';
import {ApiOperation} from '@nestjs/swagger';
import {Roles} from '../decorators/roles.decorator';

@Controller('test-rbac')
export class TestRbacController {

  @ApiOperation({
    operationId: 'getAbc',
    description: 'Get abc',
  })
  @Roles(['admin'])
  @Get('abc')
  async getAbc() {
    return 'abc';
  }

  @ApiOperation({
    operationId: 'getCde',
    description: 'Get cde',
  })
  @Roles(['customers', 'admin'])
  @Get('cde')
  async getCde() {
    return 'cde';
  }
}

Collect and update endpoint policies

in AppModule we implements OnApplicationBoostrap event, we call function updateEndpointPolicies(), the ACL service automated collect all defined roles/permissions on controllers then check it with IAM service by checksum to decide policies need to update or not.

ACLModuleOptions:

  • strategy: Enum - ACL SDK provides 2 options: ACLStrategy.CBAC and ACLStrategy.RBAC
  • global: boolean - Allow this ACL SDK module is an global module
  • serviceBaseUrl: string - is global prefix URL of your service,
  • serviceName: string - is your service's name.
  • apiIamBaseUrl: string - The base URL of IAM Service
  • iamChecksumPoliciesUrl: string | Function - The define of IAM service endpoint URL to get MD5 checksum about service's policies from IAM service to decide update policies or not (by HTTP Get method)
  • iamUpdatePoliciesUrl: string | Function - The define of IAM service endpoint URL to update policies (by HTTP POST method).
  • logProcess: boolean - enable/disable logging debug when processing collect and update policies.

License

Nest is MIT licensed.