5376b9e271
* New config files As decided, config files are parsed at runtime. I decided to go for YAML config files because there can be comments in it. There are 2 files: * config/default_config.yml <= which shouldn't be edited unless we add a config parameter * config/config.yml <= which is the user-defined config. This file shouldn't be commited Changed every call to config.XXX to config.Conf.XXX (look to the new stucture of config in config/types.go) Of course, putting config parameters in config.yml overrides config in config_default.yml. You don't have to put everything in it, just add what you want to override. * Fixing test Replacing conf.New by config.Conf * Fixing call to config.Conf to config.Config{} in test files * Might have fixed testing with this Printf instead of Fatalf * Renaming config.yml in example file * Forbid commiting config.yml * Should be now fixed * Do not need this file anymore
69 lignes
2,3 Kio
Go
69 lignes
2,3 Kio
Go
package userService
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/NyaaPantsu/nyaa/config"
|
|
"github.com/NyaaPantsu/nyaa/db"
|
|
"github.com/NyaaPantsu/nyaa/model"
|
|
"github.com/NyaaPantsu/nyaa/util/email"
|
|
"github.com/NyaaPantsu/nyaa/util/publicSettings"
|
|
"github.com/NyaaPantsu/nyaa/util/timeHelper"
|
|
"github.com/gorilla/securecookie"
|
|
)
|
|
|
|
var verificationHandler = securecookie.New(config.EmailTokenHashKey, nil)
|
|
|
|
// SendEmailVerification sends an email verification token via email.
|
|
func SendEmailVerification(to string, token string) error {
|
|
T, err := publicSettings.GetDefaultTfunc()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
content := T("link") + " : https://" + config.Conf.WebAddress + "/verify/email/" + token
|
|
contentHTML := T("verify_email_content") + "<br/>" + "<a href=\"https://" + config.Conf.WebAddress + "/verify/email/" + token + "\" target=\"_blank\">" + config.Conf.WebAddress + "/verify/email/" + token + "</a>"
|
|
return email.SendEmailFromAdmin(to, T("verify_email_title"), content, contentHTML)
|
|
}
|
|
|
|
// SendVerificationToUser sends an email verification token to user.
|
|
func SendVerificationToUser(user model.User, newEmail string) (int, error) {
|
|
validUntil := timeHelper.TwentyFourHoursLater() // TODO: longer duration?
|
|
value := map[string]string{
|
|
"t": strconv.FormatInt(validUntil.Unix(), 10),
|
|
"u": strconv.FormatUint(uint64(user.ID), 10),
|
|
"e": newEmail,
|
|
}
|
|
encoded, err := verificationHandler.Encode("", value)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
err = SendEmailVerification(newEmail, encoded)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
return http.StatusOK, nil
|
|
}
|
|
|
|
// EmailVerification verifies the token used for email verification
|
|
func EmailVerification(token string, w http.ResponseWriter) (int, error) {
|
|
value := make(map[string]string)
|
|
err := verificationHandler.Decode("", token, &value)
|
|
if err != nil {
|
|
fmt.Printf("%+v\n", err)
|
|
return http.StatusForbidden, errors.New("Token is not valid")
|
|
}
|
|
timeInt, _ := strconv.ParseInt(value["t"], 10, 0)
|
|
if timeHelper.IsExpired(time.Unix(timeInt, 0)) {
|
|
return http.StatusForbidden, errors.New("Token has expired")
|
|
}
|
|
var user model.User
|
|
if db.ORM.Where("user_id = ?", value["u"]).First(&user).RecordNotFound() {
|
|
return http.StatusNotFound, errors.New("User is not found")
|
|
}
|
|
user.Email = value["e"]
|
|
return UpdateUserCore(&user)
|
|
}
|