Skip to content

Commit 0678617

Browse files
authored
Merge pull request redenfire#32 from NeoMod/pr/fix-auth-session-bugs
fix: handle auth usernames and session expiry
2 parents e45e776 + be5289e commit 0678617

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

‎main.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"errors"
1313
"io/fs"
1414
"log"
15+
"net"
1516
"net/http"
1617
"os"
1718
"os/signal"
@@ -68,11 +69,12 @@ func verifySession(token string) bool {
6869
if len(sig) != len(expected) || subtle.ConstantTimeCompare(sig, expected) != 1 {
6970
return false
7071
}
71-
parts2 := strings.SplitN(string(payload), "@", 2)
72-
if len(parts2) != 2 {
72+
payloadStr := string(payload)
73+
atIdx := strings.LastIndex(payloadStr, "@")
74+
if atIdx < 0 {
7375
return false
7476
}
75-
expiry, err := strconv.ParseInt(parts2[1], 10, 64)
77+
expiry, err := strconv.ParseInt(payloadStr[atIdx+1:], 10, 64)
7678
if err != nil || time.Now().Unix() > expiry {
7779
return false
7880
}
@@ -109,6 +111,9 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
109111
return
110112
}
111113
ip := r.RemoteAddr
114+
if host, _, err := net.SplitHostPort(ip); err == nil {
115+
ip = host
116+
}
112117
loginMu.Lock()
113118
fe := failCount[ip]
114119
now := time.Now()

‎web/index.html‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ <h1>🐳 <span id="appTitle">Docker Image Watcher</span> <small id="status"></sm
204204
bar.parentElement.classList.add('checking');
205205
try {
206206
const res = await fetch('/api/images');
207+
if (res.status === 401) {
208+
window.location.href = '/login.html';
209+
return;
210+
}
207211
const groups = await res.json();
208212
render(groups);
209213
bar.textContent = t.images(groups.length);

0 commit comments

Comments
 (0)