cmd

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2017 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TurntCmd = &cobra.Command{
	Use:   "turnt [url]",
	Short: "A helper app to make requests against Turnstile",
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 1 {
			return errors.New("URL is a required argument")
		}

		u, err := url.Parse(args[0])

		if err != nil {
			return err
		}

		requestUrl = u

		return nil
	},
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {

		if conf.identity == "" {
			return errors.New("Identity is required")
		}

		if conf.secret == "" {
			return errors.New("Secret is required")
		}

		if !utils.AlgorithmIsSupported(conf.digest) {
			errStr := fmt.Sprintf("Turnstile currently supports the following encryption algorithms: %s. You specified %s.",
				utils.GetSupportedAlgorithms(),
				conf.digest)
			return errors.New(errStr)
		}

		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		date := time.Now().Unix()
		conf.headers["date"] = strconv.Itoa(int(date))

		uri := requestUrl.Path
		host := requestUrl.Host

		algorithm := utils.GetAlgorithmType(conf.digest)
		digest := lib.GenerateDigest(algorithm, conf.payload)
		conf.headers["digest"] = digest

		method := strings.ToUpper(conf.method)

		log.WithFields(log.Fields{"method": method}).Info("Using method")
		log.WithFields(log.Fields{"uri": uri}).Info("Using URI")
		log.WithFields(log.Fields{"host": host}).Info("Using host")
		log.WithFields(log.Fields{"date": date}).Info("Using date")
		log.WithFields(log.Fields{"identity": conf.identity}).Info("Using identity")
		log.WithFields(log.Fields{"digest": digest}).Info("Using digest")

		signature := lib.GenerateSignature(algorithm, conf.identity, conf.secret, digest, method, uri, host, date)
		log.WithFields(log.Fields{"signature": signature}).Info("Using signature")

		authorization := lib.GenerateAuthorization(algorithm, conf.identity, signature)
		log.WithFields(log.Fields{"authorization": authorization}).Info("Using authorization")
		conf.headers["authorization"] = authorization

		err, out := lib.GenerateRequest(method, requestUrl.String(), conf.payload, conf.headers)
		if err != nil {
			return err
		}

		log.Infof("Response: \n%s", out.String())

		return nil
	},
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute()

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL