all repos — felt @ 50ea1c2b125b01fe8d1f6c7574dffa4aa97665da

virtual tabletop for dungeons and dragons (and similar) using Go, MongoDB, and websockets

doing a bit of work on this, we can do admin login now ayyy
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmPSFlUACgkQO3+8IhRO
Y5jgOA/+McDSIYFd01XOZ3cAQTvkOWYR8nblpUxRe0E2Mws5B8JvgowcjOr/aID1
cdxdjojjb2JaHsFOqzvdPo5m5K8fvZrdhz1p6faLmHVIcgP5QucyNOoVh/1lQyfa
daapL0GS84uSp0BJvYEJGwwE0ENHyez9w9RSZyyLSUJdDjz+xCP6s/v2cAZbcCZs
lcb8s06VxbvcDuXo5d8So3tZ55NRFKvyp0KbijvPFB2zmTJeqApLQ03OHJEygalt
Gi9JWH6oSBuArPJibQUeW+32BGklV0Gpcb+ssuxcQQxLOrzEEFBNwSVDetYqHifm
eD4lG2Cp+xkR+xAAfZi7q9R5ynh4pIr1KR4TLi/Rsi4CWzWJdUdkklZCEAR1qf++
6/HaCW6gx+Soj0n5m1rIZ5TG/IwkMTo4pArTUm0FtCpHuAfy4T5bx2eaIhkuHXEW
sbWHh04MKuVUadQaJGDHfkFGCuaegag0TaSwUeksAhpxz5Tg8xK3JhdQCE/D4/zz
5CilDZK81fCSvexaOC/FO+f+qT5UrjC9dm/G4ooVvspV1Ul7zPdsuMorf5ofcJpB
ob2HwFEaVLzVkWme8Hi2qC+HW7H2OGxZg47guG2xN6R49Yr+UWfJLETmm7n4mLpl
KoeYrnWW5AT4C9z4169fFdVJ+7njuTCOjCJDAjtu08Czfc5oG5o=
=rEjL
-----END PGP SIGNATURE-----
commit

50ea1c2b125b01fe8d1f6c7574dffa4aa97665da

parent

7f18668f44cae07c9bc3b2bdc77e8267217a2a5e

7 files changed, 61 insertions(+), 5 deletions(-)

jump to
M .gitignore.gitignore

@@ -1,3 +1,4 @@

felt +user.db mongodb/data/* mongodb/.env
M admin/admin.goadmin/admin.go

@@ -1,6 +1,7 @@

package admin import ( + "html/template" "net/http" "nilfm.cc/git/felt/admin/util" "nilfm.cc/git/felt/models"

@@ -145,7 +146,7 @@ }

func CreateAdminInterface(udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler { // create quartzgun router - rtr := &router.Router{} + rtr := &router.Router{ Fallback: *template.Must(template.ParseFiles("static/error.html")) } scopes := map[string]string{}
M gametable/server.gogametable/server.go

@@ -13,6 +13,7 @@ "nilfm.cc/git/felt/models"

"nilfm.cc/git/felt/mongodb" "nilfm.cc/git/quartzgun/auth" "nilfm.cc/git/quartzgun/cookie" + "nilfm.cc/git/quartzgun/renderer" "sync" "time" )

@@ -40,8 +41,8 @@ subscribers: make(map[*Subscriber]models.TableKey),

publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8), dbAdapter: adapter, } - srvr.serveMux.Handle("/table/", http.FileServer(http.Dir("./static"))) - srvr.serveMux.Handle("/admin/", admin.CreateAdminInterface(udb, adapter)) + srvr.serveMux.Handle("/table/", http.StripPrefix("/table/", renderer.Subtree("./static"))) + srvr.serveMux.Handle("/admin/", http.StripPrefix("/admin", admin.CreateAdminInterface(udb, adapter))) srvr.serveMux.HandleFunc("/subscribe", srvr.subscribeHandler) srvr.serveMux.HandleFunc("/publish", srvr.publishHandler)
M main.gomain.go

@@ -34,6 +34,8 @@ }

udb := indentalUserDB.CreateIndentalUserDB( "./user.db") + + udb.AddUser("nilix", "questing") gt := gametable.New(dbEngine, udb) s := &http.Server{
A static/admin.js

@@ -0,0 +1,36 @@

+let adminToken = null; + +async function doLogin() { + const adminUsrInput = document.getElementById("input_admin_usr"); + const adminPassInput = document.getElementById("input_admin_pass"); + const errDiv = document.getElementById("loginErr"); + + if (adminUsrInput && adminPassInput) { + adminToken = await getAdminToken(adminUsrInput.value, adminPassInput.value); + if (adminToken) { + // render admin interface + } else { + if (errDiv) { + errDiv.innerHTML = "Incorrect credentials"; + } + } + } +} + +async function getAdminToken(user, pass) { + const headers = new Headers(); + headers.set('Authorization', 'Basic ' + btoa(user + ":" + pass)); + try { + const res = await fetch('/admin/api/auth/', { + method: 'POST', + headers: headers + }); + + if (res.ok) { + return await res.json(); + } + return null; + } catch (err) { + return null; + } +}
A static/error.html

@@ -0,0 +1,8 @@

+{{ $params := (.Context).Value "params" }} + +<html> +<body> +<h1>ERR {{ $params.ErrorCode }}</h1> +<p>{{ $params.ErrorMessage }}</p> +</body> +</html>
M static/index.htmlstatic/index.html

@@ -4,7 +4,9 @@ <head>

<meta charset="UTF-8" /> <title>Felt</title> <meta name="viewport" content="width=device-width" /> - <link href="/style.css" rel="stylesheet" /> + <link href="./style.css" rel="stylesheet" /> + <script src="./admin.js" type="text/javascript"></script> + <script src="./index.js" type="text/javascript"></script> </head> <body> <nav>

@@ -12,7 +14,12 @@ <input id="name_entry">

<button id="goto_table">Change Table</button> <button id="admin_login">Admin Login</button> </nav> - <div id="dynamic_modal"></div> + <div id="admin_modal"> + <label>usr<input id="input_admin_usr"></label> + <label>pass<input type="password" id="input_admin_pass"></label> + <button id="admin_login" onclick="doLogin()">login</button> + <div id="loginErr"></div> + </div> <div id="dice_log"></div> <select name="num_dice"> <option>1</option>