From f1cdfe6a54377f299f94b3f2735f23a4fccfb5ce Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Thu, 25 Mar 2021 20:10:10 +0100 Subject: [PATCH] validate new account requests --- internal/module/account/account.go | 37 +++++++++++--- internal/module/account/validation.go | 57 ++++++++++++++++++++++ internal/module/account/validation_test.go | 19 ++++++++ 3 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 internal/module/account/validation.go create mode 100644 internal/module/account/validation_test.go diff --git a/internal/module/account/account.go b/internal/module/account/account.go index db04ed0b3..c820f6618 100644 --- a/internal/module/account/account.go +++ b/internal/module/account/account.go @@ -28,6 +28,7 @@ import ( "github.com/gotosocial/gotosocial/internal/module" "github.com/gotosocial/gotosocial/internal/module/oauth" "github.com/gotosocial/gotosocial/internal/router" + "github.com/gotosocial/gotosocial/pkg/mastotypes" "github.com/sirupsen/logrus" ) @@ -48,30 +49,50 @@ func New(config *config.Config, db db.DB, log *logrus.Logger) module.ClientAPIMo return &accountModule{ config: config, db: db, - log: log, + log: log, } } // Route attaches all routes from this module to the given router func (m *accountModule) Route(r router.Router) error { - r.AttachHandler(http.MethodPost, basePath, m.AccountCreatePOSTHandler) - r.AttachHandler(http.MethodGet, verifyPath, m.AccountVerifyGETHandler) + r.AttachHandler(http.MethodPost, basePath, m.accountCreatePOSTHandler) + r.AttachHandler(http.MethodGet, verifyPath, m.accountVerifyGETHandler) return nil } -func (m *accountModule) AccountCreatePOSTHandler(c *gin.Context) { +func (m *accountModule) accountCreatePOSTHandler(c *gin.Context) { l := m.log.WithField("func", "AccountCreatePOSTHandler") + // TODO: check whether a valid app token has been presented!! + // See: https://docs.joinmastodon.org/methods/accounts/ + l.Trace("checking if registration is open") if !m.config.AccountsConfig.OpenRegistration { - l.Trace("account registration is closed, returning error to client") + l.Debug("account registration is closed, returning error to client") + c.JSON(http.StatusUnauthorized, gin.H{"error": "account registration is closed"}) + return + } + + l.Trace("parsing request form") + form := &mastotypes.AccountCreateRequest{} + if err := c.ShouldBind(form); err != nil { + l.Debugf("could not parse form from request: %s", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + l.Tracef("validating form %+v", form) + if err := validateCreateAccount(form, m.config.AccountsConfig.ReasonRequired, m.db); err != nil { + l.Debugf("error validating form: %s", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return } } -// AccountVerifyGETHandler serves a user's account details to them IF they reached this +// accountVerifyGETHandler serves a user's account details to them IF they reached this // handler while in possession of a valid token, according to the oauth middleware. -func (m *accountModule) AccountVerifyGETHandler(c *gin.Context) { +func (m *accountModule) accountVerifyGETHandler(c *gin.Context) { l := m.log.WithField("func", "AccountVerifyGETHandler") - + l.Trace("getting account details from session") i, ok := c.Get(oauth.SessionAuthorizedAccount) if !ok { diff --git a/internal/module/account/validation.go b/internal/module/account/validation.go new file mode 100644 index 000000000..3078b68d1 --- /dev/null +++ b/internal/module/account/validation.go @@ -0,0 +1,57 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package account + +import ( + "errors" + + "github.com/gotosocial/gotosocial/internal/db" + "github.com/gotosocial/gotosocial/internal/util" + "github.com/gotosocial/gotosocial/pkg/mastotypes" +) + +func validateCreateAccount(form *mastotypes.AccountCreateRequest, reasonRequired bool, db db.DB) error { + if err := util.ValidateSignUpUsername(form.Username); err != nil { + return err + } + + if err := util.ValidateEmail(form.Email); err != nil { + return err + } + + if err := util.ValidateSignUpPassword(form.Password); err != nil { + return err + } + + if !form.Agreement { + return errors.New("agreement to terms and conditions not given") + } + + if err := util.ValidateLanguage(form.Locale); err != nil { + return err + } + + if err := util.ValidateSignUpReason(form.Reason, reasonRequired); err != nil { + return err + } + + //TODO: validate new email address and new username + + return nil +} diff --git a/internal/module/account/validation_test.go b/internal/module/account/validation_test.go new file mode 100644 index 000000000..223a0c145 --- /dev/null +++ b/internal/module/account/validation_test.go @@ -0,0 +1,19 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package account