taldir

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

command_validator.go (2671B)


      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) ChallengeFee() string {
     59 	return t.config.Ini.Section("taldir-validator-" + t.name).Key("challenge_fee").MustString("KUDOS:0")
     60 }
     61 
     62 func (t CommandValidator) IsAliasValid(alias string) (err error) {
     63 	if t.validAliasRegex != "" {
     64 		matched, _ := regexp.MatchString(t.validAliasRegex, alias)
     65 		if !matched {
     66 			return fmt.Errorf("alias `%s' invalid", alias) // TODO i18n
     67 		}
     68 	}
     69 	return
     70 }
     71 
     72 func (t CommandValidator) RegistrationStart(topic string, link string, message string, alias string, challenge string) (string, error) {
     73 	path, err := exec.LookPath(t.command)
     74 	if err != nil {
     75 		return "", err
     76 	}
     77 	out, err := exec.Command(path, alias, challenge, topic, message).Output()
     78 	// FIXME logger
     79 	fmt.Printf("Executing `%s %s %s %s %s`\n", path, alias, challenge, topic, message)
     80 	if err != nil {
     81 		fmt.Printf("%s, %v\n", out, err)
     82 		return "", err
     83 	}
     84 	return "", nil
     85 }
     86 
     87 func makeCommandValidator(cfg *TaldirConfig, name string, landingPageTpl *template.Template) CommandValidator {
     88 	sec := cfg.Ini.Section("taldir-validator-" + name)
     89 	return CommandValidator{
     90 		name:            name,
     91 		config:          cfg,
     92 		landingPageTpl:  landingPageTpl,
     93 		validAliasRegex: sec.Key("valid_alias_regex").MustString(""),
     94 		command:         sec.Key("command").MustString(""),
     95 	}
     96 }