Skip to content

Commit

Permalink
feat(): add basic authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuxas committed Aug 19, 2024
1 parent 48528f3 commit b8ad1dd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
files/
.key
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ services:
- "58080:8080"
volumes:
- ./files:/app/files
- ./.key:/app/.key
restart: unless-stopped
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
return
}

if !checkAuth(w, r) {
http.Error(w, "You're not authorized.", http.StatusBadRequest)
return
}

r.Body = http.MaxBytesReader(w, r.Body, maxFileSize)

file, _, err := r.FormFile("file")
Expand Down Expand Up @@ -77,3 +82,8 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
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)
}

0 comments on commit b8ad1dd

Please sign in to comment.