Skip to content

Commit

Permalink
Allow for 3 seconds for grace before challenge is fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Jun 10, 2024
1 parent 8dbaed1 commit d7e727d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 5 additions & 1 deletion internal/router/session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,26 @@ func (c *Challenger) Challenge(address string) error {

err := conn.SetWriteDeadline(time.Now().Add(2 * time.Second))
if err != nil {
conn.Close()
return err
}

err = conn.WriteJSON("challenge")
if err != nil {
conn.Close()
return err
}

err = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
if err != nil {
conn.Close()
return err
}

msg := struct{ Challenge string }{}
err = conn.ReadJSON(&msg)
if err != nil {
conn.Close()
return err
}

Expand Down Expand Up @@ -153,7 +157,7 @@ func (c *Challenger) WS(w http.ResponseWriter, r *http.Request) {

err = c.Challenge(remoteAddress.String())
if err != nil {
log.Printf("client did not complete ws challenge: %s", err)
log.Printf("client did not complete inital ws challenge: %s", err)
return
}

Expand Down
14 changes: 12 additions & 2 deletions internal/router/statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/NHAS/wag/internal/acls"
"github.com/NHAS/wag/internal/data"
Expand Down Expand Up @@ -103,15 +104,24 @@ func deviceChanges(_ string, current, previous data.Device, et data.EventType) e

log.Printf("challenging %s:%s device, as endpoint changed: %s -> %s", current.Username, current.Address, current.Endpoint.String(), previous.Endpoint.String())
// Will take at most 4 seconds
err := Verifier.Challenge(current.Address)
if err != nil {

attempts := 0
for ; attempts < 3; attempts++ {
err = Verifier.Challenge(current.Address)
if err != nil {
time.Sleep(1 * time.Second)
}
}

if attempts >= 3 {
log.Printf("%s:%s failed to pass websockets challenge: %s", current.Username, current.Address, err)
err := Deauthenticate(current.Address)
if err != nil {
return fmt.Errorf("cannot deauthenticate device %s: %s", current.Address, err)
}
} else {
log.Printf("%s:%s device succeeded challenge", current.Username, current.Address)

}
}

Expand Down

0 comments on commit d7e727d

Please sign in to comment.