-
-
Notifications
You must be signed in to change notification settings - Fork 691
solution 5 #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
solution 5 #554
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||||||||||||||||||||||||
| 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) { | ||||||||||||||||||||||||||||||||
| // TODO: Implement the logic: | ||||||||||||||||||||||||||||||||
| // 1) Grab the "X-Auth-Token" header | ||||||||||||||||||||||||||||||||
| // 2) Compare against validToken | ||||||||||||||||||||||||||||||||
| // 3) If mismatch or missing, respond with 401 | ||||||||||||||||||||||||||||||||
| // 4) Otherwise pass to next handler | ||||||||||||||||||||||||||||||||
| authToken, exist := r.Header["X-Auth-Token"] | ||||||||||||||||||||||||||||||||
| if !exist || authToken[0] != validToken { | ||||||||||||||||||||||||||||||||
| http.Error(w, "", http.StatusUnauthorized) | ||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+20
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Use idiomatic header access and provide meaningful error message. The current implementation has two issues:
Apply this diff to improve the implementation: - authToken, exist := r.Header["X-Auth-Token"]
- if !exist || authToken[0] != validToken {
- http.Error(w, "", http.StatusUnauthorized)
+ authToken := r.Header.Get("X-Auth-Token")
+ if authToken != validToken {
+ http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||
| 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()) | ||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for server startup.
Apply this diff to handle the error: + "log"
+
func main() {
// Optional: you can run a real server for local testing
- http.ListenAndServe(":8080", SetupServer())
+ log.Println("Starting server on :8080")
+ if err := http.ListenAndServe(":8080", SetupServer()); err != nil {
+ log.Fatalf("Server failed to start: %v", err)
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move secret to environment variable.
Hardcoding the authentication token is a security risk. In production, secrets should be loaded from environment variables or a secure configuration service.
Apply this diff to use an environment variable:
🤖 Prompt for AI Agents