taldir

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

main.go (2863B)


      1 // This file is part of taldir, the Taler Directory implementation.
      2 // Copyright (C) 2022 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 main
     20 
     21 import (
     22 	"crypto/sha512"
     23 	"encoding/binary"
     24 	"flag"
     25 	"fmt"
     26 	"log"
     27 	"net/url"
     28 	"os"
     29 
     30 	"gopkg.in/ini.v1"
     31 	"taler.net/taldir/internal/util"
     32 )
     33 
     34 // Hashes the alias with its type in a prefix-free fashion
     35 // SHA512(len(atype||alias)||atype||alias)
     36 func HashAlias(atype string, alias string) []byte {
     37 	h := sha512.New()
     38 	b := make([]byte, 4)
     39 	binary.BigEndian.PutUint32(b, uint32(len(atype)+len(alias)))
     40 	h.Write(b)
     41 	h.Write([]byte(atype))
     42 	h.Write([]byte(alias))
     43 	return h.Sum(nil)
     44 }
     45 
     46 // Generates a link from a challenge and alias
     47 func generateLink(host string, alias string, atype string, challenge string) string {
     48 	h_alias := HashAlias(atype, alias)
     49 	h_alias_s := util.Base32CrockfordEncode(h_alias)
     50 	return host + "/register/" + url.QueryEscape(h_alias_s) + "/" + url.QueryEscape(challenge) + "?alias=" + url.QueryEscape(alias)
     51 }
     52 
     53 func main() {
     54 	var solveFlag = flag.Bool("s", false, "Provide a solution for the challenge/pubkey")
     55 	var linkFlag = flag.Bool("l", false, "Provide a link for activation")
     56 	var challengeFlag = flag.String("c", "", "Activation challenge")
     57 	var pubkeyFlag = flag.String("p", "", "Public key")
     58 	var aliasFlag = flag.String("a", "", "Alias")
     59 	var atypeFlag = flag.String("t", "", "Alias type")
     60 	flag.Parse()
     61 	cfgfile := "taldir.conf"
     62 	_cfg, err := ini.LooseLoad(cfgfile)
     63 	if err != nil {
     64 		log.Fatalf("Failed to read config: %v", err)
     65 		os.Exit(1)
     66 	}
     67 	host := _cfg.Section("taldir").Key("base_url").MustString("http://localhost")
     68 	if *solveFlag {
     69 		if len(*challengeFlag) == 0 || len(*pubkeyFlag) == 0 {
     70 			fmt.Println("You need to provide an activation challenge and a public key to generate a solution")
     71 			os.Exit(1)
     72 		}
     73 		fmt.Println(util.GenerateSolution(*pubkeyFlag, *challengeFlag))
     74 		os.Exit(0)
     75 	}
     76 	if *linkFlag {
     77 		if len(*challengeFlag) == 0 || len(*aliasFlag) == 0 || len(*atypeFlag) == 0 {
     78 			fmt.Println("You need to provide an activation challenge and an alias to generate a link")
     79 			os.Exit(1)
     80 		}
     81 		fmt.Println(generateLink(host, *aliasFlag, *atypeFlag, *challengeFlag))
     82 		os.Exit(0)
     83 	}
     84 }