2018-01-26 08:43:53 +01:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2018-01-29 09:18:19 +01:00
|
|
|
"git.klink.asia/paul/certman/services"
|
2018-02-03 18:14:47 +01:00
|
|
|
"golang.org/x/oauth2"
|
2018-01-29 09:18:19 +01:00
|
|
|
|
2018-01-26 08:43:53 +01:00
|
|
|
"git.klink.asia/paul/certman/assets"
|
|
|
|
"git.klink.asia/paul/certman/handlers"
|
|
|
|
"git.klink.asia/paul/certman/views"
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
"github.com/gorilla/csrf"
|
|
|
|
|
|
|
|
mw "git.klink.asia/paul/certman/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// TODO: make this configurable
|
|
|
|
csrfCookieName = "csrf"
|
|
|
|
csrfFieldName = "csrf_token"
|
|
|
|
csrfKey = []byte("7Oj4DllZ9lTsxJnisTuWiiQBGQIzi6gX")
|
|
|
|
cookieKey = []byte("osx70sMD8HZG2ouUl8uKI4wcMugiJ2WH")
|
|
|
|
)
|
|
|
|
|
2018-01-29 16:52:59 +01:00
|
|
|
func HandleRoutes(provider *services.Provider) http.Handler {
|
2018-01-26 08:43:53 +01:00
|
|
|
mux := chi.NewMux()
|
|
|
|
|
2018-01-29 09:18:19 +01:00
|
|
|
//mux.Use(middleware.RequestID)
|
2018-01-29 16:52:59 +01:00
|
|
|
mux.Use(middleware.Logger) // log requests
|
|
|
|
mux.Use(middleware.RealIP) // use proxy headers
|
|
|
|
mux.Use(middleware.RedirectSlashes) // redirect trailing slashes
|
|
|
|
mux.Use(mw.Recoverer) // recover on panic
|
|
|
|
mux.Use(provider.Sessions.Manager.Use) // use session storage
|
2018-01-26 08:43:53 +01:00
|
|
|
|
2018-02-03 18:14:47 +01:00
|
|
|
// TODO: move this code away from here
|
|
|
|
oauth2Config := &oauth2.Config{
|
|
|
|
ClientID: os.Getenv("OAUTH2_CLIENT_ID"),
|
|
|
|
ClientSecret: os.Getenv("OAUTH2_CLIENT_SECRET"),
|
|
|
|
Scopes: []string{"read_user"},
|
|
|
|
RedirectURL: os.Getenv("OAUTH2_REDIRECT_URL"),
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: os.Getenv("OAUTH2_AUTH_URL"),
|
|
|
|
TokenURL: os.Getenv("OAUTH2_TOKEN_URL"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-26 08:43:53 +01:00
|
|
|
// we are serving the static files directly from the assets package
|
2018-01-29 09:18:19 +01:00
|
|
|
// this either means we use the embedded files, or live-load
|
|
|
|
// from the file system (if `--tags="dev"` is used).
|
2018-01-26 08:43:53 +01:00
|
|
|
fileServer(mux, "/static", assets.Assets)
|
|
|
|
|
|
|
|
mux.Route("/", func(r chi.Router) {
|
|
|
|
if os.Getenv("ENVIRONMENT") != "test" {
|
|
|
|
r.Use(csrf.Protect(
|
|
|
|
csrfKey,
|
|
|
|
csrf.Secure(false),
|
|
|
|
csrf.CookieName(csrfCookieName),
|
|
|
|
csrf.FieldName(csrfFieldName),
|
2018-01-29 09:18:19 +01:00
|
|
|
csrf.ErrorHandler(http.HandlerFunc(handlers.CSRFErrorHandler)),
|
2018-01-26 08:43:53 +01:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-02-01 09:31:06 +01:00
|
|
|
r.HandleFunc("/", http.RedirectHandler("certs", http.StatusFound).ServeHTTP)
|
2018-01-26 08:43:53 +01:00
|
|
|
|
2018-01-29 09:18:19 +01:00
|
|
|
r.Route("/login", func(r chi.Router) {
|
2018-02-03 18:14:47 +01:00
|
|
|
r.Get("/", handlers.GetLoginHandler(provider, oauth2Config))
|
|
|
|
r.Get("/oauth2/redirect", handlers.OAuth2Endpoint(provider, oauth2Config))
|
2018-01-29 09:18:19 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
r.Route("/certs", func(r chi.Router) {
|
2018-01-29 16:52:59 +01:00
|
|
|
r.Use(mw.RequireLogin(provider.Sessions))
|
2018-02-01 09:31:06 +01:00
|
|
|
r.Get("/", handlers.ListClientsHandler(provider))
|
2018-01-29 16:52:59 +01:00
|
|
|
r.Post("/new", handlers.CreateCertHandler(provider))
|
2018-02-01 09:31:06 +01:00
|
|
|
r.HandleFunc("/download/{name}", handlers.DownloadCertHandler(provider))
|
2018-02-03 18:14:47 +01:00
|
|
|
r.Post("/delete/{name}", handlers.DeleteCertHandler(provider))
|
2018-01-26 08:43:53 +01:00
|
|
|
})
|
2018-02-03 18:14:47 +01:00
|
|
|
|
|
|
|
r.Get("/unconfigured-backend", handlers.NotFoundHandler)
|
2018-01-26 08:43:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// what should happen if no route matches
|
|
|
|
mux.NotFound(handlers.NotFoundHandler)
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2018-01-29 09:18:19 +01:00
|
|
|
// v is a helper function for quickly displaying a view
|
|
|
|
func v(template string) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
view := views.New(req)
|
|
|
|
view.Render(w, template)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 08:43:53 +01:00
|
|
|
// fileServer sets up a http.FileServer handler to serve
|
|
|
|
// static files from a http.FileSystem.
|
|
|
|
func fileServer(r chi.Router, path string, root http.FileSystem) {
|
|
|
|
if strings.ContainsAny(path, "{}*") {
|
|
|
|
panic("FileServer does not permit URL parameters.")
|
|
|
|
}
|
|
|
|
|
|
|
|
//fs := http.StripPrefix(path, http.FileServer(root))
|
|
|
|
fs := http.FileServer(root)
|
|
|
|
|
|
|
|
if path != "/" && path[len(path)-1] != '/' {
|
|
|
|
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
path += "*"
|
|
|
|
|
|
|
|
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fs.ServeHTTP(w, r)
|
|
|
|
}))
|
|
|
|
}
|