Exemple #1
0
// Run is the main function
func Run(cmd *cobra.Command, args []string) {
	database.InitDB()
	address := viper.GetString("address")

	// Throttling control
	store, err := memstore.New(65536)
	if err != nil {
		logrus.Fatal(err)
	}

	quota := throttled.RateQuota{throttled.PerMin(20), 5}
	rateLimiter, err := throttled.NewGCRARateLimiter(store, quota)
	if err != nil {
		logrus.Fatal(err)
	}

	httpRateLimiter := throttled.HTTPRateLimiter{
		RateLimiter: rateLimiter,
		VaryBy:      &throttled.VaryBy{Path: true},
	}

	mux := http.NewServeMux()
	mux.HandleFunc("/authenticate/free", handleFreeCookie)
	mux.HandleFunc("/authenticate/verify", handleAuthenticate)
	mux.Handle("/", http.StripPrefix("/authenticate/", http.FileServer(http.Dir("./static"))))

	database.InitDB()

	if buildst == "none" {
		buildst = "[This Dev version is not compiled using regular procedure]"
	}

	logrus.Info("Starting App, ", Version)
	logrus.Infof("2FA HTTP layer listening on %s", address)
	logrus.Infof("Domain for cookies is %s", viper.GetString("domain"))
	logrus.Infof("Cookie max age is %d hour(s)", viper.GetInt("cookiemaxage"))
	logrus.Info("Starting instance on ", time.Now())

	if err := http.ListenAndServe(address, httpRateLimiter.RateLimit(mux)); err != nil {
		logrus.Fatal("Unable to create HTTP layer", err)
	}
}
Exemple #2
0
// CreateUser is a procedure for creating a user
func CreateUser(cmd *cobra.Command, args []string) {
	database.InitDB()
	db := database.GetDB()

	username := viper.GetString("name")
	if username == "" {
		fmt.Println("Required 'name' parameter not specified")
		return
	}

	fmt.Printf("Creating User %s...\n", username)

	for _, item := range db.Users {
		if item.Username == username {
			logrus.Errorf("User %s already exists in the database", username)
			return
		}
	}

	// Generate TOTP
	init2FA, err := gototp.New(gototp.RandomSecret(10))
	if err != nil {
		logrus.Error(err)
		return
	}

	// check if password was specified, otherwise, go interactive
	password := viper.GetString("password")
	if password == "" {
		password = prompter.Password("Enter password to use")
	}

	user, err := pwMan.NewUser(username, password, init2FA.Secret())
	if err != nil {
		fmt.Printf("Error while creating user %s: %v\n", username, err)
	}

	db.AddUser(*user)

	fmt.Printf("User %s created. Caracteristics :\n", username)
	fmt.Printf("2FA init: %s || QRCode link: %s\n", init2FA.Secret(), init2FA.QRCodeGoogleChartsUrl("Code", 320))
	//fmt.Println(.QRCodeTerminal("label"))

}