Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion iznik-server-go/user/authMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/freegle/iznik-server-go/utils"
"github.com/getsentry/sentry-go"
"github.com/gofiber/fiber/v2"
"gorm.io/plugin/dbresolver"
"sync"
"time"
)
Expand Down Expand Up @@ -41,8 +42,19 @@ func NewAuthMiddleware(config Config) fiber.Handler {

// We have a uid. Check if the user is still present in the DB.
// Also fetch systemrole for HAProxy rate limit exemption.
result := db.Raw("SELECT users.id, users.lastaccess, users.systemrole FROM sessions INNER JOIN users ON users.id = sessions.userid WHERE sessions.id = ? AND users.id = ? LIMIT 1;", sessionIdInJWT, userIdInJWT).Scan(&userIdInDB)
const q = "SELECT users.id, users.lastaccess, users.systemrole FROM sessions INNER JOIN users ON users.id = sessions.userid WHERE sessions.id = ? AND users.id = ? LIMIT 1;"
result := db.Raw(q, sessionIdInJWT, userIdInJWT).Scan(&userIdInDB)
dbQueryErr = result.Error

// Read/write split: the session row is INSERTed on the write host at login, so a
// read replica can momentarily return zero rows right after login (Galera apply
// lag). That would make the check below wrongly 401 a valid session. On a miss,
// confirm against the write host before treating the session as invalid.
// .Clauses(dbresolver.Write) is a no-op when no replica is configured.
if dbQueryErr == nil && userIdInDB.Id == 0 {
result = db.Clauses(dbresolver.Write).Raw(q, sessionIdInJWT, userIdInJWT).Scan(&userIdInDB)
dbQueryErr = result.Error
}
}()
}

Expand Down