Exemplo n.º 1
0
// ForgotPublicKey removes corresponding public key fingerprint hash from `key` table
func (h *Handler) ForgotPublicKey(connection ssh.Channel, term *terminal.Terminal, input []string) error {
	fingerprint := h.KeyFingerPrint
	if fingerprint == nil {
		return fmt.Errorf("Public key is empty!")
	}
	k := string(ssh.MarshalAuthorizedKey(fingerprint))
	key := models.Key{
		UserID:  h.CurrentUser.ID,
		Content: models.Hash(k),
	}
	err := models.DB.Table("key").Where("content=?", models.Hash(k)).First(&key).Error
	if err == nil {
		err = models.DB.Table("key").Delete(&key).Error
		if err != nil {
			return err
		}
		h.writeToUser("Public key is removed! You'll need password in future to be able to authorize from this client.")
		return nil
	}
	if err == gorm.ErrRecordNotFound {
		return nil
	}
	return err
}
Exemplo n.º 2
0
// ImportPublicKey saves hash of public key from Handler.KeyFingerPrint
// into `key` table
func (h *Handler) ImportPublicKey(input []string) error {
	fingerprint := h.KeyFingerPrint
	if fingerprint == nil {
		return fmt.Errorf("Public key is empty!")
	}
	_, err := h.writeToUser("Importing public key...")
	if err != nil {
		return err
	}
	k := string(ssh.MarshalAuthorizedKey(fingerprint))
	key := models.Key{
		UserID:  h.CurrentUser.ID,
		Content: models.Hash(k),
	}
	err = models.DB.Table("key").Create(&key).Error
	if err != nil {
		_, err = h.writeToUser("Error importing key, probably it is already imported!")
		return err
	}
	_, err = h.writeToUser("Key imported succesefully!")
	return err
}