taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

command_validator.go (2791B)


      1 // This file is part of tdir, the Taler Directory implementation.
      2 // Copyright (C) 2025 Martin Schanzenbach
      3 //
      4 // Taldir is free software: you can redistribute it and/or modify it
      5 // under the terms of the GNU Affero General Public License as published
      6 // by the Free Software Foundation, either version 3 of the License,
      7 // or (at your option) any later version.
      8 //
      9 // Taldir is distributed in the hope that it will be useful, but
     10 // WITHOUT ANY WARRANTY; without even the implied warranty of
     11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12 // Affero General Public License for more details.
     13 //
     14 // You should have received a copy of the GNU Affero General Public License
     15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 //
     17 // SPDX-License-Identifier: AGPL3.0-or-later
     18 
     19 package taldir
     20 
     21 import (
     22 	"fmt"
     23 	"html/template"
     24 	"os/exec"
     25 	"regexp"
     26 )
     27 
     28 type CommandValidator struct {
     29 
     30 	// Name
     31 	name string
     32 
     33 	// Config
     34 	config *TaldirConfig
     35 
     36 	// Validator alias regex
     37 	validAliasRegex string
     38 
     39 	// The command to call for validation
     40 	command string
     41 
     42 	// registration/lookup page
     43 	landingPageTpl *template.Template
     44 }
     45 
     46 func (t CommandValidator) LandingPageTpl() *template.Template {
     47 	return t.landingPageTpl
     48 }
     49 
     50 func (t CommandValidator) Type() ValidatorType {
     51 	return ValidatorTypeCommand
     52 }
     53 
     54 func (t CommandValidator) Name() string {
     55 	return t.name
     56 }
     57 
     58 func (t CommandValidator) DisplayName() string {
     59 	return t.config.Ini.GetString("directory-validator-"+t.name, "display_name", t.name)
     60 }
     61 
     62 func (t CommandValidator) ChallengeFee() string {
     63 	return t.config.Ini.GetString("directory-validator-"+t.name, "challenge_fee", "KUDOS:0")
     64 }
     65 
     66 func (t CommandValidator) IsAliasValid(alias string) (err error) {
     67 	if t.validAliasRegex != "" {
     68 		matched, _ := regexp.MatchString(t.validAliasRegex, alias)
     69 		if !matched {
     70 			return fmt.Errorf("alias `%s' invalid", alias) // TODO i18n
     71 		}
     72 	}
     73 	return
     74 }
     75 
     76 func (t CommandValidator) RegistrationStart(topic string, link string, message string, alias string, challenge string) (string, error) {
     77 	path, err := exec.LookPath(t.command)
     78 	if err != nil {
     79 		return "", err
     80 	}
     81 	out, err := exec.Command(path, alias, challenge, topic, message).Output()
     82 	// FIXME logger
     83 	fmt.Printf("Executing `%s %s %s %s %s`\n", path, alias, challenge, topic, message)
     84 	if err != nil {
     85 		fmt.Printf("%s, %v\n", out, err)
     86 		return "", err
     87 	}
     88 	return "", nil
     89 }
     90 
     91 func makeCommandValidator(cfg *TaldirConfig, name string, landingPageTpl *template.Template) CommandValidator {
     92 	sec := "directory-validator-" + name
     93 	return CommandValidator{
     94 		name:            name,
     95 		config:          cfg,
     96 		landingPageTpl:  landingPageTpl,
     97 		validAliasRegex: cfg.Ini.GetString(sec, "valid_alias_regex", ""),
     98 		command:         cfg.Ini.GetString(sec, "command", ""),
     99 	}
    100 }