BareRTC/pkg/pages.go

128 lines
3.2 KiB
Go
Raw Permalink Normal View History

2023-01-11 06:38:48 +00:00
package barertc
import (
"fmt"
2023-01-11 06:38:48 +00:00
"html/template"
"net/http"
"strings"
2023-01-11 06:38:48 +00:00
"git.kirsle.net/apps/barertc/pkg/config"
"git.kirsle.net/apps/barertc/pkg/jwt"
2023-01-11 06:38:48 +00:00
"git.kirsle.net/apps/barertc/pkg/log"
"git.kirsle.net/apps/barertc/pkg/util"
2023-01-11 06:38:48 +00:00
)
// IndexPage returns the HTML template for the chat room.
func IndexPage() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Load the template, TODO: once on server startup.
tmpl := template.New("index")
// Handle a JWT authentication token.
var (
tokenStr = r.FormValue("jwt")
claims = &jwt.Claims{}
authOK bool
blocklist = []string{} // cached blocklist from your website, for JWT auth only
)
if tokenStr != "" {
parsed, ok, err := jwt.ParseAndValidate(tokenStr)
if err != nil {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(
fmt.Sprintf("Error parsing your JWT token: %s", err),
))
return
}
authOK = ok
claims = parsed
blocklist = GetCachedBlocklist(claims.Subject)
}
// Are we enforcing strict JWT authentication?
if config.Current.JWT.Enabled && config.Current.JWT.Strict && !authOK {
// Do we have a landing page to redirect to?
if config.Current.JWT.LandingPageURL != "" {
w.Header().Add("Location", config.Current.JWT.LandingPageURL)
w.WriteHeader(http.StatusFound)
return
}
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(
"Authentication denied. Please go back and try again.",
))
return
}
// Variables to give to the front-end page.
var values = map[string]interface{}{
// A cache-busting hash for JS and CSS includes.
"CacheHash": util.RandomString(8),
// The current website settings.
"Config": config.Current,
// Authentication settings.
"JWTTokenString": tokenStr,
"JWTAuthOK": authOK,
"JWTClaims": claims,
// Cached user blocklist sent by your website.
"CachedBlocklist": blocklist,
}
2023-01-11 06:38:48 +00:00
tmpl.Funcs(template.FuncMap{
"AsHTML": func(v string) template.HTML {
return template.HTML(v)
},
2023-02-11 06:46:39 +00:00
"AsJS": func(v interface{}) template.JS {
return template.JS(fmt.Sprintf("%v", v))
},
2023-01-11 06:38:48 +00:00
})
tmpl, err := tmpl.ParseFiles("dist/index.html")
2023-01-11 06:38:48 +00:00
if err != nil {
panic(err.Error())
}
// END load the template
log.Info("GET / [%s] %s", r.RemoteAddr, strings.Join([]string{
r.Header.Get("X-Forwarded-For"),
r.Header.Get("X-Real-IP"),
r.Header.Get("User-Agent"),
util.IPAddress(r),
}, " "))
tmpl.ExecuteTemplate(w, "index", values)
2023-01-11 06:38:48 +00:00
})
}
// AboutPage returns the HTML template for the about page.
func AboutPage() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Load the template, TODO: once on server startup.
tmpl := template.New("index")
// Variables to give to the front-end page.
var values = map[string]interface{}{
// A cache-busting hash for JS and CSS includes.
"CacheHash": util.RandomString(8),
// The current website settings.
"Config": config.Current,
}
tmpl.Funcs(template.FuncMap{
"AsHTML": func(v string) template.HTML {
return template.HTML(v)
},
})
tmpl, err := tmpl.ParseFiles("web/templates/about.html")
if err != nil {
panic(err.Error())
}
tmpl.ExecuteTemplate(w, "index", values)
})
}