Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions challenge-5/submissions/Raycas96/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"net/http"
)

const validToken = "secret"

// AuthMiddleware checks the "X-Auth-Token" header.
// If it's "secret", call the next handler.
// Otherwise, respond with 401 Unauthorized.
func AuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 1) Grab the "X-Auth-Token" header
authHeader := r.Header.Get("X-Auth-Token")
// 2) Compare against validToken
if authHeader == "" || authHeader != "secret" {
http.Error(w, "", http.StatusUnauthorized)
return
}

next.ServeHTTP(w, r)
})
}

// helloHandler returns "Hello!" on GET /hello
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello!")
}

// secureHandler returns "You are authorized!" on GET /secure
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Update the handler comment to avoid implying GET-only behavior.

Line [32] says “on GET /secure”, but this handler is correctly method-agnostic. Keeping the comment as-is can mislead future edits into adding method restrictions that would break expected behavior.

Based on learnings: In challenge-5/submissions/*/solution-template.go, ensure the secureHandler is method-agnostic (do not restrict it to GET), and tests include POST /secure with valid token expecting 200 OK.

func secureHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "You are authorized!")
}

// SetupServer configures the HTTP routes with the authentication middleware.
func SetupServer() http.Handler {
mux := http.NewServeMux()

// Public route: /hello (no auth required)
mux.HandleFunc("/hello", helloHandler)

// Secure route: /secure
// Wrap with AuthMiddleware
secureRoute := http.HandlerFunc(secureHandler)
mux.Handle("/secure", AuthMiddleware(secureRoute))

return mux
}

func main() {
// Optional: you can run a real server for local testing
// http.ListenAndServe(":8080", SetupServer())
}
Loading