Skip to content

Commit

Permalink
Add auth check wrapper around most endpoints, fix ping causing an unm…
Browse files Browse the repository at this point in the history
…arshal error, and move UI testing out of default
  • Loading branch information
joeyak committed Jan 24, 2023
1 parent 80576b3 commit de25205
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
- name: test
run: go test ./...

- name: test ui
run: go test ./... -tags=test_ui

build:
runs-on: ubuntu-latest

Expand Down
27 changes: 18 additions & 9 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
return
}

if data.Type == common.CdPing {
continue
}

var joinData common.JoinData
err = json.Unmarshal([]byte(data.Message), &joinData)
if err != nil {
common.LogInfof("[handler] Could not unmarshal join data %#v: %v\n", data.Message, err)
common.LogInfof("[handler] Could not unmarshal websocket %d data %#v: %v\n", data.Type, data.Message, err)
continue
}

Expand Down Expand Up @@ -315,14 +319,6 @@ func handleEmoteTemplate(w http.ResponseWriter, r *http.Request) {
}

func handleIndexTemplate(w http.ResponseWriter, r *http.Request) {
if settings.RoomAccess != AccessOpen {
if !checkRoomAccess(w, r) {
common.LogDebugln("Denied access")
return
}
common.LogDebugln("Granted access")
}

type Data struct {
Video, Chat bool
MessageHistoryCount int
Expand Down Expand Up @@ -473,3 +469,16 @@ func handleDefault(w http.ResponseWriter, r *http.Request) {
handleIndexTemplate(w, r)
}
}

func wrapAuth(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if settings.RoomAccess != AccessOpen {
if !checkRoomAccess(w, r) {
common.LogDebugln("Denied access")
return
}
common.LogDebugln("Granted access")
}
next.ServeHTTP(w, r)
})
}
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,17 @@ func run(args args) {

router := http.NewServeMux()

router.HandleFunc("/ws", wsHandler) // Chat websocket
router.Handle("/static/", http.FileServer(http.FS(staticFsys)))
router.HandleFunc("/emotes/", wsEmotes)
router.HandleFunc("/chat", handleIndexTemplate)
router.HandleFunc("/video", handleIndexTemplate)
router.HandleFunc("/help", handleHelpTemplate)
router.HandleFunc("/emotes", handleEmoteTemplate)

router.HandleFunc("/live", handleLive)
router.HandleFunc("/", handleDefault)
router.HandleFunc("/ws", wrapAuth(wsHandler)) // Chat websocket
router.HandleFunc("/chat", wrapAuth(handleIndexTemplate))
router.HandleFunc("/video", wrapAuth(handleIndexTemplate))
router.HandleFunc("/help", wrapAuth(handleHelpTemplate))
router.HandleFunc("/emotes", wrapAuth(handleEmoteTemplate))

router.HandleFunc("/live", wrapAuth(handleLive))
router.HandleFunc("/", wrapAuth(handleDefault))

httpServer := &http.Server{
Addr: args.Addr,
Expand Down
2 changes: 2 additions & 0 deletions main_test.go → ui_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build test_ui

package main

import (
Expand Down

0 comments on commit de25205

Please sign in to comment.