github.com/decred/politeia@v1.4.0/politeiawww/cmd/cmswww/register.go (about)

     1  // Copyright (c) 2017-2019 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"strings"
    11  
    12  	"github.com/decred/politeia/politeiawww/api/cms/v1"
    13  	www "github.com/decred/politeia/politeiawww/api/www/v1"
    14  	"github.com/decred/politeia/politeiawww/cmd/shared"
    15  )
    16  
    17  // RegisterUserCmd allows invited contractors to complete the registration
    18  // process and will allow them to login and submit invoices to receive payment.
    19  type RegisterUserCmd struct {
    20  	Args struct {
    21  		Email    string `positional-arg-name:"email"`
    22  		Username string `positional-arg-name:"username"`
    23  		Password string `positional-arg-name:"password"`
    24  		Token    string `positional-arg-name:"token"`
    25  	} `positional-args:"true" required:"true"`
    26  }
    27  
    28  // Execute executes the register user command
    29  func (cmd *RegisterUserCmd) Execute(args []string) error {
    30  	// Fetch  policy for password requirements
    31  	pr, err := client.Policy()
    32  	if err != nil {
    33  		return fmt.Errorf("Policy: %v", err)
    34  	}
    35  
    36  	// Validate password
    37  	if uint(len(cmd.Args.Password)) < pr.MinPasswordLength {
    38  		return fmt.Errorf("password must be %v characters long",
    39  			pr.MinPasswordLength)
    40  	}
    41  
    42  	// Create user identity and save it to disk
    43  	id, err := shared.NewIdentity()
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	ru := &v1.RegisterUser{
    49  		Email:             cmd.Args.Email,
    50  		Username:          strings.TrimSpace(cmd.Args.Username),
    51  		Password:          shared.DigestSHA3(cmd.Args.Password),
    52  		VerificationToken: strings.TrimSpace(cmd.Args.Token),
    53  		PublicKey:         hex.EncodeToString(id.Public.Key[:]),
    54  	}
    55  
    56  	// Print request details
    57  	err = shared.PrintJSON(ru)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	// Send request
    63  	rur, err := client.RegisterUser(ru)
    64  	if err != nil {
    65  		return fmt.Errorf("Register: %v", err)
    66  	}
    67  
    68  	err = cfg.SaveIdentity(ru.Username, id)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	// Print response details
    74  	err = shared.PrintJSON(rur)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	// Login to cms
    80  	l := &www.Login{
    81  		Email:    cmd.Args.Email,
    82  		Password: shared.DigestSHA3(cmd.Args.Password),
    83  	}
    84  
    85  	_, err = client.Login(l)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	// Update the logged in username that we store
    91  	// on disk to know what identity to load.
    92  	return cfg.SaveLoggedInUsername(ru.Username)
    93  }