1
0
mirror of https://github.com/Luzifer/vault-otp-ui.git synced 2024-09-19 17:13:00 +00:00

Remove template rendering, send vars through extra JS file

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-09-09 19:02:52 +02:00
parent 8b77004a95
commit ff37285740
Signed by: luzifer
GPG Key ID: DC2729FDD34BE99E

40
main.go
View File

@ -16,9 +16,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/Luzifer/rconfig"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/alecthomas/template"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/gorilla/securecookie" "github.com/gorilla/securecookie"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
@ -26,6 +24,8 @@ import (
"github.com/tdewolff/minify/html" "github.com/tdewolff/minify/html"
"github.com/tdewolff/minify/js" "github.com/tdewolff/minify/js"
validator "gopkg.in/validator.v2" validator "gopkg.in/validator.v2"
"github.com/Luzifer/rconfig"
) )
const ( const (
@ -96,6 +96,7 @@ func main() {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/oauth2", handleOAuthCallback) r.HandleFunc("/oauth2", handleOAuthCallback)
r.HandleFunc("/application.js", handleApplicationJS) r.HandleFunc("/application.js", handleApplicationJS)
r.HandleFunc("/vars.js", handleApplicationVars)
r.HandleFunc("/codes.json", handleCodesJSON) r.HandleFunc("/codes.json", handleCodesJSON)
r.PathPrefix("/static").HandlerFunc(handleStatics) r.PathPrefix("/static").HandlerFunc(handleStatics)
r.HandleFunc("/", handleIndexPage) r.HandleFunc("/", handleIndexPage)
@ -118,32 +119,13 @@ func getFileContentFallback(filename string) (io.Reader, error) {
} }
func handleIndexPage(res http.ResponseWriter, r *http.Request) { func handleIndexPage(res http.ResponseWriter, r *http.Request) {
sess, _ := cookieStore.Get(r, sessionName)
_, hasAccessToken := sess.Values["access_token"]
content, err := getFileContentFallback("index.html") content, err := getFileContentFallback("index.html")
if err != nil { if err != nil {
http.Error(res, "No suitable index page found", http.StatusInternalServerError) http.Error(res, "No suitable index page found", http.StatusInternalServerError)
} }
buf := new(bytes.Buffer) mini.Minify("text/html", res, content)
io.Copy(buf, content)
tpl, err := template.New("index").Parse(buf.String())
if err != nil {
log.Errorf("Parsing index template failed: %s", err)
http.Error(res, "No suitable index page found", http.StatusInternalServerError)
return
}
outbuf := new(bytes.Buffer)
tpl.Execute(outbuf, map[string]interface{}{
"isloggedin": hasAccessToken,
"authurl": getAuthenticationURL(),
})
mini.Minify("text/html", res, outbuf)
} }
func handleApplicationJS(res http.ResponseWriter, r *http.Request) { func handleApplicationJS(res http.ResponseWriter, r *http.Request) {
@ -156,6 +138,18 @@ func handleApplicationJS(res http.ResponseWriter, r *http.Request) {
mini.Minify("application/javascript", res, content) mini.Minify("application/javascript", res, content)
} }
func handleApplicationVars(w http.ResponseWriter, r *http.Request) {
sess, _ := cookieStore.Get(r, sessionName)
_, hasAccessToken := sess.Values["access_token"]
var buf = new(bytes.Buffer)
fmt.Fprintf(buf, "const signedIn = %v\n", hasAccessToken)
fmt.Fprintf(buf, "const authUrl = %q\n", getAuthenticationURL())
mini.Minify("application/javascript", w, buf)
}
func handleOAuthCallback(res http.ResponseWriter, r *http.Request) { func handleOAuthCallback(res http.ResponseWriter, r *http.Request) {
sess, _ := cookieStore.Get(r, sessionName) sess, _ := cookieStore.Get(r, sessionName)
@ -241,7 +235,7 @@ func handleCodesJSON(res http.ResponseWriter, r *http.Request) {
func handleStatics(res http.ResponseWriter, r *http.Request) { func handleStatics(res http.ResponseWriter, r *http.Request) {
req := strings.TrimLeft(r.URL.Path, "/") req := strings.TrimLeft(r.URL.Path, "/")
ext := req[strings.LastIndex(req, "."):len(req)] ext := req[strings.LastIndex(req, "."):]
t := mime.TypeByExtension(ext) t := mime.TypeByExtension(ext)
b, err := Asset(req) b, err := Asset(req)