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

t-secure-express

v0.0.10

Published

express middleware & client for RSA encrypted communication

Downloads

100

Readme

tSecureExpress

express middleware & client for RSA encrypted communication

Two modules in one, meant to work together.

A express middleware, that is able to accept RSA encrypted payloads and change the request to the properties found in the body. Second, a secure client, that contacts to a server using the encrypted middleware.

motivation

There are three usecases for this custom http encryption library.

  1. I wanted to have secure encryption between client and server, without being required to register an SSH certificate.
  2. In China, and other places, the https protocol get purposefully slow down. Having the encrypted communication transfer over plain http, will stay fast.
  3. Provide a secure authentication, that not only the application provider can trust, but also the user.
    • the application provider will be able to prove, that commands are send by a particular user.
    • the user can be sure, that no action was taken in his name without his consent. (these two properties are very important or blockchain applications.)

In fact I am on the way, to develop a communication system, that is easy for everyone to setup, participate and use. This module is one step on the way, to allow easy development of RSA encrypted client server communication. It is a perfect fit for PWA and can also be used for communications between servers and other clients.

Using the middlewares, it is also possible to add the encryption module to existing applications and have it work, without starting over.

how it works

Under the hood, is the already provided trsa module, for easy key pair creation and cryptography. The server will generate a keyPair for himself, Clients who send encrypted content to the server using the servers public key. The server key can be provided on a public route, or get shared in any other secure way. (to avoid man in the middle attacks).

The client library, will be configured to the endpoint of the server. The clientside uses the same type of keys. They can even be generated on client side using the trsa module. The client sign the requests to the server. All that will be very easy using the client module.

how to use it.

This library is made to work with express.js. Reading the sourcecode, a developer should also be able to adopt this logic for other frameworks, as it is only about 40 lines of code. a simple server can look like this:

// this like generate a keypair, feel free to read it from a file, 
// so it always stay the same, and can be remembered by your clients
const serverKeys = rsa.generateKeyPair({bits:512});

const app = express();
app.listen(1234);

// the encruption missleware, providing a encryted route at /encrypted
app.use(secureMiddlewares.encryptedMiddleware(serverKeys));

// a second optional middleware, to make sure all request take the same time.
// this is made, so that an attacker can not measure the time spend 
// on encryption or other logic.
app.use(secureMiddlewares.saveResponseMiddleware);

// your actual logic
app.use((req,res)=>{
    // respond with encrypted content,
    // in this case, all the extras, that the `encryptedMiddleware` provide or set
    res.respondSave({
        body: req.body
        encrypted: req.encrypted, // boolean
        method: req.method,
        query: req.query,
        senderKey: req.senderKey, // the clients public key, you can use it for authentication
        url: req.url,
    });
});

The clientside can look like this:

import * as httpClient from '../client/httpClient';

export async function main(){
    /**
     * create a client
     * provide the keyPair with private and public key as first argument
     */
    const client = new httpClient.RsaFetch({keyPair: clientKeys, url:window.location.origin+'/encrypted', serverKey });
    // simple GET request to the server
    const requestInfo = await client.fetch('/hello');
    console.log(client)
    /**
     * post some data
     * note: all options will be JSON stringified. so you don't need to stringify the body.
     */
    await client.fetch({
        method: 'POST',
        url: '/someEntity',
        headers: {'your-other':'headers'} // note: they do not get encrypted and signed
        body: {da: 'ta'}
    });
}

This two examples should show you something great. Setting url, query, body and method, the client has total control, to send requests as before. And also your app, is able to work just as before, with almost no changes.

import

For the serverside, you simply require('t-secure-express');, for clients, using webpack, you can use require('t-secure-express/client/httpClient');. of course, modern syntax with import works the same way.

The client SDK is also build

<script src="https://unpkg.com/[email protected]/client/webClient.js"></script>
<script>
  const clientKeys = secureClient.rsa.generateKeyPair({bits:512});
  const client = new httpClient.RsaFetch({keyPair: clientKeys, url:window.location.origin+'/encrypted', serverKey });
  client.fetch({
    method: 'POST',
    url: '/someEntity',
    headers: {'your-other':'headers'} // note: they do not get encrypted and signed
    body: {da: 'ta'},
  });

</script>