Skip to content

Commit

Permalink
auth fix for /details
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-streltsov committed Sep 17, 2024
1 parent ad85df9 commit 8f804c7
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,13 @@ func (h *Handler) deleteURLHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) urlDetailsHandler(w http.ResponseWriter, r *http.Request) {
session, _ := h.store.Get(r, "session")
user, ok := session.Values["user"].(*database.User)
if !ok {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}

urlID, err := strconv.ParseInt(strings.TrimPrefix(r.URL.Path, "/details/"), 10, 64)
if err != nil {
http.Error(w, "Invalid URL ID", http.StatusBadRequest)
Expand All @@ -622,6 +629,11 @@ func (h *Handler) urlDetailsHandler(w http.ResponseWriter, r *http.Request) {
return
}

if url.UserID != user.ID {
http.Error(w, "Unauthorized", http.StatusForbidden)
return
}

shortURL := fmt.Sprintf("http://%s/r/%s", r.Host, url.Key)
qrCode, err := qrcode.Encode(shortURL, qrcode.Medium, 256)
if err != nil {
Expand Down

0 comments on commit 8f804c7

Please sign in to comment.