Pretty good middlware privacy
npm install koa-pgp
npm install koa-bodyparser-secure
- Requires a content-type of application/pgp-encrypted with koa-bodyparser-secure installed
- Requires a content-type of application/pgp-encrypted with koa-bodyparser-secure installed
- Requires a PGP-Identifier
'use strict';constAPP=require('koa');constFS=require('fs');constKOAPGP=require('koa-pgp');constPARSER=require('koa-bodyparser-secure');APP.use(PARSER());APP.use(function*(next){this.request.body=':: koa-pgp: starting example ::';console.log(this.request.body);yieldnext;});letcreateFile=function(file_name,data){returnnewPromise(function(resolve,reject){letfile_path='./example_files/'+file_name;FS.writeFile(file_path,data,function(err){if(err){resolve(false);returnconsole.log(err);}else{console.log('Writing file '+file_path);resolve(true);}});});};letreadFile=function(file_path){returnnewPromise(function(resolve,reject){FS.readFile(file_path,'utf8',function(err,data){if(err){throwerr;resolve(false);}else{resolve(data);}});});};APP.use(function*(next){console.log('running next step in co-flow');letctx=this;//instantiate the inheritence of openpgp.jsctx._pgp=ctx._pgp ? ctx._pgp : yieldKOAPGP.init;//options argument for openpgp.js https://github.com/openpgpjs/openpgpjsletoptions={numBits: 2048,userId: 'Jon Smith <jon.smith@example.org>',passphrase: secret};//create the keysletkeys=yieldKOAPGP.createKeys(this._pgp,options);//console.log(keys);letprivate_key=keys.private_key;letpublic_key=keys.public_key;// Write files to local example_keys directoryletcreatePKFile=yieldcreateFile('private.key',private_key);letcreatePubFile=yieldcreateFile('pub.key',public_key);// Passing into scope to show example// ctx.public_key = public_key;// ctx.private_key = private_key;// ctx.passphrase = options.passphrase;//encrypt the messageletmessage=yieldKOAPGP.encrypt(ctx,ctx.request.body,private_key);letcreateMsg=yieldcreateFile('example.msg',message);//setting the body to the encrypted messageyieldnext;});APP.use(function*(next){letctx=this;letencrypted_message=yieldreadFile('./example_files/example.msg');ctx.request.body=encrypted_message;console.log();console.log();console.log('-----------------------ENCRYPTED MESSAGE--------------------');console.log(ctx.request.body);console.log('-----------------------ENCRYPTED MESSAGE--------------------');console.log();console.log();yieldnext;});APP.use(function*(next){letctx=this;ctx._pgp=ctx._pgp ? ctx._pgp : yieldKOAPGP.init();letpk=yieldreadFile('./example_files/private.key');letmessage=yieldKOAPGP.decrypt(ctx,ctx.request.body,pk,secret);//setting the body to the decrypted messagectx.request.body=message;console.log();console.log();console.log('-----------------------DECRYPTED MESSAGE--------------------');console.log(ctx.request.body);console.log('-----------------------DECRYPTED MESSAGE--------------------');console.log();console.log();yieldnext;});APP.use(function*(next){//the decrypted body after the public key has been passed.this.response.status=200;this.response.body=this.request.body;yieldnext;});APP.listen(1988);'use strict';constCONFIG=require('./config');constFS=require('fs');constSECUREPARSER=require('koa-bodyparser-secure');module.exports=function(app,KOAPGP){FS.readFile('./example_files/private.key','utf8',function(err,privkey){if(err){throwerr;}else{FS.readFile('./example_files/pub.key','utf8',function(err,pubkey){if(err){throwerr;}else{// Header required of application/pgp-encryptedapp.use(SECUREPARSER());app.use(KOAPGP.middleware(privkey,CONFIG.secret));app.use(function*(next){console.log(this.request.body);yieldnext;});letinjection={};injection.status=200;app.use(KOAPGP.middleware_out(pubkey,injection))app.listen(1988);}});}});};'use strict';constCONFIG=require('./config');constFS=require('fs');constSECUREPARSER=require('koa-bodyparser-secure');// requires a content-type of application/pgp-encryptedmodule.exports=function(APP,KOAPGP){letretrievePrivateKey=function(){returnnewPromise(function(resolve,reject){try{FS.readFile('./example_files/server_example_pk.key','utf8',function(err,privkey){if(err){throwerr;}else{resolve(privkey);}});}catch(err){console.error(err.stack);resolve(false);}});};// Header Content-Type required of application/pgp-encrypted// Header PGP-Identifier requiredAPP.use(SECUREPARSER());APP.use(function*(next){letctx=this;ctx._pgp=ctx._pgp ? ctx._pgp : yieldKOAPGP.init();ctx._pgp._privateKey=ctx._pgp._privateKey ? ctx._pgp._privateKey : yieldretrievePrivateKey();ctx._pgp._passphrase=ctx._pgp._passphrase ? ctx._pgp._passphrase : CONFIG.secret;yieldnext;});APP.use(KOAPGP.middleware_lookup_pubkey());// Lookups up the key with a designated key server; it's default is set to pgp.mit.eduAPP.use(KOAPGP.middleware());APP.use(function*(next){console.log('decrypted body:',this.request.body);this.response.body={message: 'super secret, wow',code: 200,success: true};console.log('secret reply:',this.response.body);yieldnext;});letinjection={};injection.status=200;APP.use(KOAPGP.middleware_out(null,injection));APP.listen(CONFIG.port);console.log('Starting Koa-PGP Middleware example on port:',CONFIG.port);};See example.js -- injection arguments will soon be added
- middleware(private_key, passphrase, injection)
- middleware_out(public_key, injection)
- middleware_lookup_pubkey(options, promiseFunction)
- options: header_key, hkp_server
- Defaults hkp_server to lookup Public Key at pgp.mit.edu
- Defaults header_key to 'PGP-Identifier' as header parameter expected
- middleware_injection(promiseFunction)
Want to contribute to this repository? Submit a pull request!
- Unit Tests
- A standard to convert PGP messsages to JSON
- TBA
MIT



