It's middleware for nodejs backend servers! Allow to simply implement information encoding. Using as core virgil-crypto lib!
- Install virgil-crypto version 5.0.2 or newest
- Install @virgilsecurity/data-utils version 2.0.0 or newest
After this initialize class instance
import { ZtMiddleware } from 'build';
const virgil = new ZtMiddleware(KeyPairType.ED25519, '/login');
KeyPairType.ED25519
Argument it's key type, you can check other values on KeyPairType enum
/login
It's login url, that will accept your frontend key
You can pass more than this two parameters:
- Storage interface function
storageControl
- Encoding default value is
base64
Let's see a full elements that ypu can pass into constructor
const TemplateStorage: Map<string, any> = new Map<string, any>();
const storageSave = (key: unknown, isClient: boolean) => {
TemplateStorage.set(isClient ? 'client' : 'server', key);
};
const storageLoad = (isClient: boolean) => {
return isClient ? TemplateStorage.get('client') : TemplateStorage.get('server');
};
function storage(isSave: boolean, isClient: boolean, key?: unknown) {
if (isSave) {
storageSave(key, isClient);
return;
}
return storageLoad(isClient);
}
const virgil = new ZtMiddleware(KeyPairType.ED25519, '/login', storage, 'base64');
The storage function is interface for working with files or another save instrument. Upper we realize simple storage, to collect server and clients keys. We are saving this in TypeScript Map, then convert in .json file after server broke. When server is up again we can load this keys and using them. You can create your own storage logic, all that you need is create function like:
storage(isSave: boolean, isClient: boolean, key?: unknown)
Then pass it inside constructor, we will save keys and get it, if needed.
app.use(virgil.zeroTrustMiddleware);
After this, on frontend you need to pass keys on login url. Save key on frontend and that's all. Encryption is ready for use!
For more information you can check example/backend and example/frontend to see how it's working.