Trim comments to the non-obvious why
Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -19,12 +19,7 @@ import (
|
||||
|
||||
// Login is the resolver for the login field.
|
||||
func (r *mutationResolver) Login(ctx context.Context, input model.LoginInput) (*model.AuthPayload, error) {
|
||||
// The Gin context is needed for two things GraphQL cannot express: the
|
||||
// client IP for rate limiting, and setting response cookies. It is
|
||||
// smuggled in by AuthContextMiddleware.
|
||||
//
|
||||
// This mutation mirrors handlers.Store.Login; the two share the limiter
|
||||
// and services.Auth, so the same 5/min budget covers both entry points.
|
||||
// Gin context for the client IP and cookies. Shares the REST login's limiter.
|
||||
gc := GinContextFromCtx(ctx)
|
||||
if gc == nil {
|
||||
return nil, fmt.Errorf("could not get gin context")
|
||||
@@ -34,8 +29,6 @@ func (r *mutationResolver) Login(ctx context.Context, input model.LoginInput) (*
|
||||
return nil, fmt.Errorf("too many login attempts, please try again later")
|
||||
}
|
||||
|
||||
// Identical message for unknown user and wrong password, so the API
|
||||
// cannot be used to enumerate usernames.
|
||||
var user models.User
|
||||
if err := r.Store.DB.Where("username = ?", input.Username).First(&user).Error; err != nil {
|
||||
return nil, fmt.Errorf("invalid credentials")
|
||||
@@ -52,8 +45,7 @@ func (r *mutationResolver) Login(ctx context.Context, input model.LoginInput) (*
|
||||
|
||||
services.SetAuthCookies(gc, r.Store.Auth.Config, tokens)
|
||||
|
||||
// The payload carries no token: the tokens only ever travel as HttpOnly
|
||||
// cookies, so client JavaScript never sees them.
|
||||
// Tokens travel only as HttpOnly cookies.
|
||||
return &model.AuthPayload{User: &user}, nil
|
||||
}
|
||||
|
||||
@@ -64,12 +56,9 @@ func (r *mutationResolver) Logout(ctx context.Context) (bool, error) {
|
||||
return false, fmt.Errorf("could not get gin context")
|
||||
}
|
||||
|
||||
// Bumping the token version is what actually revokes the tokens; clearing
|
||||
// the cookies only stops the browser presenting them. Both are best
|
||||
// effort and logout always reports success, matching the REST endpoint.
|
||||
// Best effort, always succeeds, like the REST endpoint.
|
||||
if userID, ok := UserIDFromCtx(ctx); ok {
|
||||
// The context only carries claims from a token that already
|
||||
// verified, so this cannot be used to revoke someone else's sessions.
|
||||
// Claims in ctx are already verified, so this can't target others.
|
||||
if err := services.BumpTokenVersion(r.Store.DB, userID); err != nil {
|
||||
log.Printf("logout: failed to revoke tokens for user %d: %v", userID, err)
|
||||
}
|
||||
@@ -92,11 +81,7 @@ func (r *mutationResolver) RefreshToken(ctx context.Context) (*model.AuthPayload
|
||||
return nil, fmt.Errorf("unauthorized")
|
||||
}
|
||||
|
||||
// Shared with the REST refresh endpoint, but the messages below are the
|
||||
// ones this resolver already returned, so the GraphQL error strings are
|
||||
// unchanged. Admin status and the token version are both re-read from the
|
||||
// database here, which is what lets a revoked admin flag or a logout take
|
||||
// effect on refresh.
|
||||
// Re-reads admin and token version from the DB.
|
||||
user, err := r.Store.Auth.AuthenticateToken(r.Store.DB, refreshToken)
|
||||
if err != nil {
|
||||
switch {
|
||||
@@ -121,16 +106,12 @@ func (r *mutationResolver) RefreshToken(ctx context.Context) (*model.AuthPayload
|
||||
|
||||
// Me is the resolver for the me field.
|
||||
func (r *queryResolver) Me(ctx context.Context) (*models.User, error) {
|
||||
// Checked here rather than with @signedIn because the message differs:
|
||||
// this query has always answered "unauthorized", and the directive's is
|
||||
// "authentication required".
|
||||
// Not @signedIn: this query's error has always been "unauthorized".
|
||||
userID, ok := UserIDFromCtx(ctx)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unauthorized")
|
||||
}
|
||||
|
||||
// Read fresh from the database rather than returned from the token
|
||||
// claims, so `me` reflects the current username and admin flag.
|
||||
var user models.User
|
||||
user.ID = userID
|
||||
if err := r.Store.DB.First(&user).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user