Created
August 16, 2019 04:01
-
-
Save hikilaka/6423d4b921aa6ce8a1c0fcc1483407fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline') | |
const uuid = require('uuid/v4') | |
const knex = require('knex')({ | |
client: 'sqlite3', | |
connection: { filename: './database.db' }, | |
useNullAsDefault: true | |
}) | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}) | |
console.log('adding new authentication key...') | |
rl.question('alias? ', alias => { | |
rl.question('enabled? ', enabled => { | |
knex('auth_keys').insert({ | |
alias: alias.trim().toLowerCase(), | |
key: uuid(), | |
enabled: enabled.trim().toLowerCase().startsWith('y') | |
}).then(record => { | |
console.log(JSON.stringify(record)) | |
rl.close() | |
knex.destroy() | |
}).catch(error => { | |
console.log(`error inserting auth key: ${error}`) | |
rl.close() | |
knex.destroy() | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
const port = 5000 | |
const knex = require('knex')({ | |
client: 'sqlite3', | |
connection: { filename: './database.db' }, | |
useNullAsDefault: true | |
}) | |
knex.schema.hasTable('auth_keys').then(exists => { | |
console.log('table exists? ' + exists) | |
if (exists) return; | |
return knex.schema.createTable('auth_keys', table => { | |
table.increments('id').primary() | |
table.string('alias', 40).defaultTo('') | |
table.string('key', 40).defaultTo('') | |
table.boolean('enabled').defaultTo(false) | |
}).then(() => { | |
console.log('Created auth_keys table..') | |
}).catch(error => { | |
console.log(`Error creating auth_keys table: ${error}`); | |
}) | |
}).then(() => { | |
startApp() | |
}).catch(error => { | |
console.log(`Error checking if table exists: ${error}`) | |
}) | |
function startApp() { | |
console.log('app starting..') | |
app.get('/authenticate/:key', (req, res) => { | |
knex('auth_keys') | |
.select() | |
.where('key', req.params.key) | |
.then(results => { | |
const enabled = results.length > 0 ? results[0].enabled : false | |
res.end(enabled ? 'yes' : 'no') | |
}).catch(error => { | |
console.log(`error: ${error}`) | |
res.end('error') | |
}) | |
}) | |
app.listen(port, () => { | |
console.log(`Authentication app listening on port ${port}!`) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment