示例#1
0
func NewAccountEmail(uid string) {
	token, err := createToken(uid)
	if err != nil {
		logrus.Fatal(err.Error())
	}

	app, err := NewApplication()
	if err != nil {
		logrus.Fatal(err.Error())
	}

	vars := map[string]interface{}{
		"link": fmt.Sprintf("%s/auth/setup/%s", viper.GetString("email_link_base"), model.SignToken(ACCOUNT_SETUP_SALT, token.Token))}

	err = app.SendEmail(token.Email, fmt.Sprintf("[%s] New Account Setup", viper.GetString("email_prefix")), "setup-account.txt", vars)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"uid":   uid,
			"error": err,
		}).Error("failed send email to user")
	}
}
示例#2
0
func ResetPasswordEmail(uid string) {
	db, err := NewDb()
	if err != nil {
		logrus.Fatal(err.Error())
	}

	_, err = model.FetchAnswer(db, uid)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"uid":   uid,
			"error": err,
		}).Error("Failed to fetch security answer. Please run newacct to setup user account.")
		return
	}

	token, err := createToken(uid)
	if err != nil {
		logrus.Fatal(err.Error())
	}

	app, err := NewApplication()
	if err != nil {
		logrus.Fatal(err.Error())
	}

	vars := map[string]interface{}{
		"link": fmt.Sprintf("%s/auth/resetpw/%s", viper.GetString("email_link_base"), model.SignToken(RESET_SALT, token.Token))}

	err = app.SendEmail(token.Email, fmt.Sprintf("[%s] Please reset your password", viper.GetString("email_prefix")), "reset-password.txt", vars)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"uid":   uid,
			"error": err,
		}).Error("failed send email to user")
	}
}
示例#3
0
func forgotPassword(app *Application, r *http.Request) error {
	uid := r.FormValue("uid")
	if len(uid) == 0 {
		return errors.New("Please provide a user name.")
	}

	_, err := model.FetchTokenByUser(app.db, uid, viper.GetInt("setup_max_age"))
	if err == nil {
		logrus.WithFields(logrus.Fields{
			"uid": uid,
		}).Error("Forgotpw: user already has active token")
		return nil
	}

	client := NewIpaClient(true)
	userRec, err := client.UserShow(uid)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"uid":   uid,
			"error": err,
		}).Error("Forgotpw: invalid uid")
		return nil
	}

	if len(userRec.Email) == 0 {
		logrus.WithFields(logrus.Fields{
			"uid": uid,
		}).Error("Forgotpw: missing email address")
		return nil
	}

	_, err = model.FetchAnswer(app.db, uid)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"uid":   uid,
			"error": err,
		}).Error("Forgotpw: Failed to fetch security answer")
		return nil
	}

	token, err := model.NewToken(app.db, uid, string(userRec.Email))
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"uid":   uid,
			"error": err,
		}).Error("Forgotpw: Failed to create token")
		return nil
	}

	vars := map[string]interface{}{
		"link": fmt.Sprintf("%s/auth/resetpw/%s", viper.GetString("email_link_base"), model.SignToken(RESET_SALT, token.Token))}

	err = app.SendEmail(token.Email, fmt.Sprintf("[%s] Please reset your password", viper.GetString("email_prefix")), "reset-password.txt", vars)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"uid":   uid,
			"error": err,
		}).Error("Forgotpw: failed send email to user")
	}

	return nil
}