Harden auth: same-origin check, live admin flag, 90-day refresh
- Refuse non-GET requests whose Origin (or Sec-Fetch-Site) is not the apex or www. SameSite=Lax lets sibling subdomains such as radar. and git. send the auth cookies. The WebSocket check shares the helper and no longer accepts localhost in production. - Take the admin flag from the user row the token check already loads, so a demotion applies on the next request, not on token expiry. - Cut the refresh token lifetime from 365 to 90 days; refreshes renew it. - Docs: fix the stale revocation notes in backend/README.md and the Cloudflare Access comment in routes.caddy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -41,8 +41,8 @@ func InitAuth(config *AuthConfig) *Auth {
|
||||
return &Auth{Config: config}
|
||||
}
|
||||
|
||||
// GenerateJWT issues an access/refresh pair. Only the access token carries
|
||||
// admin; refresh re-reads the row, so a revoked admin flag applies then.
|
||||
// GenerateJWT issues an access/refresh pair. The admin claim is informational:
|
||||
// AuthenticateTokenWithClaims overwrites it from the user row.
|
||||
func (auth *Auth) GenerateJWT(user *models.User) (*Tokens, error) {
|
||||
accessToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"username": user.Username,
|
||||
@@ -140,6 +140,10 @@ func (auth *Auth) AuthenticateTokenWithClaims(db *gorm.DB, tokenStr string) (*mo
|
||||
return nil, nil, ErrTokenRevoked
|
||||
}
|
||||
|
||||
// The row is already loaded, so it decides admin: a demotion applies on
|
||||
// the next request rather than when the token expires.
|
||||
(*claims)["admin"] = user.Admin
|
||||
|
||||
return &user, claims, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user