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

@laqus/laqus.auth.js

v1.0.9

Published

---

Downloads

447

Readme

laqus.auth.js


Uma lib criada para facilitar a implementação de autenticação e autorização em aplicações Nest.js

Features:

  • AuthGuard de autenticação para ser usado globalmente
  • Decorator para declarar rotas publicas
  • Serviço de contexto (para recuperar as infos do usuário + correlationId)
  • AuthGuard para proteger rotas de acordo com roles do keycloak
  • Serviço para gerar o token da aplicação no keycloak (client session)

Instalação


Para instalar a lib no projeto:

npm i @laqus/laqus.auth.js

Adicionalmente, é necessário incluir o segmento "Keycloak" no .ENV (contatar a malawi para conseguir as variáveis de sandbox, hml e prod):

KEYCLOAK_PUBLIC_KEY=
KEYCLOAK_REALM=
KEYCLOAK_CLIENT_ID=
KEYCLOAK_CLIENT_SECRET=
KEYCLOAK_AUTH_SERVER_URL=

Para habilitar cada feature, são necessários steps diferentes:

AuthGuards

AuthGuard global

No app.module, ou no modulo base da sua app que é instanciado no main.ts, inclua o LaqusAuthGuard como provider:

providers: [
    {
    provide: 'APP_GUARD',
    useClass: LaqusAuthGuard,
    },
],

Para declarar rotas publicas, é necessario usar esse decorator:

@Controller()
export class AppController {

    @Get('health')
    @HttpCode(200)
    @Public() // decorator para rotas publicas
    healthCheck(): any {
        return Math.random();
    }
}

Para o swagger aceitar o novo bearer, é necessario adicionar esses trechos no main.ts e em cada controller

// main.ts
const config = new DocumentBuilder()
    .setTitle('')
    .setDescription('')
    .setVersion('1.0')
    + .addBearerAuth(
    +     {
    +     type: 'http',
    +     scheme: 'bearer',
    +     bearerFormat: 'JWT',
    +     name: 'auth',
    +     description: 'Enter JWT token',
    +     in: 'header',
    +     },
    +     'auth',
    + )
    .build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);

//controller
@ApiTags('')
@Controller('')
+ @ApiBearerAuth('auth')
export class TemplateController {
constructor(
) {}

    @Post()
    async create() {
    }
//...

Serviço de contexto e gerador de client sessions:

É necessário apenas incluir os serviços no modulo:

@Module({
imports: [FilesModule, TypeOrmModule.forFeature([MyEntity])],
controllers: [MyController],
providers: [
    MyService,
    MyRepository,
    + LaqusContextService,
    + LaqusKeyCloakService,
],
exports: [MyService],
})
export class MyModule {}

Authuard baseado em roles

Para usar, é necessário passar uma lista de roles:

@UseGuards(new LaqusRoleGuard(['CONSULTOR_LEITURA']))
@Get()
async findAll() {}