This repository has been archived by the owner on Apr 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
154 lines (116 loc) · 3.79 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
/**
@author: Peter A. Tariche <ptariche@gmail.com>
@fingerprint: 599F 6D20 2806 C23E 6776 F17C 49D6 EE01 6556 E77F
@license: MIT
**/
const FS = require('fs');
const KOA = require('koa');
const PARSER = require('koa-bodyPARSER-secure');
const KOAPGP = require('./index.js');
const CONFIG = require('./example_files/CONFIG.js');
const SECRET = CONFIG.SECRET;
const APP = KOA();
/**
APP.use(PARSER());
APP.use(function *(next) {
this.request.body = ':: koa-pgp: starting example ::';
console.log(this.request.body);
yield next;
});
let createFile = function (file_name, data) {
return new Promise( function (resolve,reject) {
let file_path = './example_files/' + file_name;
FS.writeFile(file_path, data, function (err) {
if (err) {
resolve(false);
return console.log(err);
} else {
console.log('Writing file ' + file_path);
resolve(true);
}
});
});
};
let readFile = function (file_path) {
return new Promise( function (resolve, reject) {
FS.readFile(file_path, 'utf8', function (err, data) {
if (err) {
throw err;
resolve(false);
} else {
resolve(data);
}
});
});
};
APP.use(function *(next) {
console.log('running next step in co-flow');
let ctx = this;
//instantiate the inheritence of openpgp.js
ctx._pgp = ctx._pgp ? ctx._pgp : yield KOAPGP.init;
//options argument for openpgp.js https://github.com/openpgpjs/openpgpjs
let options = {
numBits: 2048,
userId: 'Jon Smith <jon.smith@example.org>',
passphrase: SECRET
};
//create the keys
let keys = yield KOAPGP.createKeys(this._pgp, options);
//console.log(keys);
let private_key = keys.private_key;
let public_key = keys.public_key;
// Write files to local example_keys directory
let createPKFile = yield createFile('private.key', private_key);
let createPubFile = yield createFile('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 message
let message = yield KOAPGP.encrypt(ctx, ctx.request.body, private_key);
let createMsg = yield createFile('example.msg', message);
//setting the body to the encrypted message
yield next;
});
APP.use(function *(next) {
let ctx = this;
let encrypted_message = yield readFile('./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();
yield next;
});
APP.use(function *(next) {
let ctx = this;
ctx._pgp = ctx._pgp ? ctx._pgp : yield KOAPGP.init;
let pk = yield readFile('./example_files/private.key');
let message = yield KOAPGP.decrypt(ctx, ctx.request.body, pk, SECRET);
//setting the body to the decrypted message
ctx.request.body = message;
console.log();
console.log();
console.log('-----------------------DECRYPTED MESSAGE--------------------');
console.log(ctx.request.body);
console.log('-----------------------DECRYPTED MESSAGE--------------------');
console.log();
console.log();
yield next;
});
APP.use(function *(next) {
//the decrypted body after the public key has been passed.
this.response.status = 200;
this.response.body = this.request.body;
yield next;
});
APP.listen(1988);
**/
/** USE AS MIDDLEWARE **/
// Header required of application/pgp-encrypted and npm install koa-bodyPARSER-secure
//require('./example_files/middleware_basic.js')(APP, KOAPGP);
require('./example_files/middleware_advanced')(APP, KOAPGP);