@lindorm-io/koa-keystore
v0.11.12
Published
Keystore middleware for @lindorm-io/koa applications
Downloads
28
Readme
@lindorm-io/koa-keystore
Keystore middleware for @lindorm-io/koa applications.
Installation
npm install --save @lindorm-io/koa-keystore
Peer Dependencies
This package has the following peer dependencies:
Usage
You will need a middleware that sets keys on context. You can add multiple middleware to add multiple key sources. They will flatten to one array. Once you are done, you will need to initialise the keystore. Add this middleware after all keys have been set.
Keys
Use one or many of these strategies to add keys to context.
JWKS
koaApp.addMiddleware(jwksKeysMiddleware);
Repository Keys
koaApp.addMiddleware(repositoryMiddleware(KeyPairRepository)); // from koa-mongo
koaApp.addMiddleware(repositoryKeysMiddleware);
Cached Repository Keys
koaApp.addWorker(
keyPairMongoCacheWorker({
mongoConnection, // not required if mongoConnectionOptions is set
mongoConnectionOptions: {
auth: { user: "root", password: "example" },
databaseName: "database",
hostname: "mongo.host",
port: 27000,
}, // not required if mongoConnection is set
resisConnection, // not required if redisConnectionOptions is set
redisConnectionOptions: {
port: 1000,
type: RedisConnectionType.CACHE,
}, // not required if redisConnection is set
winston: winstonLogger,
workerInterval: "1 hours",
}),
);
koaApp.addMiddleware(cacheMiddleware(KeyPairCache)); // from koa-redis
koaApp.addMiddleware(cacheKeysMiddleware);
Cached JWKS
koaApp.addWorker(
keyPairJwksCacheWorker({
baseUrl: "https://authentication.service",
clientName: "Authentication",
resisConnection, // not required if redisConnectionOptions is set
redisConnectionOptions: {
port: 1000,
type: RedisConnectionType.CACHE,
}, // not required if redisConnection is set
winston: winstonLogger,
workerInterval: "5 minutes",
}),
);
koaApp.addMiddleware(cacheMiddleware(KeyPairCache)); // from koa-redis
koaApp.addMiddleware(cacheKeysMiddleware);
Keystore
koaApp.addMiddleware(keystoreMiddleware);
Rotation
If you want a worker to handle key rotation automatically, you can let this worker generate keys.
koaApp.addWorker(
keyPairRotationWorker({
keyType: KeyType.EC, // optional
mongoConnection, // not required if mongoConnectionOptions is set
mongoConnectionOptions: {
auth: { user: "root", password: "example" },
databaseName: "database",
hostname: "mongo.host",
port: 27000,
}, // not required if mongoConnection is set
namedCurve: NamedCurve.P521, // optional
passphrase: "passphrase", // optional
rotationInterval: "90 days", // optional
winston: winstonLogger,
workerInterval: "1 days",
}),
);
Cleanup
If you're using key-pairs from repository, you should leave a worker running to clean up expired keys.
koaApp.addWorker(
keyPairCleanupWorker({
mongoConnection, // not required if mongoConnectionOptions is set
mongoConnectionOptions: {
auth: { user: "root", password: "example" },
databaseName: "database",
hostname: "mongo.host",
port: 27000,
}, // not required if mongoConnection is set
winston: winstonLogger,
workerInterval: "1 days",
}),
);