Cleanup REST api and use 'op' query param for verbs
This commit is contained in:
16
src/app.js
16
src/app.js
@@ -49,21 +49,7 @@ router.post('/api/v1/key', function *() {
|
||||
yield rest.create(this);
|
||||
});
|
||||
router.get('/api/v1/key', function *() {
|
||||
yield rest.read(this);
|
||||
});
|
||||
router.del('/api/v1/key', function *() {
|
||||
yield rest.remove(this);
|
||||
});
|
||||
|
||||
// links for verification, removal and sharing
|
||||
router.get('/api/v1/verify', function *() {
|
||||
yield rest.verify(this);
|
||||
});
|
||||
router.get('/api/v1/removeKey', function *() {
|
||||
yield rest.remove(this);
|
||||
});
|
||||
router.get('/api/v1/verifyRemove', function *() {
|
||||
yield rest.verifyRemove(this);
|
||||
yield rest.query(this);
|
||||
});
|
||||
|
||||
// Redirect all http traffic to https
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"verifyKey": {
|
||||
"subject": "Verify Your Key",
|
||||
"text": "Hello {{name}},\n\nplease click here to verify your key:\n\n{{baseUrl}}/api/v1/verify?keyId={{keyId}}&nonce={{nonce}}",
|
||||
"html": "<p>Hello {{name}},</p><p>please <a href=\"{{baseUrl}}/api/v1/verify?keyId={{keyId}}&nonce={{nonce}}\">click here to verify</a> your key.</p>"
|
||||
"text": "Hello {{name}},\n\nplease click here to verify your key:\n\n{{baseUrl}}/api/v1/key?op=verify&keyId={{keyId}}&nonce={{nonce}}",
|
||||
"html": "<p>Hello {{name}},</p><p>please <a href=\"{{baseUrl}}/api/v1/key?op=verify&keyId={{keyId}}&nonce={{nonce}}\">click here to verify</a> your key.</p>"
|
||||
},
|
||||
"verifyRemove": {
|
||||
"subject": "Verify Key Removal",
|
||||
"text": "Hello {{name}},\n\nplease click here to verify the removal of your key:\n\n{{baseUrl}}/api/v1/verifyRemove?keyId={{keyId}}&nonce={{nonce}}",
|
||||
"html": "<p>Hello {{name}},</p><p>please <a href=\"{{baseUrl}}/api/v1/verifyRemove?keyId={{keyId}}&nonce={{nonce}}\">click here to verify</a> the removal of your key.</p>"
|
||||
"text": "Hello {{name}},\n\nplease click here to verify the removal of your key:\n\n{{baseUrl}}/api/v1/key?op=verifyRemove&keyId={{keyId}}&nonce={{nonce}}",
|
||||
"html": "<p>Hello {{name}},</p><p>please <a href=\"{{baseUrl}}/api/v1/key?op=verifyRemove&keyId={{keyId}}&nonce={{nonce}}\">click here to verify</a> the removal of your key.</p>"
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,23 @@ class REST {
|
||||
ctx.status = 201;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public key query via http GET
|
||||
* @param {Object} ctx The koa request/response context
|
||||
*/
|
||||
*query(ctx) {
|
||||
let op = ctx.query.op;
|
||||
if (this[op]) {
|
||||
return yield this[op](ctx); // delegate operation
|
||||
}
|
||||
// do READ if no 'op' provided
|
||||
let q = { keyId:ctx.query.keyId, fingerprint:ctx.query.fingerprint, email:ctx.query.email };
|
||||
if (!util.isKeyId(q.keyId) && !util.isFingerPrint(q.fingerprint) && !util.isEmail(q.email)) {
|
||||
ctx.throw(400, 'Invalid request!');
|
||||
}
|
||||
ctx.body = yield this._publicKey.get(q);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify a public key's user id via http GET
|
||||
* @param {Object} ctx The koa request/response context
|
||||
@@ -66,18 +83,6 @@ class REST {
|
||||
ctx.set('Content-Type', 'text/html; charset=utf-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Public key fetch via http GET
|
||||
* @param {Object} ctx The koa request/response context
|
||||
*/
|
||||
*read(ctx) {
|
||||
let q = { keyId:ctx.query.keyId, fingerprint:ctx.query.fingerprint, email:ctx.query.email };
|
||||
if (!util.isKeyId(q.keyId) && !util.isFingerPrint(q.fingerprint) && !util.isEmail(q.email)) {
|
||||
ctx.throw(400, 'Invalid request!');
|
||||
}
|
||||
ctx.body = yield this._publicKey.get(q);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request public key removal via http DELETE
|
||||
* @param {Object} ctx The koa request/response context
|
||||
|
||||
@@ -53,7 +53,8 @@
|
||||
|
||||
<div class="col-lg-12">
|
||||
<h2>OpenPGP key removal</h2>
|
||||
<form action="/api/v1/removeKey" method="get">
|
||||
<form action="/api/v1/key" method="get">
|
||||
<input class="hidden" type="radio" name="op" value="remove" checked="checked">
|
||||
<div class="input-group input-group-lg">
|
||||
<input class="form-control" name="email" type="email" spellcheck="false" placeholder="Email address" required>
|
||||
<span class="input-group-btn">
|
||||
|
||||
Reference in New Issue
Block a user