Implement API token authentication for upload endpoints#7
Draft
Implement API token authentication for upload endpoints#7
Conversation
Co-authored-by: drdule <204492158+drdule@users.noreply.github.com>
Co-authored-by: drdule <204492158+drdule@users.noreply.github.com>
Co-authored-by: drdule <204492158+drdule@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Implement API token authentication for endpoint protection
Implement API token authentication for upload endpoints
Jan 4, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds Bearer token authentication to protect image upload endpoints while keeping monitoring endpoints public.
Changes
Authentication
verify_token()dependency function validating Bearer tokens againstAPI_TOKENenvironment variablehmac.compare_digest()for constant-time comparison (timing attack protection)Protected Endpoints (require Bearer token)
POST /upload- existing upload endpointPOST /notify_upload- new endpoint verifying image exists in database and on diskPublic Endpoints (no authentication)
GET /- rootGET /health- health checkGET /metrics- new endpoint returning system statistics (total images, active meters, images per meter)Configuration
API_TOKENto.env.exampleDocumentation
Example
Usage:
Original prompt
Tačka 6: API Token Autentikacija
Implementirati API token autentikaciju za zaštitu endpoint-a.
Zahtevi:
1. Environment varijabla za API token
Dodaj u
.envfajl (ili kreiraj ako ne postoji):Napomena:
.envfajl mora biti u.gitignore(ne sme ići na GitHub)2. Dependency funkcija
verify_token()umain.py3. Primena autentikacije na endpoint-e
Zaštićeni endpoint-i (ZAHTEVAJU token):
POST /uploadPOST /notify_uploadJavni endpoint-i (NE zahtevaju token):
GET /(root)GET /health(health check)GET /metrics(metrics)Primer za
/uploadendpoint:Primer za
/notify_uploadendpoint:4. Ažuriraj imports u
main.pyProveri da imaš:
5. Dodaj
.env.examplefajlKreiraj fajl
.env.exampleu root direktorijumu:Napomena: Ovo je template fajl (IDE na GitHub), stvarni
.envostaje lokalno.6. Proveri
.gitignoreProveri da
.gitignoresadrži:Testiranje:
Test 1: Upload BEZ tokena (treba da ODBIJE):
Test 2: Upload SA pogrešnim tokenom (treba da ODBIJE):
Test 3: Upload SA ispravnim tokenom (treba da PROĐE):