From 6420ffb055f08348c54cd08a193aba5fe5ebc13a Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 18 Aug 2021 15:36:16 +0200 Subject: [PATCH] Switched to fastify-auth to support multiple auth providers --- package.json | 1 + src/server.js | 17 +++++++++-------- yarn.lock | 8 ++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 510b104..78d1132 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "argon2": "^0.28.2", "dotenv": "^10.0.0", "fastify": "^3.20.1", + "fastify-auth": "^1.1.0", "fastify-basic-auth": "^2.1.0", "fastify-cors": "^6.0.2", "knex": "^0.21.21", diff --git a/src/server.js b/src/server.js index 59c3f34..91122c1 100644 --- a/src/server.js +++ b/src/server.js @@ -6,6 +6,7 @@ const argon2 = require('argon2'); let config = { domain: process.env.DOMAIN || "localhost:3000", https: (process.env.SSL === 'true') || false, + env: process.env.NODE_ENV || 'development', recognizeProviders: !(process.env.DISABLE_PROVIDERS === 'true'), registrationEnabled: (process.env.ENABLE_REGISTER === 'true'), getBaseUrl() { @@ -15,17 +16,17 @@ let config = { return `http://${config.domain}`; } } -const environment = process.env.NODE_ENV || 'development'; -const knexConfiguration = require('../knexfile')[environment]; +const knexConfiguration = require('../knexfile')[config.env]; const knex = require('knex')(knexConfiguration); const authenticate = { realm: 'Short' } +fastify.register(require('fastify-auth')) fastify.register(require('fastify-basic-auth'), { validate, authenticate }); -fastify.register(require('fastify-cors'), { +fastify.register(require('fastify-cors'), { origin: true, preflight: true, preflightContinue: true - }) +}) //Automagic Amazn redirects on /a/ fastify.get('/a/:id', async (req, res) => { @@ -219,7 +220,7 @@ fastify.post('/api/register', async (req, res) => { //Anything in here has some kind of auth fastify.after(() => { //Get url api route - fastify.get('/api/:shortcode/visits', { onRequest: fastify.basicAuth }, async (req, res) => { + fastify.get('/api/:shortcode/visits', { onRequest: fastify.auth([fastify.basicAuth]) }, async (req, res) => { const shortcode = req.params.shortcode; //This should never happen but better safe than 500 @@ -243,7 +244,7 @@ fastify.after(() => { }); //Get url api route - fastify.delete('/api/:shortcode', { onRequest: fastify.basicAuth }, async (req, res) => { + fastify.delete('/api/:shortcode', { onRequest: fastify.auth([fastify.basicAuth]) }, async (req, res) => { const shortcode = req.params.shortcode; //This should never happen but better safe than 500 @@ -260,13 +261,13 @@ fastify.after(() => { }); //Get all urls api route - fastify.get('/api', { onRequest: fastify.basicAuth }, async (req, res) => { + fastify.get('/api', { onRequest: fastify.auth([fastify.basicAuth]) }, async (req, res) => { urls = await knex.select('target', 'shortcode') .from('urls'); for (let url of urls) { url.url = `${config.getBaseUrl()}/${url.shortcode}` - if(req.query.showVisits){ + if (req.query.showVisits) { url.visits = (await knex.select('timestamp') .from('visits') .where('shortcode', '=', url.shortcode)).length; diff --git a/yarn.lock b/yarn.lock index 2851592..7ea0666 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1240,6 +1240,14 @@ fast-safe-stringify@^2.0.8: resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.0.8.tgz#dc2af48c46cf712b683e849b2bbd446b32de936f" integrity sha512-lXatBjf3WPjmWD6DpIZxkeSsCOwqI0maYMpgDlx8g4U2qi4lbjA9oH/HD2a87G+KfsUmo5WbJFmqBZlPxtptag== +fastify-auth@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/fastify-auth/-/fastify-auth-1.1.0.tgz#75076c9c0664addaff07078907db6432086be1d6" + integrity sha512-8IajmAZB3QJ3wTP0q8Z3TG9DkxrIcAlS85TdPCBEfJi3mMKQd/sCYxtZ0dYv11v5hZaJ9z8XmNzhK3AH6/JpNw== + dependencies: + fastify-plugin "^3.0.0" + reusify "^1.0.4" + fastify-basic-auth@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/fastify-basic-auth/-/fastify-basic-auth-2.1.0.tgz#a368e4e900f402a2c26f4cab927484b9f2ac539c"