Files
aura-crm/production/backend/internal/auth/jwt.go
T

37 lines
941 B
Go

// Package auth verifies staff JWTs issued by core — production has no login
// of its own and shares JWT_SECRET (HS256) with core out of band.
package auth
import (
"fmt"
"os"
"github.com/golang-jwt/jwt/v5"
)
type Claims struct {
StaffID string `json:"staff_id"`
Name string `json:"name"`
Role string `json:"role"`
Permissions []string `json:"permissions"`
jwt.RegisteredClaims
}
func ParseToken(tokenStr string) (*Claims, error) {
secret := os.Getenv("JWT_SECRET")
token, err := jwt.ParseWithClaims(tokenStr, &Claims{}, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
}
return []byte(secret), nil
})
if err != nil {
return nil, err
}
claims, ok := token.Claims.(*Claims)
if !ok || !token.Valid {
return nil, fmt.Errorf("invalid token")
}
return claims, nil
}