Skip to content

Commit be5289e

Browse files
committed
fix: handle auth usernames and session expiry
1 parent d39339a commit be5289e

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
@@ -11,6 +11,7 @@ import (
1111
"encoding/json"
1212
"io/fs"
1313
"log"
14+
"net"
1415
"net/http"
1516
"os"
1617
"os/signal"
@@ -67,11 +68,12 @@ func verifySession(token string) bool {
6768
if len(sig) != len(expected) || subtle.ConstantTimeCompare(sig, expected) != 1 {
6869
return false
6970
}
70-
parts2 := strings.SplitN(string(payload), "@", 2)
71-
if len(parts2) != 2 {
71+
payloadStr := string(payload)
72+
atIdx := strings.LastIndex(payloadStr, "@")
73+
if atIdx < 0 {
7274
return false
7375
}
74-
expiry, err := strconv.ParseInt(parts2[1], 10, 64)
76+
expiry, err := strconv.ParseInt(payloadStr[atIdx+1:], 10, 64)
7577
if err != nil || time.Now().Unix() > expiry {
7678
return false
7779
}
@@ -108,6 +110,9 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
108110
return
109111
}
110112
ip := r.RemoteAddr
113+
if host, _, err := net.SplitHostPort(ip); err == nil {
114+
ip = host
115+
}
111116
loginMu.Lock()
112117
fe := failCount[ip]
113118
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)