Skip to content

Commit

Permalink
Fixed authhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
dimapin committed Sep 2, 2024
1 parent e8942d0 commit d65d4c8
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,7 @@ func main() {
*/
router := http.NewServeMux()
router.HandleFunc("/clammit", infoHandler)
if bytes.Equal(secretKey, []byte("secret-key")) {
router.HandleFunc("/clammit/scan", scanHandler)
} else {
authenticatedScanHandler := checkAuthentication(http.HandlerFunc(scanHandler))
router.HandleFunc("/clammit/scan", func(w http.ResponseWriter, r *http.Request) {
authenticatedScanHandler.ServeHTTP(w, r)
})
}
router.HandleFunc("/clammit/scan", scanHandler)
router.HandleFunc("/clammit/readyz", readyzHandler)

if ctx.Config.App.TestPages {
Expand All @@ -194,23 +187,6 @@ func main() {
}
}

func checkAuthentication(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookieName := "jwt"
token, err := getTokenFromCookie(w, r, cookieName)
if err != nil {
log.Fatalf("Not authenticated")
return
}
err2 := verifyToken(token)
if err2 != nil {
log.Fatalf("Token not valid")
return
}
next.ServeHTTP(w, r)
})
}

/*
* Returns the value of an environment variable, or a default value
*/
Expand Down Expand Up @@ -429,6 +405,22 @@ func scanHandler(w http.ResponseWriter, req *http.Request) {
}
ctx.ActivityChan <- 1
defer func() { ctx.ActivityChan <- -1 }()
// Authentication logic
if string(secretKey) != "secret-key" {
cookieName := "jwt"
token, err := getTokenFromCookie(w, req, cookieName)
if err != nil {
log.Fatalf("Not authenticated")
w.WriteHeader(http.StatusUnauthorized)
return
}
err2 := verifyToken(token)
if err2 != nil {
log.Fatalf("Token not valid")
w.WriteHeader(http.StatusUnauthorized)
return
}
}

if !ctx.ScanInterceptor.Handle(w, req, req.Body) {
w.Write([]byte("No virus found"))
Expand Down

0 comments on commit d65d4c8

Please sign in to comment.