2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-11 01:15:56 +08:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"net/http"
|
2018-05-27 22:40:42 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
func commenterNew(email string, name string, link string, photo string, provider string, password string) (string, error) {
|
2018-05-27 22:40:42 +08:00
|
|
|
if email == "" || name == "" || link == "" || photo == "" || provider == "" {
|
|
|
|
return "", errorMissingField
|
|
|
|
}
|
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
if provider == "commento" && password == "" {
|
|
|
|
return "", errorMissingField
|
|
|
|
}
|
|
|
|
|
2018-06-11 01:43:18 +08:00
|
|
|
// See utils_sanitise.go's documentation on isHttpsUrl. This is not a URL
|
|
|
|
// validator, just an XSS preventor.
|
|
|
|
// TODO: reject URLs instead of malforming them.
|
|
|
|
if !isHttpsUrl(link) {
|
|
|
|
link = "https://" + link
|
|
|
|
}
|
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
if _, err := commenterGetByEmail(provider, email); err == nil {
|
|
|
|
return "", errorEmailAlreadyExists
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
commenterHex, err := randomHex(32)
|
|
|
|
if err != nil {
|
|
|
|
return "", errorInternal
|
|
|
|
}
|
|
|
|
|
2018-06-11 01:15:56 +08:00
|
|
|
passwordHash := []byte{}
|
|
|
|
if (password != "") {
|
|
|
|
passwordHash, err = bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot generate hash from password: %v\n", err)
|
|
|
|
return "", errorInternal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
statement := `
|
|
|
|
INSERT INTO
|
2018-06-11 01:15:56 +08:00
|
|
|
commenters (commenterHex, email, name, link, photo, provider, passwordHash, joinDate)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8 );
|
2018-05-27 22:40:42 +08:00
|
|
|
`
|
2018-06-11 01:15:56 +08:00
|
|
|
_, err = db.Exec(statement, commenterHex, email, name, link, photo, provider, string(passwordHash), time.Now().UTC())
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
2018-06-11 01:15:56 +08:00
|
|
|
logger.Errorf("cannot insert commenter: %v", err)
|
2018-05-27 22:40:42 +08:00
|
|
|
return "", errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
return commenterHex, nil
|
|
|
|
}
|
2018-06-11 01:15:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
func commenterNewHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
type request struct {
|
|
|
|
Email *string `json:"email"`
|
|
|
|
Name *string `json:"name"`
|
|
|
|
Website *string `json:"website"`
|
|
|
|
Password *string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var x request
|
|
|
|
if err := unmarshalBody(r, &x); err != nil {
|
|
|
|
writeBody(w, response{"success": false, "message": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add gravatar?
|
|
|
|
// TODO: email confirmation if provider = commento?
|
|
|
|
// TODO: email confirmation if provider = commento?
|
|
|
|
if _, err := commenterNew(*x.Email, *x.Name, *x.Website, "undefined", "commento", *x.Password); err != nil {
|
|
|
|
writeBody(w, response{"success": false, "message": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeBody(w, response{"success": true, "confirmEmail": smtpConfigured})
|
|
|
|
}
|