Advertisement
arxeiss

HTTP Basic auth

May 14th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.90 KB | None | 0 0
  1. // All paths defined in secureRouter have to pass HTTP Basic auth
  2. secureRouter := router.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
  3.     name, pass, ok := r.BasicAuth()
  4.     if ok && name == configObject.AuthName && pass == configObject.AuthPass {
  5.         return true
  6.     }
  7.     return false
  8. }).Subrouter()
  9.  
  10. campaignsRouter := router.PathPrefix("/campaigns").Subrouter()
  11. campaignsRouter.HandleFunc("/", controllers.CampaignListActive).Methods("GET")
  12. campaignsRouter.HandleFunc("/active/", controllers.CampaignListActive).Methods("GET")
  13. campaignsRouter.HandleFunc("/", controllers.CampaignUpdateOrCreate).Methods("POST")
  14.  
  15. // Will not match images, css or JS and will not pass HTTP Basic auth
  16. router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  17.     w.Header().Set("WWW-Authenticate", `Basic realm="Spamler"`)
  18.     w.WriteHeader(401)
  19.     w.Write([]byte("401 Unauthorized\n"))
  20. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement