main.go 2.72 KB
Newer Older
Loïck Bonniot's avatar
Loïck Bonniot committed
1 2 3 4 5 6
package main

import (
	"dfss"
	"flag"
	"fmt"
7
	osuser "os/user"
Loïck Bonniot's avatar
Loïck Bonniot committed
8
	"runtime"
9 10 11
	"time"

	"dfss/dfssc/user"
Loïck Bonniot's avatar
Loïck Bonniot committed
12 13 14
)

var (
15 16 17 18 19
	verbose  bool
	fca      string // Path to the CA
	fcert    string // Path to the certificate
	fkey     string // Path to the private key
	addrPort string // Address and port of the platform
Loïck Bonniot's avatar
Loïck Bonniot committed
20 21 22 23 24
)

func init() {

	flag.BoolVar(&verbose, "v", false, "Print verbose messages")
25 26 27 28
	flag.StringVar(&fca, "ca", "ca.pem", "Path to the root certificate")
	flag.StringVar(&fcert, "cert", "cert.pem", "Path to the user certificate")
	flag.StringVar(&fkey, "key", "priv_key.pem", "Path to the private key")
	flag.StringVar(&addrPort, "host", "127.0.0.1:9000", "Host of the DFSS platform (e.g. 127.0.0.1:9000)")
Loïck Bonniot's avatar
Loïck Bonniot committed
29 30 31 32 33 34

	flag.Usage = func() {
		fmt.Println("DFSS client command line v" + dfss.Version)
		fmt.Println("A tool to sign multiparty contracts")

		fmt.Println("\nUsage:")
35
		fmt.Println("  dfssc [flags] command")
Loïck Bonniot's avatar
Loïck Bonniot committed
36 37

		fmt.Println("\nThe commands are:")
38 39 40
		fmt.Println("  help      print this help")
		fmt.Println("  version   print dfss client version")
		fmt.Println("  register  register a new client")
Caro Axel's avatar
Caro Axel committed
41
		fmt.Println("  auth      authenticate a new client")
Loïck Bonniot's avatar
Loïck Bonniot committed
42
		fmt.Println("  show <c>  print contract information from file c")
Loïck Bonniot's avatar
Loïck Bonniot committed
43 44 45 46 47 48 49 50 51 52

		fmt.Println("\nFlags:")
		flag.PrintDefaults()

		fmt.Println()
	}
}

func main() {
	flag.Parse()
53

Loïck Bonniot's avatar
Loïck Bonniot committed
54 55 56 57 58
	command := flag.Arg(0)

	switch command {
	case "version":
		fmt.Println("v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	case "register":
		fmt.Println("Registering a new user")
		// Initialize variables
		var country, mail, organization, unit, passphrase string
		var bits int

		u, err := osuser.Current()
		if err != nil {
			fmt.Println("An error occurred : ", err.Error())
			break
		}

		// Get all the necessary parameters
		readStringParam("Mail", "", &mail)
		readStringParam("Country", time.Now().Location().String(), &country)
		readStringParam("Organization", u.Name, &organization)
		readStringParam("Organizational unit", u.Name, &unit)
		readIntParam("Length of the key (2048 or 4096)", 2048, &bits)
		err = readPassword(&passphrase)
		if err != nil {
			fmt.Println("An error occurred : ", err.Error())
			break
		}

		recapUser(fca, fcert, fkey, addrPort, country, organization, unit, mail, bits)

		err = user.Register(fca, fcert, fkey, addrPort, passphrase, country, organization, unit, mail, bits)
		if err != nil {
			fmt.Println("An error occurred : ", err.Error())
		}

Caro Axel's avatar
Caro Axel committed
90 91 92 93 94 95 96 97 98 99 100 101
	case "authenticate":
		fmt.Println("Authenticating user")
		var mail, token string

		readStringParam("Mail", "", &mail)
		readStringParam("Token", "", &token)

		err := user.Authenticate(fca, fcert, addrPort, mail, token)
		if err != nil {
			fmt.Println("An error occurred : ", err.Error())
		}

Loïck Bonniot's avatar
Loïck Bonniot committed
102 103
	case "show":
		showContract(flag.Arg(1))
Loïck Bonniot's avatar
Loïck Bonniot committed
104 105 106 107
	default:
		flag.Usage()
	}
}