Files
telegram-gateway/config.go
Gabriel Radureau 07115e3162
Some checks failed
Docker Build / build-and-push-image (push) Failing after 18s
Phase 1.5 — auth layer (Redis sessions, allowlist, requireAuth)
Adds an authentication layer in front of the bot handlers :

- Auth handler on the principal bot (@arcodange_factory_bot, slug
  factory) parses /start, /auth <code>, /whoami, /logout. On a
  successful /auth, the message containing the code is best-effort
  deleted from the user's chat (replay defense).
- Redis-backed sessions (key tg-gw:auth:<from.id>, TTL 24h, configurable
  via AUTH_SESSION_TTL). Constant-time secret compare via crypto/subtle.
- ALLOWED_USERS env (CSV of Telegram user IDs) — silent-drops anyone
  not in the list before the auth gate runs.
- New per-bot field 'requireAuth' (pointer-bool). Default = true (secure
  by default). Auto-forced to false for handler=auth (chicken-and-egg).
- Server gates: allowlist first, then requireAuth before handler dispatch.
- Fail-at-startup if a bot is configured with handler=auth or
  requireAuth: true while AUTH_SECRET is unset.

Design: factory/docs/adr/20260509-telegram-gateway-auth.md (in factory PR).
User docs: AUTH.md (new), HOWTO_ADD_BOT.md (Cas 2 updated for default
true and gated flow).

New deps: github.com/redis/go-redis/v9.

Refs ~/.claude/plans/pour-les-notifications-on-inherited-seal.md § Phase 1.5.
2026-05-09 13:56:30 +02:00

49 lines
1.4 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
)
type Config struct {
Bots map[string]BotConfig `yaml:"bots"`
}
type BotConfig struct {
Handler string `yaml:"handler"`
// RequireAuth is *bool so "absent" is distinct from "false". When unset
// (nil) the registry treats it as true — secure by default. Explicit
// `requireAuth: false` is required to expose a bot publicly.
// Forced to false for handler=auth (chicken-and-egg).
RequireAuth *bool `yaml:"requireAuth,omitempty"`
Token string `yaml:"-"`
Secret string `yaml:"-"`
}
// LoadConfig reads the YAML routing config and merges per-bot secrets pulled
// from the process environment. Per-bot env keys are derived from the bot
// slug uppercased: BOT_<UPPER_SLUG>_TOKEN, BOT_<UPPER_SLUG>_SECRET.
func LoadConfig(path string) (*Config, error) {
raw, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read config %s: %w", path, err)
}
var cfg Config
if err := yaml.Unmarshal(raw, &cfg); err != nil {
return nil, fmt.Errorf("parse yaml: %w", err)
}
if len(cfg.Bots) == 0 {
return nil, fmt.Errorf("no bots in %s", path)
}
for slug, b := range cfg.Bots {
envSlug := strings.ToUpper(strings.ReplaceAll(slug, "-", "_"))
b.Token = os.Getenv("BOT_" + envSlug + "_TOKEN")
b.Secret = os.Getenv("BOT_" + envSlug + "_SECRET")
cfg.Bots[slug] = b
}
return &cfg, nil
}