Files
podinfo/pkg/api/http/token.go
T
Stefan Prodan 5037beda6c Cap HTTP request and websocket message sizes
Unauthenticated clients could exhaust process memory (and, for /store,
disk) by posting arbitrarily large bodies, and could OOM or pin
goroutines via the /ws/echo websocket.

- Wrap request bodies in http.MaxBytesReader (10 MiB) on the echo,
  store, cache and token handlers via a shared readLimitedBody helper
- Bound /ws/echo: per-message read limit, idle read deadline with
  ping/pong keepalive, and write deadlines
- Add regression tests for both limits
2026-06-08 23:58:43 +03:00

126 lines
3.2 KiB
Go

package http
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/golang-jwt/jwt/v4"
)
type jwtCustomClaims struct {
Name string `json:"name"`
jwt.RegisteredClaims
}
// Token godoc
// @Summary Generate JWT token
// @Description issues a JWT token valid for one minute
// @Tags HTTP API
// @Accept json
// @Produce json
// @Router /token [post]
// @Success 200 {object} http.TokenResponse
func (s *Server) tokenGenerateHandler(w http.ResponseWriter, r *http.Request) {
_, span := s.tracer.Start(r.Context(), "tokenGenerateHandler")
defer span.End()
body, ok := s.readLimitedBody(w, r, span)
if !ok {
return
}
defer r.Body.Close()
user := "anonymous"
if len(body) > 0 {
user = string(body)
}
expiresAt := time.Now().Add(time.Minute * 1)
claims := &jwtCustomClaims{
user,
jwt.RegisteredClaims{
Issuer: "podinfo",
ExpiresAt: jwt.NewNumericDate(expiresAt),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
t, err := token.SignedString([]byte(s.config.JWTSecret))
if err != nil {
s.ErrorResponse(w, r, span, err.Error(), http.StatusBadRequest)
return
}
var result = TokenResponse{
Token: t,
ExpiresAt: claims.ExpiresAt.Time,
}
s.JSONResponse(w, r, result)
}
// TokenValidate godoc
// @Summary Validate JWT token
// @Description validates the JWT token
// @Tags HTTP API
// @Accept json
// @Produce json
// @Router /token/validate [post]
// @Success 200 {object} http.TokenValidationResponse
// @Failure 401 {string} string "Unauthorized"
// Get: JWT=$(curl -s -d 'test' localhost:9898/token | jq -r .token)
// Post: curl -H "Authorization: Bearer ${JWT}" localhost:9898/token/validate
func (s *Server) tokenValidateHandler(w http.ResponseWriter, r *http.Request) {
_, span := s.tracer.Start(r.Context(), "tokenValidateHandler")
defer span.End()
authorizationHeader := r.Header.Get("authorization")
if authorizationHeader == "" {
s.ErrorResponse(w, r, span, "authorization bearer header required", http.StatusUnauthorized)
return
}
bearerToken := strings.Split(authorizationHeader, " ")
if len(bearerToken) != 2 || strings.ToLower(bearerToken[0]) != "bearer" {
s.ErrorResponse(w, r, span, "authorization bearer header required", http.StatusUnauthorized)
return
}
claims := jwtCustomClaims{}
token, err := jwt.ParseWithClaims(bearerToken[1], &claims, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("invalid signing method")
}
return []byte(s.config.JWTSecret), nil
})
if err != nil {
s.ErrorResponse(w, r, span, err.Error(), http.StatusUnauthorized)
return
}
if token.Valid {
if claims.Issuer != "podinfo" {
s.ErrorResponse(w, r, span, "invalid issuer", http.StatusUnauthorized)
} else {
var result = TokenValidationResponse{
TokenName: claims.Name,
ExpiresAt: claims.ExpiresAt.Time,
}
s.JSONResponse(w, r, result)
}
} else {
s.ErrorResponse(w, r, span, "Invalid authorization token", http.StatusUnauthorized)
}
}
type TokenResponse struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
}
type TokenValidationResponse struct {
TokenName string `json:"token_name"`
ExpiresAt time.Time `json:"expires_at"`
}