|
| 1 | +import * as helper from './helper.js'; |
| 2 | +import Fastify from 'fastify'; |
| 3 | + |
| 4 | +const bodyLimit = parseInt(process.env.BODYLIMIT); |
| 5 | +const fieldLimit = parseInt(process.env.FIELDLIMIT); |
| 6 | + |
| 7 | +// Impose content-length limit |
| 8 | +const fastify = Fastify({ |
| 9 | + ignoreTrailingSlash: true, |
| 10 | + bodyLimit: bodyLimit |
| 11 | +}) |
| 12 | + |
| 13 | +// Enable CORS. This implementation is lightweight than importing '@fastify/cors' |
| 14 | +fastify.addHook('onRequest', (request, reply, done) => { |
| 15 | + reply.headers({ |
| 16 | + 'Access-Control-Allow-Origin': '*', |
| 17 | + 'Access-Control-Allow-Methods': 'GET,POST' |
| 18 | + }) |
| 19 | + done(); |
| 20 | +}) |
| 21 | + |
| 22 | +// Enable parser for application/x-www-form-urlencoded |
| 23 | +fastify.register(import('@fastify/formbody')) |
| 24 | + |
| 25 | +const callUnauthorized = function(reply, msg){ |
| 26 | + reply.code(401).send({message: msg, error: "Unauthorized", statusCode: reply.statusCode}); |
| 27 | +} |
| 28 | + |
| 29 | +const callBadRequest = function(reply, msg){ |
| 30 | + reply.code(400).send({message: msg, error: "Bad Request", statusCode: reply.statusCode}); |
| 31 | +} |
| 32 | + |
| 33 | +const callInternalServerError = function(reply, msg){ |
| 34 | + reply.code(500).send({message: msg, error: "Internal Server Error", statusCode: reply.statusCode}); |
| 35 | +} |
| 36 | + |
| 37 | +fastify.get('/', (request, reply) => { |
| 38 | + reply.redirect('https://securelay.github.io', 301); |
| 39 | +}) |
| 40 | + |
| 41 | +fastify.post('/', (request, reply) => { |
| 42 | + reply.redirect('https://securelay.github.io', 301); |
| 43 | +}) |
| 44 | + |
| 45 | +fastify.get('/keys', (request, reply) => { |
| 46 | + reply.send(helper.genKeyPair()); |
| 47 | +}) |
| 48 | + |
| 49 | +fastify.get('/keys/:key', (request, reply) => { |
| 50 | + const { key } = request.params; |
| 51 | + const keyType = helper.validate(key); |
| 52 | + if (keyType === 'public') { |
| 53 | + reply.send({type: "public"}); |
| 54 | + } else if (keyType === 'private') { |
| 55 | + reply.send({type: "private", public: helper.genPublicKey(key)}); |
| 56 | + } else { |
| 57 | + reply.callNotFound(); |
| 58 | + } |
| 59 | +}) |
| 60 | + |
| 61 | +fastify.post('/public/:publicKey', async (request, reply) => { |
| 62 | + const { publicKey } = request.params; |
| 63 | + try { |
| 64 | + if (helper.validate(publicKey) !== 'public') throw 401; |
| 65 | + await helper.publicProduce(publicKey, JSON.stringify(request.body)); |
| 66 | + reply.send({message: "Done", error: "Ok", statusCode: reply.statusCode}); |
| 67 | + } catch (err) { |
| 68 | + if (err == 401) { |
| 69 | + callUnauthorized(reply, 'Provided key is not Public'); |
| 70 | + } else { |
| 71 | + callInternalServerError(reply, err); |
| 72 | + } |
| 73 | + } |
| 74 | +}) |
| 75 | + |
| 76 | +fastify.get('/private/:privateKey', async (request, reply) => { |
| 77 | + const { privateKey } = request.params; |
| 78 | + try { |
| 79 | + if (helper.validate(privateKey) !== 'private') throw 401; |
| 80 | + const dataArray = await helper.privateConsume(privateKey); |
| 81 | + if (!dataArray.length) throw 404; |
| 82 | + reply.send(dataArray); |
| 83 | + } catch (err) { |
| 84 | + if (err == 401) { |
| 85 | + callUnauthorized(reply, 'Provided key is not Private'); |
| 86 | + } else if (err == 404) { |
| 87 | + reply.callNotFound(); |
| 88 | + } else { |
| 89 | + callInternalServerError(reply, err); |
| 90 | + } |
| 91 | + } |
| 92 | +}) |
| 93 | + |
| 94 | +fastify.post('/private/:privateKey', async (request, reply) => { |
| 95 | + const { privateKey } = request.params; |
| 96 | + try { |
| 97 | + if (helper.validate(privateKey) !== 'private') throw 401; |
| 98 | + await helper.privateProduce(privateKey, JSON.stringify(request.body)); |
| 99 | + reply.send({message: "Done", error: "Ok", statusCode: reply.statusCode}); |
| 100 | + } catch (err) { |
| 101 | + if (err == 401) { |
| 102 | + callUnauthorized(reply, 'Provided key is not Private'); |
| 103 | + } else { |
| 104 | + callInternalServerError(reply, err); |
| 105 | + } |
| 106 | + } |
| 107 | +}) |
| 108 | + |
| 109 | +fastify.get('/public/:publicKey', async (request, reply) => { |
| 110 | + const { publicKey } = request.params; |
| 111 | + try { |
| 112 | + if (helper.validate(publicKey) !== 'public') throw 401; |
| 113 | + const data = await helper.publicConsume(publicKey); |
| 114 | + if (!data) throw 404; |
| 115 | + reply.send(data); |
| 116 | + } catch (err) { |
| 117 | + if (err == 401) { |
| 118 | + callUnauthorized(reply, 'Provided key is not Public'); |
| 119 | + } else if (err == 404) { |
| 120 | + reply.callNotFound(); |
| 121 | + } else { |
| 122 | + callInternalServerError(reply, err); |
| 123 | + } |
| 124 | + } |
| 125 | +}) |
| 126 | + |
| 127 | +fastify.post('/private/:privateKey/:key', async (request, reply) => { |
| 128 | + const { privateKey, key } = request.params; |
| 129 | + if (key.substr(0,fieldLimit) !== key) {callBadRequest(reply, 'Provided field is too long'); return;} |
| 130 | + try { |
| 131 | + if (helper.validate(privateKey) !== 'private') throw 401; |
| 132 | + await helper.oneToOneProduce(privateKey, key, JSON.stringify(request.body)); |
| 133 | + reply.send({message: "Done", error: "Ok", statusCode: reply.statusCode}); |
| 134 | + } catch (err) { |
| 135 | + if (err == 401) { |
| 136 | + callUnauthorized(reply, 'Provided key is not Private'); |
| 137 | + } else { |
| 138 | + callInternalServerError(reply, err); |
| 139 | + } |
| 140 | + } |
| 141 | +}) |
| 142 | + |
| 143 | +fastify.get('/public/:publicKey/:key', async (request, reply) => { |
| 144 | + const { publicKey, key } = request.params; |
| 145 | + if (key.substr(0,fieldLimit) !== key) {callBadRequest(reply, 'Provided field is too long'); return;} |
| 146 | + try { |
| 147 | + if (helper.validate(publicKey) !== 'public') throw 401; |
| 148 | + const data = await helper.oneToOneConsume(publicKey, key); |
| 149 | + if (!data) throw 404; |
| 150 | + reply.send(data); |
| 151 | + } catch (err) { |
| 152 | + if (err == 401) { |
| 153 | + callUnauthorized(reply, 'Provided key is not Public'); |
| 154 | + } else if (err == 404) { |
| 155 | + reply.callNotFound(); |
| 156 | + } else { |
| 157 | + callInternalServerError(reply, err); |
| 158 | + } |
| 159 | + } |
| 160 | +}) |
| 161 | + |
| 162 | +fastify.get('/private/:privateKey/:key', async (request, reply) => { |
| 163 | + const { privateKey, key } = request.params; |
| 164 | + if (key.substr(0,fieldLimit) !== key) {callBadRequest(reply, 'Provided field is too long'); return;} |
| 165 | + try { |
| 166 | + if (helper.validate(privateKey) !== 'private') throw 401; |
| 167 | + reply.send(await helper.oneToOneIsConsumed(privateKey, key)); |
| 168 | + } catch (err) { |
| 169 | + if (err == 401) { |
| 170 | + callUnauthorized(reply, 'Provided key is not Private'); |
| 171 | + } else { |
| 172 | + callInternalServerError(reply, err); |
| 173 | + } |
| 174 | + } |
| 175 | +}) |
| 176 | + |
| 177 | +export default async function handler(req, res) { |
| 178 | + await fastify.ready(); |
| 179 | + fastify.server.emit('request', req, res); |
| 180 | +} |
0 commit comments