2023-01-11 06:38:48 +00:00
|
|
|
package barertc
|
|
|
|
|
|
|
|
import (
|
2023-02-06 01:42:09 +00:00
|
|
|
"fmt"
|
2023-01-11 06:38:48 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2023-02-09 04:01:06 +00:00
|
|
|
"strings"
|
2023-01-11 06:38:48 +00:00
|
|
|
|
2023-02-05 08:53:50 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/config"
|
2023-02-06 01:42:09 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/jwt"
|
2023-01-11 06:38:48 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/log"
|
2023-02-05 08:53:50 +00:00
|
|
|
"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")
|
2023-02-05 08:53:50 +00:00
|
|
|
|
2023-02-06 01:42:09 +00:00
|
|
|
// Handle a JWT authentication token.
|
|
|
|
var (
|
2023-07-30 17:32:08 +00:00
|
|
|
tokenStr = r.FormValue("jwt")
|
|
|
|
claims = &jwt.Claims{}
|
|
|
|
authOK bool
|
|
|
|
blocklist = []string{} // cached blocklist from your website, for JWT auth only
|
2023-02-06 01:42:09 +00:00
|
|
|
)
|
|
|
|
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
|
2023-07-30 17:32:08 +00:00
|
|
|
blocklist = GetCachedBlocklist(claims.Subject)
|
2023-02-06 01:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Are we enforcing strict JWT authentication?
|
|
|
|
if config.Current.JWT.Enabled && config.Current.JWT.Strict && !authOK {
|
2023-07-01 01:41:06 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-02-06 01:42:09 +00:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
w.Write([]byte(
|
2023-07-01 01:41:06 +00:00
|
|
|
"Authentication denied. Please go back and try again.",
|
2023-02-06 01:42:09 +00:00
|
|
|
))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-05 08:53:50 +00:00
|
|
|
// 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,
|
2023-02-06 01:42:09 +00:00
|
|
|
|
|
|
|
// Authentication settings.
|
|
|
|
"JWTTokenString": tokenStr,
|
|
|
|
"JWTAuthOK": authOK,
|
|
|
|
"JWTClaims": claims,
|
2023-07-30 17:32:08 +00:00
|
|
|
|
|
|
|
// Cached user blocklist sent by your website.
|
|
|
|
"CachedBlocklist": blocklist,
|
2023-02-05 08:53:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 06:38:48 +00:00
|
|
|
tmpl.Funcs(template.FuncMap{
|
2023-02-06 04:26:00 +00:00
|
|
|
"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
|
|
|
})
|
2023-09-07 00:15:02 +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
|
|
|
|
|
2023-02-09 04:01:06 +00:00
|
|
|
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),
|
|
|
|
}, " "))
|
2023-02-05 08:53:50 +00:00
|
|
|
tmpl.ExecuteTemplate(w, "index", values)
|
2023-01-11 06:38:48 +00:00
|
|
|
})
|
|
|
|
}
|
2023-02-06 04:26:00 +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.
|
2024-10-18 03:13:08 +00:00
|
|
|
"Config": config.Current,
|
|
|
|
"Hostname": r.Host,
|
2023-02-06 04:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
2023-09-09 01:46:36 +00:00
|
|
|
|
|
|
|
// LogoutPage returns the HTML template for the logout page.
|
|
|
|
func LogoutPage() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Load the template, TODO: once on server startup.
|
|
|
|
tmpl := template.New("index")
|
|
|
|
tmpl, err := tmpl.ParseFiles("web/templates/logout.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
tmpl.ExecuteTemplate(w, "index", nil)
|
|
|
|
})
|
|
|
|
}
|