1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-08 19:00:25 +00:00

Actually check for illegal username, pretty errors

This commit is contained in:
handlerug 2021-07-01 15:45:03 +07:00
parent 530b2a97e1
commit 7073f9bce0
No known key found for this signature in database
GPG Key ID: 38009F0605051491
3 changed files with 19 additions and 4 deletions

View File

@ -1,4 +1,3 @@
//go:generate go get -u github.com/valyala/quicktemplate/qtc
//go:generate qtc -dir=views //go:generate qtc -dir=views
//go:generate qtc -dir=tree //go:generate qtc -dir=tree
// Command mycorrhiza is a program that runs a mycorrhiza wiki. // Command mycorrhiza is a program that runs a mycorrhiza wiki.

View File

@ -2,12 +2,13 @@ package user
import ( import (
"errors" "errors"
"github.com/bouncepaw/mycorrhiza/cfg" "fmt"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
@ -49,6 +50,7 @@ func Register(username, password string) error {
return errors.New("Username " + username + " is taken already.") return errors.New("Username " + username + " is taken already.")
case !util.IsPossibleUsername(username): case !util.IsPossibleUsername(username):
log.Println("Illegal username:", username) log.Println("Illegal username:", username)
return fmt.Errorf("Illegal username \"%s\".", username)
} }
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil { if err != nil {

View File

@ -1,11 +1,13 @@
package web package web
import ( import (
"github.com/bouncepaw/mycorrhiza/cfg" "fmt"
"io" "io"
"log" "log"
"mime"
"net/http" "net/http"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/views" "github.com/bouncepaw/mycorrhiza/views"
@ -46,7 +48,19 @@ func handlerRegister(w http.ResponseWriter, rq *http.Request) {
err = user.Register(username, password) err = user.Register(username, password)
) )
if err != nil { if err != nil {
io.WriteString(w, err.Error()) w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(
w,
views.BaseHTML(
"Register",
fmt.Sprintf(
`<main class="main-width"><p>%s</p><p><a href="/register">Try again<a></p></main>`,
err.Error(),
),
user.FromRequest(rq),
),
)
} else { } else {
user.LoginDataHTTP(w, rq, username, password) user.LoginDataHTTP(w, rq, username, password)
http.Redirect(w, rq, "/"+rq.URL.RawQuery, http.StatusSeeOther) http.Redirect(w, rq, "/"+rq.URL.RawQuery, http.StatusSeeOther)