Beispiel #1
0
func ResetPassword(db *sql.DB, email string) error {
	if email == "" {
		return util.NewError(nil, "Correo electrónico requerido", 400)
	}

	username, err := unusedEmail(db, email)
	if err != nil {
		return err
	}

	if username == "" {
		return util.NewError(nil, "Correo electrónico no registrado", 400)
	}

	alphaNum := []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv")
	randValue := ""
	for i := 0; i < 32; i++ {
		num, err := util.RandKey(58)
		if err != nil {
			return err
		}
		randValue = randValue + string(alphaNum[num])
	}
	hashed := sha256.New()
	hashed.Write([]byte(randValue))
	hashedStr := hex.EncodeToString(hashed.Sum(nil))

	err = storePasswordToken(db, email, hashedStr)
	if err != nil {
		return err
	}

	subject := "5sur - Restablecer contraseña"
	text := "<b>" + username + "</b> - Para cambiar tu contraseña haz click en el siguiente link."
	link := "https://5sur.com/passwordChange?t=" + randValue + "&u=" + username
	body := util.EmailTemplate(text, "Cambiar contraseña", link)
	err = util.SendEmail(email, subject, body)

	return nil
}
Beispiel #2
0
func UserAuth(db *sql.DB, username string, password string, email string) error {
	// Create auth token
	alphaNum := []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv")
	randValue := ""
	for i := 0; i < 32; i++ {
		num, err := util.RandKey(58)
		if err != nil {
			return err
		}
		randValue = randValue + string(alphaNum[num])
	}
	hashed := sha256.New()
	hashed.Write([]byte(randValue))
	hashedStr := hex.EncodeToString(hashed.Sum(nil))

	err := deleteUserAuth(db, email)
	if err != nil {
		return err
	}

	err = createUserAuth(db, username, password, email, hashedStr)
	if err != nil {
		return err
	}

	subject := "Verificación de correo electrónico 5sur"
	text := "Bienvenido a 5sur.com! haz click en el siguiente link para registrarte."
	link := "https://5sur.com/auth/?t=" + randValue
	body := util.EmailTemplate(text, "Regístrate", link)
	err = util.SendEmail(email, subject, body)
	if err != nil {
		return err
	}

	return nil
}