-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.go
69 lines (58 loc) · 1.92 KB
/
middleware.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
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"net"
"net/http"
"strings"
)
//type MiddlewareFunc func(http.Handler) http.Handler
// Define auth struct
type authenticationMiddleware struct {
tokenUsers map[string]string
allowIPs map[string]string
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do stuff here
logger.Println(r.RemoteAddr, r.Method, r.Referer(), r.RequestURI)
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}
// Initialize authenticationMiddleware
func (amw *authenticationMiddleware) Populate() {
amw.tokenUsers = make(map[string]string)
amw.allowIPs = make(map[string]string)
//Populate token
//amw.tokenUsers["00000000"] = "user0"
for _, t := range conf.User_token {
p := strings.Split(t, ":=")
amw.tokenUsers[p[1]] = p[0]
}
//Populate allow ip
//amw.allowIPs["::1"] = "local"
for _, l := range conf.Allow_ip {
p := strings.Split(l, ":=")
amw.allowIPs[p[1]] = p[0]
}
}
// Middleware function, which will be called for each request
func (amw *authenticationMiddleware) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("X-Session-Token")
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
if user, found := amw.tokenUsers[token]; found {
// We found the token in our map
logger.Printf("Authenticated user %s\n", user)
// Pass down the request to the next middleware (or final handler)
next.ServeHTTP(w, r)
} else if location, found := amw.allowIPs[ip]; found {
// We found the ip in our allow ip
logger.Printf("Authenticated from %s\n", location)
// Pass down the request to the next middleware (or final handler)
next.ServeHTTP(w, r)
} else {
// Write an error and stop the handler chain
http.Error(w, "Forbidden", http.StatusForbidden)
}
})
}