Readability pass across all four areas of the stack. Comments explain why rather than what, concentrated at the points where behaviour is non-obvious: concurrency, caching, auth, scraping and nginx templating. Backend: doc comments on every exported declaration across handlers, services, models and resolvers. Resolver commentary lives inside function bodies so gqlgen does not strip it on regeneration; schema files use # comments rather than description blocks so introspection output is unchanged. Extracted the duplicated auth-cookie blocks into helpers. Frontend: header comments on every component, store and view, including STATUS: notes marking which of the parallel/legacy views are still live. Documented the non-reactive animation-state pattern, the homeData hub and the WebSocket stale-socket guard. Removed a few dead imports and named the reconnect-backoff and poll-interval constants. Python: module and function docstrings covering the Travelodge scraping contract, the deadline threading, the two-executor concurrency and the NDJSON streaming protocol. Infra: header blocks on every compose file, nginx template, Dockerfile and entrypoint, covering the config selection, envsubst allow-lists, rate-limit semantics and volume persistence. Hoisted the duplicated envsubst variable list in nginx/entrypoint.sh. New: backend/README.md, python/README.md, DEPLOYMENT.md, .env.example, backend/graph/doc.go. Refreshed vue/README.md. Corrected CLAUDE.md, which stated a 24h access token where main.go sets 7 days. No behaviour change. go build/vet, vite build, py_compile and docker compose config all pass. Co-Authored-By: Claude Opus 5 <[email protected]>
133 lines
4.1 KiB
Go
133 lines
4.1 KiB
Go
package graph
|
|
|
|
// This file will be automatically regenerated based on the schema, any resolver
|
|
// implementations
|
|
// will be copied through when generating and any unknown code will be moved to the end.
|
|
// Code generated by github.com/99designs/gqlgen version v0.17.95
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"adam-french.co.uk/backend/graph/model"
|
|
"adam-french.co.uk/backend/models"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// 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.
|
|
gc := GinContextFromCtx(ctx)
|
|
if gc == nil {
|
|
return nil, fmt.Errorf("could not get gin context")
|
|
}
|
|
|
|
if !r.Store.LoginLimiter.Allow(gc.ClientIP()) {
|
|
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")
|
|
}
|
|
|
|
if err := bcrypt.CompareHashAndPassword(user.Password, []byte(input.Password)); err != nil {
|
|
return nil, fmt.Errorf("invalid credentials")
|
|
}
|
|
|
|
tokens, err := r.Store.Auth.GenerateJWT(&user)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to generate tokens")
|
|
}
|
|
|
|
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.
|
|
return &model.AuthPayload{User: &user}, nil
|
|
}
|
|
|
|
// Logout is the resolver for the logout field.
|
|
func (r *mutationResolver) Logout(ctx context.Context) (bool, error) {
|
|
gc := GinContextFromCtx(ctx)
|
|
if gc == nil {
|
|
return false, fmt.Errorf("could not get gin context")
|
|
}
|
|
|
|
// Max-age -1 tells the browser to drop the cookies. Nothing is
|
|
// invalidated server-side, so a token captured beforehand still works
|
|
// until it expires.
|
|
gc.SetSameSite(http.SameSiteLaxMode)
|
|
gc.SetCookie("access_token", "", -1, "/", r.Store.Auth.Config.Domain, true, true)
|
|
gc.SetCookie("refresh_token", "", -1, "/", r.Store.Auth.Config.Domain, true, true)
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// RefreshToken is the resolver for the refreshToken field.
|
|
func (r *mutationResolver) RefreshToken(ctx context.Context) (*model.AuthPayload, error) {
|
|
gc := GinContextFromCtx(ctx)
|
|
if gc == nil {
|
|
return nil, fmt.Errorf("could not get gin context")
|
|
}
|
|
|
|
refreshToken, err := gc.Cookie("refresh_token")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unauthorized")
|
|
}
|
|
|
|
claims, err := r.Store.Auth.VerifyJWT(refreshToken)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unauthorized")
|
|
}
|
|
|
|
userIDF, ok := (*claims)["id"].(float64)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid token claims")
|
|
}
|
|
|
|
// Setting the primary key and calling First with no condition makes GORM
|
|
// look the row up by that key. Admin status is re-read from the database
|
|
// here, which is what lets a revoked admin flag take effect on refresh.
|
|
var user models.User
|
|
user.ID = uint(userIDF)
|
|
if err := r.Store.DB.First(&user).Error; err != nil {
|
|
return nil, fmt.Errorf("user not found")
|
|
}
|
|
|
|
tokens, err := r.Store.Auth.GenerateJWT(&user)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to generate tokens")
|
|
}
|
|
|
|
setAuthCookies(gc, r.Store.Auth.Config, tokens)
|
|
|
|
return &model.AuthPayload{User: &user}, nil
|
|
}
|
|
|
|
// Me is the resolver for the me field.
|
|
func (r *queryResolver) Me(ctx context.Context) (*models.User, error) {
|
|
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 {
|
|
return nil, fmt.Errorf("user not found")
|
|
}
|
|
|
|
return &user, nil
|
|
}
|