From bc8e58e472100266da31959572d6c59d93b115bf Mon Sep 17 00:00:00 2001 From: Schneider Roland Date: Fri, 21 Feb 2025 23:37:46 +0100 Subject: [PATCH] add auth --- .gitignore | 3 ++- README.md | 14 +++++++++++++- index.js | 8 ++++++++ test-1.txt | 2 ++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test-1.txt diff --git a/.gitignore b/.gitignore index b512c09..aa15018 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +data \ No newline at end of file diff --git a/README.md b/README.md index 551a19a..faa41ac 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ ## Create Docker image +npm i + +docker build -t simple-http-upload:0.1.0 . docker build -t 172.22.102.100:40016/team-mmkb/simple-http-upload:0.1.0 . @@ -20,10 +23,19 @@ docker push 172.22.102.100:40016/team-mmkb/simple-http-upload:0.1.0 # Run +docker run --rm -p 3000:3000 -v ./data:/data simple-http-upload:0.1.0 + docker run --rm -p 3000:3000 -v ./data:/data 172.22.102.100:40016/team-mmkb/simple-http-upload:0.1.0 + +docker run --rm -p 3000:3000 -v ./data:/data -e API_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtbWtiIiwibmFtZSI6Im1ta2IiLCJpYXQiOjE1MTYyMzkwMjJ9.vBHcF6SFhImcGC4xDxKdPyq3shjNTPkVxkvdDyfx7Ss simple-http-upload:0.1.0 + + ## Test ```bash curl -XPOST -F 'data=@test-5.txt' localhost:3000/ -``` \ No newline at end of file + +curl -XPOST -F 'data=@test-1.txt' --header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtbWtiIiwibmFtZSI6Im1ta2IiLCJpYXQiOjE1MTYyMzkwMjJ9.vBHcF6SFhImcGC4xDxKdPyq3shjNTPkVxkvdDyfx7Ss" localhost:3000/ +``` + diff --git a/index.js b/index.js index 349f83a..3f684ba 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,14 @@ const config = process.env.config || '{ "uploadGroups": [{ "matcher": "^test.*\. app.use(fileUpload()) +app.use((req, res, next) =>{ + const authenticated = req.get('Authorization') === 'Bearer ' + process.env.API_KEY ; + if(!authenticated){ + return res.status(401).send('Unauthorized'); + } + next(); +}); + const processGroups = () => { const files = [] = fs.readdirSync(uploadPath); diff --git a/test-1.txt b/test-1.txt new file mode 100644 index 0000000..db04a02 --- /dev/null +++ b/test-1.txt @@ -0,0 +1,2 @@ +curl -XPOST -F 'data=@test-5.txt' localhost:3000/ +