示例#1
0
文件: key.go 项目: epitron/ssh-chat
// ReadPrivateKey attempts to read your private key and possibly decrypt it if it
// requires a passphrase.
// This function will prompt for a passphrase on STDIN if the environment variable (`IDENTITY_PASSPHRASE`),
// is not set.
func ReadPrivateKey(path string) ([]byte, error) {
	privateKey, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("failed to load identity: %v", err)
	}

	block, rest := pem.Decode(privateKey)
	if len(rest) > 0 {
		return nil, fmt.Errorf("extra data when decoding private key")
	}
	if !x509.IsEncryptedPEMBlock(block) {
		return privateKey, nil
	}

	passphrase := os.Getenv("IDENTITY_PASSPHRASE")
	if passphrase == "" {
		passphrase, err = gopass.GetPass("Enter passphrase: ")
		if err != nil {
			return nil, fmt.Errorf("couldn't read passphrase: %v", err)
		}
	}
	der, err := x509.DecryptPEMBlock(block, []byte(passphrase))
	if err != nil {
		return nil, fmt.Errorf("decrypt failed: %v", err)
	}

	privateKey = pem.EncodeToMemory(&pem.Block{
		Type:  block.Type,
		Bytes: der,
	})

	return privateKey, nil
}
示例#2
0
// addUser adds a new user based on the provided stdin. The provided password will be hashed using bcrypt.
// The following details are required: username, password, email, first name, last name.
func addUser() bool {
	var user models.User

	fmt.Println("Adding new user to ScanBadge API...")

	fmt.Println("Username:"******"" {
		fmt.Println("Username is required")
		return false
	}

	password, err := gopass.GetPass("Password:\n")
	if err != nil {
		fmt.Println(err)
		return false
	}

	if password == "" || len(password) < 8 || len(password) > 512 {
		fmt.Println("Password must be at least 8 characters long and cannot exceed 512 characters")
		return false
	}

	user.Password = utility.HashPassword(password)

	fmt.Println("Email:")
	fmt.Scanln(&user.Email)
	if user.Email == "" {
		fmt.Println("Email is required")
		return false
	}

	fmt.Println("First name:")
	fmt.Scanln(&user.FirstName)
	if user.FirstName == "" {
		fmt.Println("First name is required")
		return false
	}

	fmt.Println("Last name:")
	fmt.Scanln(&user.LastName)
	if user.LastName == "" {
		fmt.Println("Last name is required")
		return false
	}

	// Everything seems to be all right, attempt to insert new user to database.
	err = configuration.Dbmap.Insert(&user)

	if err == nil {
		fmt.Println(fmt.Sprintf("Successfully added new user '%s'", user.Username))
	} else {
		fmt.Println("Cannot add new user due to " + err.Error())
	}

	return err == nil
}
示例#3
0
func addUser(c *cli.Context) {
	if _, err := os.Stat("db/registry.db"); os.IsNotExist(err) {
		log.Fatal("cant't find registry.db")
	}

	user := c.Args().First()

	// Password
	pwd, err := gopass.GetPass("Password:"******"Confirm Password:"******"Passwords don't match")
	}
	db, err := bolt.Open("registry.db", 0600, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	password := SetPassword(pwd)

	err = db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte("auth"))
		if err != nil {
			return err
		}
		err = b.Put([]byte(user), password)
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Successful")
}