Skip to content

Commit

Permalink
Add basic api key handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
julien66 committed Dec 3, 2019
1 parent 2e3019f commit f082803
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
keys.js
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ RUN yarn install

ENV TILE_SET_CACHE 128
ENV TILE_SET_PATH /app/data
ENV MAX_POST_SIZE 700kb
ENV MAX_POST_SIZE 1000kb

EXPOSE 3000
EXPOSE 4000

HEALTHCHECK CMD curl --fail http://localhost:3000/status || exit 1
HEALTHCHECK CMD curl --fail http://localhost:4000/status || exit 1

CMD ["yarn", "run", "start"]
53 changes: 52 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,43 @@ const limitedMap = require("limited-map");
const query = require("micro-query");
const cors = require("micro-cors")();
const { FileTileSet, S3TileSet } = require("./tileset");
//const hat = require('hat');
const crypto = require('crypto');
const keys = require('./keys.js');

const cacheSize = process.env.TILE_SET_CACHE || 128;
const tileFolder = process.env.TILE_SET_PATH || __dirname;
const maxPostSize = process.env.MAX_POST_SIZE || "500kb";
const maxPostSize = process.env.MAX_POST_SIZE || "1000kb";
const maxParallelProcessing = 500;

// var rack = hat.rack();
// var key = rack();
// var key = 'your-key-goes-here';
// const shasum = crypto.createHash('sha256');
// shasum.update(key);
// console.log(shasum.digest('hex'));

const tiles = tileFolder.startsWith("s3://")
? new S3TileSet({ cacheSize })
: new FileTileSet(tileFolder, { cacheSize });

async function handlePOST(req, res) {
const payload = await json(req, { limit: maxPostSize });
const reqQuery = query(req);
const apiKey = reqQuery.key;

if (!apiKey) {
return send(res, 400, {
error: apiMessage('Missing'),
});
}

if (verifyKey(apiKey) < 0) {
return send(res, 400, {
error: apiMessage('Invalid'),
});
}

if (
!payload ||
!Array.isArray(payload) ||
Expand All @@ -36,6 +61,19 @@ async function handlePOST(req, res) {

async function handleGET(req, res) {
const reqQuery = query(req);
const apiKey = reqQuery.key;
if (!apiKey) {
return send(res, 400, {
error: apiMessage('Missing'),
});
}

if (verifyKey(apiKey) < 0) {
return send(res, 400, {
error: apiMessage('Invalid'),
});
}

const lat = parseFloat(reqQuery.lat);
const lng = parseFloat(reqQuery.lng);
if (lat == null || !Number.isFinite(lat)) {
Expand Down Expand Up @@ -76,4 +114,17 @@ async function handler(req, res) {
}
}

function verifyKey(key) {
const shasum = crypto.createHash('sha256');
shasum.update(key);
const allKeys = keys.get();
const digest = shasum.digest('hex');
return allKeys.indexOf(digest);
}

function apiMessage(wrong) {
return wrong + ' API key. Please email Téo or Julien at [email protected] or [email protected].'
}


module.exports = cors(handler);
45 changes: 45 additions & 0 deletions npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle [email protected]~prestart: [email protected]
6 silly lifecycle [email protected]~prestart: no script for prestart, continuing
7 info lifecycle [email protected]~start: [email protected]
8 verbose lifecycle [email protected]~start: unsafe-perm in lifecycle true
9 verbose lifecycle [email protected]~start: PATH: /usr/share/npm/bin/node-gyp-bin:/home/julien/elevation-service/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
10 verbose lifecycle [email protected]~start: CWD: /home/julien/elevation-service
11 silly lifecycle [email protected]~start: Args: [ '-c', 'micro -l tcp://0.0.0.0:4000 index.js' ]
12 silly lifecycle [email protected]~start: Returned: code: 1 signal: null
13 info lifecycle [email protected]~start: Failed to exec start script
14 verbose stack Error: [email protected] start: `micro -l tcp://0.0.0.0:4000 index.js`
14 verbose stack Exit status 1
14 verbose stack at EventEmitter.<anonymous> (/usr/share/npm/lib/utils/lifecycle.js:232:16)
14 verbose stack at emitTwo (events.js:126:13)
14 verbose stack at EventEmitter.emit (events.js:214:7)
14 verbose stack at ChildProcess.<anonymous> (/usr/share/npm/lib/utils/spawn.js:24:14)
14 verbose stack at emitTwo (events.js:126:13)
14 verbose stack at ChildProcess.emit (events.js:214:7)
14 verbose stack at maybeClose (internal/child_process.js:925:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
15 verbose pkgid [email protected]
16 verbose cwd /home/julien/elevation-service
17 error Linux 4.15.0-70-generic
18 error argv "/usr/bin/node" "/usr/bin/npm" "start"
19 error node v8.10.0
20 error npm v3.5.2
21 error code ELIFECYCLE
22 error [email protected] start: `micro -l tcp://0.0.0.0:4000 index.js`
22 error Exit status 1
23 error Failed at the [email protected] start script 'micro -l tcp://0.0.0.0:4000 index.js'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the elevation-service package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error micro -l tcp://0.0.0.0:4000 index.js
23 error You can get information on how to open an issue for this project with:
23 error npm bugs elevation-service
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls elevation-service
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"version": "0.0.1",
"main": "index.js",
"scripts": {
"start": "micro index.js",
"start": "micro -l tcp://0.0.0.0:4000 index.js",
"test": "node test/test.js",
"pretty": "prettier --write *.js test/*.js"
},
"author": "Norman Rzepka <[email protected]>",
"license": "MIT",
"dependencies": {
"hat": "0.0.3",
"limited-map": "^0.0.1",
"lru-memoize": "^1.0.2",
"micro": "^9.3.2",
Expand Down

0 comments on commit f082803

Please sign in to comment.