feat(): add basic authentication

This commit is contained in:
Lucas Barbieri 2024-08-19 19:47:32 -03:00
parent 48528f3a56
commit b8ad1dd9b9
3 changed files with 12 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
files/ files/
.key

View File

@ -7,4 +7,5 @@ services:
- "58080:8080" - "58080:8080"
volumes: volumes:
- ./files:/app/files - ./files:/app/files
- ./.key:/app/.key
restart: unless-stopped restart: unless-stopped

10
main.go
View File

@ -42,6 +42,11 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if !checkAuth(w, r) {
http.Error(w, "You're not authorized.", http.StatusBadRequest)
return
}
r.Body = http.MaxBytesReader(w, r.Body, maxFileSize) r.Body = http.MaxBytesReader(w, r.Body, maxFileSize)
file, _, err := r.FormFile("file") file, _, err := r.FormFile("file")
@ -77,3 +82,8 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "http://%s/%d\n", url, time) fmt.Fprintf(w, "http://%s/%d\n", url, time)
} }
} }
func checkAuth(w http.ResponseWriter, r *http.Request) bool {
authKey, _ := os.ReadFile(".key")
return r.Header.Get("X-Auth")+"\n" == string(authKey)
}