NewTokenAuthInterceptor creates a new auth interceptor. If token is empty and no JWT config is set, authentication is disabled. JWT is configured via CHATCLI_JWT_SECRET environment variable.
(token string, logger *zap.Logger)
| 40 | // If token is empty and no JWT config is set, authentication is disabled. |
| 41 | // JWT is configured via CHATCLI_JWT_SECRET environment variable. |
| 42 | func NewTokenAuthInterceptor(token string, logger *zap.Logger) *TokenAuthInterceptor { |
| 43 | ai := &TokenAuthInterceptor{ |
| 44 | token: token, |
| 45 | logger: logger, |
| 46 | failureLimiters: make(map[string]*rate.Limiter), |
| 47 | } |
| 48 | |
| 49 | // Load JWT secret from environment if available |
| 50 | if secret := os.Getenv("CHATCLI_JWT_SECRET"); secret != "" { |
| 51 | ai.jwtSecret = []byte(secret) |
| 52 | logger.Info("JWT authentication enabled (HS256)") |
| 53 | } |
| 54 | |
| 55 | // Start background cleanup to prevent memory leak from auth failure limiters |
| 56 | ai.startFailureLimiterCleanup() |
| 57 | |
| 58 | return ai |
| 59 | } |
| 60 | |
| 61 | // Unary returns a grpc.UnaryServerInterceptor that validates credentials and |
| 62 | // injects UserInfo into the context for downstream access control. |