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

simple-service-authorizer

v1.5.1

Published

A simple package that centralize authorization between applications

Downloads

10

Readme

simple-service-authorizer

This package is a encapsulated tool that uses JWT to make simple validation in communication between internal services/applications based on a shared private secret key between them and service names.

This is a simple tool, so it is not a complete security for service/applications communication. Ensure you have a complete security communication approach between you services, such as:

  • Making services/applications communication always using SSL/TLS (example: HTTPS)
  • Limiting internal services/applications to be communicated always over internal network
  • Setting expiration time for generated tokens to avoid replay attacks
  • Keeping secret key as hide as possible
  • Rotating/changing secret key (and updating it in used services/applications) frequently

Usage

The tool is based in two use cases:

  • Generating token for requester service.
  • Validating token on requested service.

Generating token from requester service

import { SimpleServiceTokenGenerator } from "simple-service-authorizer";

const SERVICE_NAME = 'service-a';
const SECRET_SST_KEY = process.env.SECRET_SST; // in this example we are getting from env var

const simpleServiceTokenGenerator = new SimpleServiceTokenGenerator({
    secretWord: SECRET_SST_KEY,
    serviceName: SERVICE_NAME,
});

const token = simpleServiceTokenGenerator.generate(20); // 20 is the token expiration time in seconds, default is 30 (if not defined)

// now we can request other service with the token. In this example we will use HTTP

const http = require('http');

const options = {
    hostname: 'internal-service-b.com',
    path: '/get-resources',
    method: 'GET',
    headers: {
        'sst-token': `${token}`,
        'sst-service-name': '${SERVICE_NAME}',
    },
};

response = await http.request(options);

Validating token on requested service.

import { SimpleServiceTokenValidator } from "simple-service-authorizer";

const SECRET_SST_KEY = process.env.SECRET_SST; // in this example we are getting from env var

const simpleServiceTokenValidator = new SimpleServiceTokenValidator({
    secretWord: SECRET_SST_KEY,
    // services' names that are allowed to request this service, with undefinition on this, all service-names will be accept
    allowedServiceNames: ['service-a'] 
});

// now we can validate the requests' tokens received. In this example we are creating a middleware for Node.js http requests

const serviceRequestValidationMiddleware = (req, res, next) => {
  const requestorServiceName  = req.headers['sst-service-name'];
  const requestorToken        = req.headers['sst-token'];

  // validation method usage
  const isServiceTokenValid = simpleServiceTokenValidator.validate(
    requestorServiceName,
    requestorToken,
  );

  if (!isServiceTokenValid) {
    res.writeHead(403, { 'Content-Type': 'text/plain' });
    res.end('Service request not authorized!');
  } else {
    console.log('Middleware: Request accepted!');
    next();
  }
};

Development

Development instructions

License

License: MIT

Stay in touch

For questions or inquiries, please contact Thauã Silveira at [email protected].