diff options
author | Andinus <andinus@nand.sh> | 2020-03-29 16:10:59 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-03-29 16:10:59 +0530 |
commit | a6826055bf4e6a23f80da047ccfe4509a209f3a6 (patch) | |
tree | f2bb2bec35ee5e61cb42f4edffb04368a0f8ba60 /account/register.go | |
parent | 7b95d6b80dd2d1efb26f7c515383abd4f0dc9d42 (diff) | |
download | perseus-a6826055bf4e6a23f80da047ccfe4509a209f3a6.tar.gz |
Initial perseus rewrite
Diffstat (limited to 'account/register.go')
-rw-r--r-- | account/register.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/account/register.go b/account/register.go new file mode 100644 index 0000000..e008370 --- /dev/null +++ b/account/register.go @@ -0,0 +1,49 @@ +package account + +import ( + "errors" + "log" + "regexp" + "strings" + + "tildegit.org/andinus/perseus/password" + "tildegit.org/andinus/perseus/storage" +) + +// Register takes in registration details and returns an error. If +// error doesn't equal nil then the registration was unsuccessful. +func (u User) Register(db *storage.DB) error { + var err error + u.ID = password.RandStr(64) + u.Username = strings.ToLower(u.Username) + + // Validate username. It must be alphanumeric and less than + // 128 characters. + re := regexp.MustCompile("^[a-zA-Z0-9]*$") + if !re.MatchString(u.Username) { + return errors.New("account/register.go: invalid username") + } + if len(u.Username) > 128 { + return errors.New("account/register.go: username too long") + } + + // Validate password + if len(u.Password) < 8 { + return errors.New("account/register.go: password too short") + } + + u.Hash, err = password.Hash(u.Password) + if err != nil { + log.Printf("account/register.go: %s\n", + "password.Hash func failed") + return err + } + + err = u.addUser(db) + if err != nil { + log.Printf("account/register.go: %s\n", + "addUser func failed") + } + return err + +} |