Auth issues Fix

This commit is contained in:
Enki 2025-05-11 21:07:07 -07:00
parent b470e0041d
commit be9cd52ac6
3 changed files with 20 additions and 9 deletions

View File

@ -162,6 +162,7 @@ func main() {
logger, logger,
keyPassword, // Use the same password for simplicity keyPassword, // Use the same password for simplicity
24*time.Hour, // Token duration 24*time.Hour, // Token duration
cfg.AllowedNpub, // Pass the allowed npub from config
) )
// Initialize bot service // Initialize bot service

View File

@ -41,10 +41,11 @@ type Service struct {
logger *zap.Logger logger *zap.Logger
secretKey []byte secretKey []byte
tokenDuration time.Duration tokenDuration time.Duration
allowedNpub string // Add allowed npub for whitelisting
} }
// NewService creates a new authentication service // NewService creates a new authentication service
func NewService(db *db.DB, logger *zap.Logger, secretKey string, tokenDuration time.Duration) *Service { func NewService(db *db.DB, logger *zap.Logger, secretKey string, tokenDuration time.Duration, allowedNpub string) *Service {
// If no secret key is provided, generate a secure random one // If no secret key is provided, generate a secure random one
decodedKey := []byte(secretKey) decodedKey := []byte(secretKey)
if secretKey == "" { if secretKey == "" {
@ -61,11 +62,20 @@ func NewService(db *db.DB, logger *zap.Logger, secretKey string, tokenDuration t
logger: logger, logger: logger,
secretKey: decodedKey, secretKey: decodedKey,
tokenDuration: tokenDuration, tokenDuration: tokenDuration,
allowedNpub: allowedNpub,
} }
} }
// Login handles user login with a Nostr signature // Login handles user login with a Nostr signature
func (s *Service) Login(pubkey, signature, eventJSON string) (string, error) { func (s *Service) Login(pubkey, signature, eventJSON string) (string, error) {
// Check if there's a whitelist and enforce it
if s.allowedNpub != "" && s.allowedNpub != pubkey {
s.logger.Warn("Login attempt from non-whitelisted pubkey",
zap.String("attempt_pubkey", pubkey),
zap.String("allowed_npub", s.allowedNpub))
return "", errors.New("unauthorized: only the configured npub is allowed to log in")
}
// Parse the event // Parse the event
var event nostr.Event var event nostr.Event
if err := json.Unmarshal([]byte(eventJSON), &event); err != nil { if err := json.Unmarshal([]byte(eventJSON), &event); err != nil {

View File

@ -52,7 +52,7 @@ type Config struct {
Write bool `mapstructure:"write"` Write bool `mapstructure:"write"`
} `mapstructure:"relays"` } `mapstructure:"relays"`
AllowedNpub string `mapstructure:"allowed_npub"` // NEW AllowedNpub string `mapstructure:"allowed_npub"` // Set this to restrict login to a specific Nostr pubkey
} }
// LoadConfig loads the configuration from file or environment variables // LoadConfig loads the configuration from file or environment variables
@ -79,7 +79,7 @@ func LoadConfig(configPath string) (*Config, error) {
{"url": "wss://freelay.sovbit.host", "read": true, "write": true}, {"url": "wss://freelay.sovbit.host", "read": true, "write": true},
}) })
v.SetDefault("allowed_npub", "") v.SetDefault("allowed_npub", "") // Empty string means no restriction
// Setup config file search // Setup config file search
if configPath != "" { if configPath != "" {