Пример #1
0
// AskReset generate a new saltTokenVerify / hashedTokenVerify
// return tokenVerify (to be sent to user by mail)
func (user *User) AskReset() (string, error) {

	err := user.FindByUsernameAndEmail(user.Username, user.Email)
	if err != nil {
		return "", err
	}

	tokenVerify, hashedTokenVerify, err := utils.GeneratePassword()
	if err != nil {
		log.Errorf("Error while generate Token for reset password %s", err)
		return tokenVerify, err
	}

	err = Store().clUsers.Update(
		bson.M{"_id": user.ID},
		bson.M{"$set": bson.M{
			"auth.hashedTokenVerify": hashedTokenVerify,
			"auth.dateAskReset":      time.Now().Unix(),
		}})

	if err != nil {
		log.Errorf("Error while ask reset user %s", err)
	}
	return tokenVerify, err
}
Пример #2
0
// Insert a new user, return tokenVerify to user, in order to
// validate account after check email
func (user *User) Insert() (string, error) {
	user.ID = bson.NewObjectId().Hex()

	user.DateCreation = time.Now().Unix()
	user.Auth.DateAskReset = time.Now().Unix()
	user.Auth.EmailVerified = false
	user.IsSystem = false
	user.IsArchived = false
	user.CanWriteNotifications = false
	nbUsers, err := CountUsers()
	if err != nil {
		log.Errorf("Error while count all users%s", err)
		return "", err
	}
	if nbUsers > 0 {
		user.IsAdmin = false
	} else {
		log.Infof("user %s is the first user, he is now admin", user.Username)
		user.IsAdmin = true
	}
	tokenVerify := ""
	tokenVerify, user.Auth.HashedTokenVerify, err = utils.GeneratePassword()
	if err != nil {
		log.Errorf("Error while generate Token Verify for new user %s", err)
		return tokenVerify, err
	}

	err = Store().clUsers.Insert(user)
	if err != nil {
		log.Errorf("Error while inserting new user %s", err)
	}
	return tokenVerify, err
}
Пример #3
0
func (user *User) regenerateAndStoreAuth() (string, error) {
	password, hashedPassword, err := utils.GeneratePassword()
	if err != nil {
		log.Errorf("Error while genereate password for user %s", err)
		return password, err
	}
	err = Store().clUsers.Update(
		bson.M{"_id": user.ID},
		bson.M{"$set": bson.M{
			"auth.hashedTokenVerify": "", // reset tokenVerify
			"auth.hashedPassword":    hashedPassword,
			"auth.dateVerify":        time.Now().Unix(),
			"auth.dateRenewPassword": time.Now().Unix(),
			"auth.emailVerify":       true,
		}})

	if err != nil {
		log.Errorf("Error while updating user %s", err)
	}

	return password, err
}