From 8e67abe0c0fe8446ba6ca3b7c0aedbc8a5040d7b Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 22 Feb 2019 19:09:38 -0700 Subject: [PATCH] Now you can hit enter to sign in --- devel/devel-server.py | 64 +++++++++++++++++++++++++++++++++---------- src/handlers.go | 14 +++++----- src/instance.go | 4 +-- src/mothd.go | 7 +---- theme/index.html | 8 ++++-- theme/logout.html | 2 +- theme/moth.js | 20 ++++++++++---- theme/puzzle.html | 2 +- theme/scoreboard.html | 2 +- 9 files changed, 80 insertions(+), 43 deletions(-) diff --git a/devel/devel-server.py b/devel/devel-server.py index af0679b..041ba7b 100755 --- a/devel/devel-server.py +++ b/devel/devel-server.py @@ -28,6 +28,16 @@ def get_seed(request): else: return int(seedstr) + +def get_puzzle(request): + seed = get_seed(request) + category = request.match_info.get("category") + points = int(request.match_info.get("points")) + filename = request.match_info.get("filename") + cat = moth.Category(request.app["puzzles_dir"].joinpath(category), seed) + puzzle = cat.puzzle(points) + return puzzle + async def handle_puzzlelist(request): seed = get_seed(request) @@ -87,6 +97,16 @@ async def handle_puzzlefile(request): content_type=content_type, ) + +async def handle_answer(request): + seed = get_seed(request) + category = request.match_info.get("category") + points = int(request.match_info.get("points")) + filename = request.match_info.get("filename") + cat = moth.Category(request.app["puzzles_dir"].joinpath(category), seed) + puzzle = cat.puzzle(points) + + async def handle_mothballer(request): seed = get_seed(request) @@ -113,19 +133,37 @@ async def handle_index(request): seed = random.getrandbits(32) body = """ - Dev Server + + Dev Server + +

Dev Server

+

- You need to provide the contest seed in the URL. - If you don't have a contest seed in mind, - why not try {seed}? + Pick a seed:

+ +

- If you are chaotic, - you could even take your chances with a - random seed for every HTTP request. - This means generated files will get a different seed than the puzzle itself! + Puzzles can be generated from Python code: these puzzles can use a random number generator if they want. + The seed is used to create these random numbers. +

+ +

+ We like to make a new seed for every contest, + and re-use that seed whenever we regenerate a category during an event + (say to fix a bug). + By using the same seed, + we make sure that all the dynamically-generated puzzles have the same values + in any new packages we build.

@@ -140,12 +178,8 @@ async def handle_static(request): themes = request.app["theme_dir"] fn = request.match_info.get("filename") if not fn: - for fn in ("puzzle-list.html", "index.html"): - path = themes.joinpath(fn) - if path.exists(): - break - else: - path = themes.joinpath(fn) + fn = "index.html" + path = themes.joinpath(fn) return web.FileResponse(path) @@ -182,7 +216,7 @@ if __name__ == '__main__': app["puzzles_dir"] = pathlib.Path(args.puzzles) app["theme_dir"] = pathlib.Path(args.theme) app.router.add_route("GET", "/", handle_index) - app.router.add_route("GET", "/{seed}/puzzles.json", handle_puzzlelist) + app.router.add_route("*", "/{seed}/puzzles.json", handle_puzzlelist) app.router.add_route("GET", "/{seed}/content/{category}/{points}/puzzle.json", handle_puzzle) app.router.add_route("GET", "/{seed}/content/{category}/{points}/{filename}", handle_puzzlefile) app.router.add_route("GET", "/{seed}/mothballer/{category}", handle_mothballer) diff --git a/src/handlers.go b/src/handlers.go index 89643b2..950d45a 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -175,6 +175,12 @@ func (ctx *Instance) answerHandler(w http.ResponseWriter, req *http.Request) { } func (ctx *Instance) puzzlesHandler(w http.ResponseWriter, req *http.Request) { + teamid := req.FormValue("id") + if _, err := ctx.TeamName(teamid); err != nil { + http.Error(w, "Unauthorized: must provide team ID", http.StatusUnauthorized) + return + } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(ctx.jPuzzleList) @@ -273,13 +279,7 @@ func (ctx *Instance) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) { w: wOrig, statusCode: new(int), } - w.Header().Set("WWW-Authenticate", "Basic") - _, password, _ := r.BasicAuth() - if password != ctx.Password { - http.Error(w, "Authentication Required", 401) - } else { - ctx.mux.ServeHTTP(w, r) - } + ctx.mux.ServeHTTP(w, r) log.Printf( "%s %s %s %d\n", r.RemoteAddr, diff --git a/src/instance.go b/src/instance.go index c226869..bd89e5d 100644 --- a/src/instance.go +++ b/src/instance.go @@ -19,7 +19,6 @@ type Instance struct { MothballDir string StateDir string ResourcesDir string - Password string Categories map[string]*Mothball update chan bool jPuzzleList []byte @@ -27,13 +26,12 @@ type Instance struct { mux *http.ServeMux } -func NewInstance(base, mothballDir, stateDir, resourcesDir, password string) (*Instance, error) { +func NewInstance(base, mothballDir, stateDir, resourcesDir string) (*Instance, error) { ctx := &Instance{ Base: strings.TrimRight(base, "/"), MothballDir: mothballDir, StateDir: stateDir, ResourcesDir: resourcesDir, - Password: password, Categories: map[string]*Mothball{}, update: make(chan bool, 10), mux: http.NewServeMux(), diff --git a/src/mothd.go b/src/mothd.go index b53bd78..46b7d42 100644 --- a/src/mothd.go +++ b/src/mothd.go @@ -37,11 +37,6 @@ func main() { "/theme", "Path to static theme resources (HTML, images, css, ...)", ) - password := flag.String( - "password", - "sesame", - "Pass Word (in the 1920s sense) to view the site. Not a secure passphrase.", - ) maintenanceInterval := flag.Duration( "maint", 20*time.Second, @@ -58,7 +53,7 @@ func main() { log.Fatal(err) } - ctx, err := NewInstance(*base, *mothballDir, *stateDir, *themeDir, *password) + ctx, err := NewInstance(*base, *mothballDir, *stateDir, *themeDir) if err != nil { log.Fatal(err) } diff --git a/theme/index.html b/theme/index.html index 6a69e85..89bdf48 100644 --- a/theme/index.html +++ b/theme/index.html @@ -2,6 +2,7 @@ MOTH + @@ -11,11 +12,11 @@
-
+
Team name: Team ID:
- -
+ +
@@ -23,6 +24,7 @@ diff --git a/theme/logout.html b/theme/logout.html index 6aaad26..14f3caf 100644 --- a/theme/logout.html +++ b/theme/logout.html @@ -15,7 +15,7 @@ sessionStorage.removeItem("id")
diff --git a/theme/moth.js b/theme/moth.js index bec145e..e81f791 100644 --- a/theme/moth.js +++ b/theme/moth.js @@ -64,6 +64,7 @@ function renderPuzzles(obj) { i.appendChild(a) a.textContent = points a.href = "puzzle.html?cat=" + cat + "&points=" + points + "&pid=" + id + a.target = "_blank" } } @@ -76,13 +77,10 @@ function renderPuzzles(obj) { container.firstChild.remove() } container.appendChild(puzzlesElement) - container.style.display = "none" - - document.getElementById("login").style.display = "block" } function heartbeat(teamId) { - rpc("puzzles.json", {teamid: teamId}) + rpc("puzzles.json", {id: teamId}) .then(resp => { if (resp.ok) { resp.json() @@ -103,16 +101,20 @@ function showPuzzles(teamId) { let spinner = document.createElement("span") spinner.classList.add("spinner") + sessionStorage.setItem("id", teamId) + document.getElementById("login").style.display = "none" document.getElementById("puzzles").appendChild(spinner) heartbeat(teamId) setInterval(e => { heartbeat(teamId) }, 40000) } -function login() { +function login(e) { let name = document.querySelector("[name=name]").value let id = document.querySelector("[name=id]").value + e.preventDefault() + rpc("register", { name: name, id: id, @@ -158,7 +160,13 @@ function toast(message, timeout=5000) { } function init() { - document.getElementById("submit").addEventListener("click", login) + // Already signed in? + let id = sessionStorage.getItem("id") + if (id) { + showPuzzles(id) + } + + document.getElementById("login").addEventListener("submit", login) } if (document.readyState === "loading") { diff --git a/theme/puzzle.html b/theme/puzzle.html index 5aa19b9..5c0f3f1 100644 --- a/theme/puzzle.html +++ b/theme/puzzle.html @@ -94,7 +94,7 @@ if (document.readyState === "loading") {
diff --git a/theme/scoreboard.html b/theme/scoreboard.html index db6531a..fcbbc5b 100644 --- a/theme/scoreboard.html +++ b/theme/scoreboard.html @@ -128,7 +128,7 @@ if (document.readyState === "loading") {