Browse Source
* Create authentication middleware example. For #339 * Fix example test filename.pull/341/merge
1 changed files with 46 additions and 0 deletions
@ -0,0 +1,46 @@ |
|||||||
|
package mux_test |
||||||
|
|
||||||
|
import ( |
||||||
|
"log" |
||||||
|
"net/http" |
||||||
|
|
||||||
|
"github.com/gorilla/mux" |
||||||
|
) |
||||||
|
|
||||||
|
// Define our struct
|
||||||
|
type authenticationMiddleware struct { |
||||||
|
tokenUsers map[string]string |
||||||
|
} |
||||||
|
|
||||||
|
// Initialize it somewhere
|
||||||
|
func (amw *authenticationMiddleware) Populate() { |
||||||
|
amw.tokenUsers["00000000"] = "user0" |
||||||
|
amw.tokenUsers["aaaaaaaa"] = "userA" |
||||||
|
amw.tokenUsers["05f717e5"] = "randomUser" |
||||||
|
amw.tokenUsers["deadbeef"] = "user0" |
||||||
|
} |
||||||
|
|
||||||
|
// 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") |
||||||
|
|
||||||
|
if user, found := amw.tokenUsers[token]; found { |
||||||
|
// We found the token in our map
|
||||||
|
log.Printf("Authenticated user %s\n", user) |
||||||
|
next.ServeHTTP(w, r) |
||||||
|
} else { |
||||||
|
http.Error(w, "Forbidden", 403) |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func Example_authenticationMiddleware() { |
||||||
|
r := mux.NewRouter() |
||||||
|
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
||||||
|
// Do something here
|
||||||
|
}) |
||||||
|
amw := authenticationMiddleware{} |
||||||
|
amw.Populate() |
||||||
|
r.Use(amw.Middleware) |
||||||
|
} |
||||||
Loading…
Reference in new issue