Implement email/password auth with JWT sessions and API key auth for sandbox lifecycle. Users get a default team on signup; sandboxes, snapshots, and API keys are scoped to teams. - Add user, team, users_teams, and team_api_keys tables (goose migrations) - Add JWT middleware (Bearer token) for user management endpoints - Add API key middleware (X-API-Key header, SHA-256 hashed) for sandbox ops - Add signup/login handlers with transactional user+team creation - Add API key CRUD endpoints (create/list/delete) - Replace owner_id with team_id on sandboxes and templates - Update all handlers to use team-scoped queries - Add godotenv for .env file loading - Update OpenAPI spec and test UI with auth flows
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package auth
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
const jwtExpiry = 6 * time.Hour
|
|
|
|
// Claims are the JWT payload.
|
|
type Claims struct {
|
|
TeamID string `json:"team_id"`
|
|
Email string `json:"email"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
// SignJWT signs a new 6-hour JWT for the given user.
|
|
func SignJWT(secret []byte, userID, teamID, email string) (string, error) {
|
|
now := time.Now()
|
|
claims := Claims{
|
|
TeamID: teamID,
|
|
Email: email,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
Subject: userID,
|
|
IssuedAt: jwt.NewNumericDate(now),
|
|
ExpiresAt: jwt.NewNumericDate(now.Add(jwtExpiry)),
|
|
},
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
return token.SignedString(secret)
|
|
}
|
|
|
|
// VerifyJWT parses and validates a JWT, returning the claims on success.
|
|
func VerifyJWT(secret []byte, tokenStr string) (Claims, error) {
|
|
token, err := jwt.ParseWithClaims(tokenStr, &Claims{}, func(t *jwt.Token) (any, error) {
|
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
|
|
}
|
|
return secret, nil
|
|
})
|
|
if err != nil {
|
|
return Claims{}, fmt.Errorf("invalid token: %w", err)
|
|
}
|
|
c, ok := token.Claims.(*Claims)
|
|
if !ok || !token.Valid {
|
|
return Claims{}, fmt.Errorf("invalid token claims")
|
|
}
|
|
return *c, nil
|
|
}
|