Switched to fastify-auth to support multiple auth providers

This commit is contained in:
Nicolai Ort 2021-08-18 15:36:16 +02:00
parent a62ee63c83
commit 6420ffb055
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
3 changed files with 18 additions and 8 deletions

View File

@ -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",

View File

@ -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;

View File

@ -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"