-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathapi.go
56 lines (46 loc) · 1.79 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"net/http"
)
var apiServeMux = http.NewServeMux()
func init() {
apiServeMux.Handle("/debug/pprof/", http.DefaultServeMux)
apiServeMux.HandleFunc("/proxy.pac", handlePACFile)
apiServeMux.HandleFunc("/reload", handleReload)
apiServeMux.HandleFunc("/classify", handleClassification)
apiServeMux.HandleFunc("/classify/analyze-score", handleClassification)
apiServeMux.HandleFunc("/classify/verbose", handleClassification)
apiServeMux.HandleFunc("/classify-text", handleClassifyText)
apiServeMux.HandleFunc("/classify-text/verbose", handleClassifyText)
apiServeMux.HandleFunc("/analyze-tally", handleAnalyzeTally)
apiServeMux.HandleFunc("/per-user-ports", handlePerUserPortList)
apiServeMux.HandleFunc("/per-user-ports/authenticate", handlePerUserAuthenticate)
}
func handleAPI(w http.ResponseWriter, r *http.Request) {
conf := getConfig()
authUser := ""
if user, pass, ok := r.BasicAuth(); ok {
if conf.ValidCredentials(user, pass) {
authUser = user
} else {
logAuthEvent("basic-auth", "invalid", r.RemoteAddr, 0, user, pass, "", "", r, "Incorrect username or password for API request")
}
}
acls := conf.APIACLs.requestACLs(r, authUser)
possibleActions := []string{"allow", "block"}
if authUser == "" {
possibleActions = append(possibleActions, "require-auth")
}
thisRule := conf.APIACLs.ChooseACLAction(acls, possibleActions...)
switch thisRule.Action {
case "require-auth":
w.Header().Set("WWW-Authenticate", `Basic realm="Redwood API"`)
http.Error(w, "Authentication required", http.StatusUnauthorized)
logAuthEvent("any-auth", "missing", r.RemoteAddr, 0, "", "", "", "", r, "Missing required API authentication")
return
case "block":
http.Error(w, "You do not have access to this page.", http.StatusForbidden)
return
}
apiServeMux.ServeHTTP(w, r)
}