Skip to content

Commit e5c5d23

Browse files
committed
Fix bug where a client could not authorize
1 parent d6243b6 commit e5c5d23

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

internal/data/devices.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
"github.com/NHAS/wag/internal/config"
1314
"github.com/NHAS/wag/internal/utils"
@@ -24,7 +25,7 @@ type Device struct {
2425
Endpoint *net.UDPAddr
2526
Attempts int
2627
Active bool
27-
Authorised bool
28+
Authorised time.Time
2829
}
2930

3031
func stringToUDPaddr(address string) (r *net.UDPAddr) {
@@ -96,6 +97,7 @@ func GetDevice(username, id string) (device Device, err error) {
9697
return
9798
}
9899

100+
// Set device as authorized and clear authentication attempts
99101
func AuthoriseDevice(username, address string) error {
100102
return doSafeUpdate(context.Background(), deviceKey(username, address), func(gr *clientv3.GetResponse) (string, bool, error) {
101103
if len(gr.Kvs) != 1 {
@@ -114,7 +116,12 @@ func AuthoriseDevice(username, address string) error {
114116
return "", false, err
115117
}
116118

117-
device.Authorised = !u.Locked
119+
if u.Locked {
120+
return "", false, errors.New("account is locked")
121+
}
122+
123+
device.Authorised = time.Now()
124+
device.Attempts = 0
118125

119126
b, _ := json.Marshal(device)
120127

@@ -144,7 +151,7 @@ func DeauthenticateDevice(address string) error {
144151
return "", false, err
145152
}
146153

147-
device.Authorised = false
154+
device.Authorised = time.Time{}
148155

149156
b, _ := json.Marshal(device)
150157

internal/data/events.go

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func addWatcher[I any, T WatcherFuncType[I]](watcher T, existingWatches *[]T) {
6464
func execWatchers[I any, T WatcherFuncType[I]](watchers []T, data I, state int) {
6565
lck.RLock()
6666

67-
log.Println(len(watchers), data)
6867
for _, watcher := range watchers {
6968
go watcher(data, state)
7069
}

internal/router/statemachine.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ func handleEvents(erroChan chan<- error) {
1818
}
1919

2020
func deviceChanges(device data.BasicEvent[data.Device], state int) {
21+
22+
log.Printf("state: %d, event: %+v", state, device)
23+
2124
switch state {
2225
case data.DELETED:
2326
err := RemovePeer(device.CurrentValue.Publickey, device.CurrentValue.Address)
@@ -42,16 +45,21 @@ func deviceChanges(device data.BasicEvent[data.Device], state int) {
4245
}
4346
}
4447

45-
if (device.CurrentValue.Attempts != device.Previous.Attempts && device.CurrentValue.Attempts > config.Values().Lockout) ||
46-
device.CurrentValue.Endpoint.String() != device.Previous.Endpoint.String() {
48+
if (device.CurrentValue.Attempts != device.Previous.Attempts && device.CurrentValue.Attempts > config.Values().Lockout) || // If the number of authentication attempts on a device has exceeded the max
49+
device.CurrentValue.Endpoint.String() != device.Previous.Endpoint.String() || // If the client ip has changed
50+
device.CurrentValue.Authorised.IsZero() { // If we've explicitly deauthorised a device
4751
err := Deauthenticate(device.CurrentValue.Address)
4852
if err != nil {
4953
log.Println(err)
5054
}
5155
}
5256

5357
if device.CurrentValue.Authorised != device.Previous.Authorised {
54-
if device.CurrentValue.Attempts <= config.Values().Lockout {
58+
log.Println("authorisation state changed on device")
59+
60+
if !device.CurrentValue.Authorised.IsZero() && device.CurrentValue.Attempts <= config.Values().Lockout {
61+
62+
log.Println("authorising device")
5563
err := SetAuthorized(device.CurrentValue.Address, device.CurrentValue.Username)
5664
if err != nil {
5765
log.Println(err)

internal/users/user.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package users
33
import (
44
"errors"
55
"fmt"
6+
"log"
67
"net"
78

89
"github.com/NHAS/wag/internal/config"
@@ -140,19 +141,17 @@ func (u *user) Authenticate(device, mfaType string, authenticator authenticators
140141
}
141142
}
142143

143-
err = u.ResetDeviceAuthAttempts(device)
144-
if err != nil {
145-
return fmt.Errorf("%s %s unable to reset number of mfa attempts: %s", u.Username, device, err)
146-
}
147-
148144
err = data.AuthoriseDevice(u.Username, device)
149145
if err != nil {
150146
return fmt.Errorf("%s %s unable to reset number of mfa attempts: %s", u.Username, device, err)
151147
}
152148

149+
log.Println("untrace")
153150
return nil
154151
}
155152

153+
//
154+
156155
func (u *user) Deauthenticate(device string) error {
157156
return data.DeauthenticateDevice(device)
158157
}

0 commit comments

Comments
 (0)