Added basics for auth
This commit is contained in:
parent
1a04743db9
commit
6d417fac1b
|
@ -1,4 +1,4 @@
|
||||||
FROM node:15-alpine as run
|
FROM node:16-alpine as run
|
||||||
|
|
||||||
COPY package.json .
|
COPY package.json .
|
||||||
RUN yarn --prod
|
RUN yarn --prod
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dotenv": "^10.0.0",
|
"dotenv": "^10.0.0",
|
||||||
"fastify": "^3.20.1",
|
"fastify": "^3.20.1",
|
||||||
|
"fastify-basic-auth": "^2.1.0",
|
||||||
"knex": "^0.21.21",
|
"knex": "^0.21.21",
|
||||||
"sqlite3": "^5.0.2",
|
"sqlite3": "^5.0.2",
|
||||||
"uniqid": "^5.3.0"
|
"uniqid": "^5.3.0"
|
||||||
|
|
|
@ -21,10 +21,8 @@ const knex = require('knex')({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//Basic home route
|
const authenticate = { realm: 'Short' }
|
||||||
fastify.get('/', async (request, reply) => {
|
fastify.register(require('fastify-basic-auth'), { validate, authenticate });
|
||||||
return { hello: 'world' }
|
|
||||||
})
|
|
||||||
|
|
||||||
//Automagic Amazn redirects on /a/
|
//Automagic Amazn redirects on /a/
|
||||||
fastify.get('/a/:id', async (req, res) => {
|
fastify.get('/a/:id', async (req, res) => {
|
||||||
|
@ -162,46 +160,51 @@ fastify.get('/api/:shortcode', async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//Get url api route
|
fastify.after(() => {
|
||||||
fastify.get('/api/:shortcode/visits', async (req, res) => {
|
//Get url api route
|
||||||
const shortcode = req.params.shortcode;
|
fastify.get('/api/:shortcode/visits', { onRequest: fastify.basicAuth }, async (req, res) => {
|
||||||
|
const shortcode = req.params.shortcode;
|
||||||
|
|
||||||
//This should never happen but better safe than 500
|
//This should never happen but better safe than 500
|
||||||
if (!shortcode) {
|
if (!shortcode) {
|
||||||
return 404;
|
return 404;
|
||||||
}
|
}
|
||||||
|
|
||||||
const exists = await knex.select('shortcode', 'target')
|
const exists = await knex.select('shortcode', 'target')
|
||||||
.from('urls')
|
.from('urls')
|
||||||
.where('shortcode', '=', shortcode)
|
.where('shortcode', '=', shortcode)
|
||||||
.limit(1);
|
.limit(1);
|
||||||
if (exists.length == 0) {
|
if (exists.length == 0) {
|
||||||
return 404;
|
return 404;
|
||||||
}
|
}
|
||||||
|
|
||||||
const visits = await knex.select('timestamp')
|
const visits = await knex.select('timestamp')
|
||||||
.from('visits')
|
.from('visits')
|
||||||
.where('shortcode', '=', shortcode);
|
.where('shortcode', '=', shortcode);
|
||||||
|
|
||||||
|
return visits;
|
||||||
|
});
|
||||||
|
|
||||||
|
//Get url api route
|
||||||
|
fastify.delete('/api/:shortcode', async (req, res) => {
|
||||||
|
const shortcode = req.params.shortcode;
|
||||||
|
|
||||||
|
//This should never happen but better safe than 500
|
||||||
|
if (!shortcode) {
|
||||||
|
return 404;
|
||||||
|
}
|
||||||
|
|
||||||
|
await knex('urls')
|
||||||
|
.where('shortcode', '=', shortcode)
|
||||||
|
.delete();
|
||||||
|
|
||||||
|
res.statusCode = 204;
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
return visits;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//Get url api route
|
|
||||||
fastify.delete('/api/:shortcode', async (req, res) => {
|
|
||||||
const shortcode = req.params.shortcode;
|
|
||||||
|
|
||||||
//This should never happen but better safe than 500
|
|
||||||
if (!shortcode) {
|
|
||||||
return 404;
|
|
||||||
}
|
|
||||||
|
|
||||||
await knex('urls')
|
|
||||||
.where('shortcode', '=', shortcode)
|
|
||||||
.delete();
|
|
||||||
|
|
||||||
res.statusCode = 204;
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks for some default providers with custom url schemes (amazon and youtube r/n)
|
* Checks for some default providers with custom url schemes (amazon and youtube r/n)
|
||||||
|
@ -240,6 +243,13 @@ function checkKnownProviders(target) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function validate(username, password, req, reply) {
|
||||||
|
console.log(username)
|
||||||
|
if (username !== 'admin' || password !== 'admin') {
|
||||||
|
return new Error('Sorry only authorized users can do that.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Run the server!
|
// Run the server!
|
||||||
const start = async () => {
|
const start = async () => {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue