summary refs log tree commit diff stats
path: root/auth/register.go
blob: f9460722749c06a63493936a7ae9ab19a25881ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package auth

import (
	"errors"
	"log"
	"regexp"
	"strings"

	"tildegit.org/andinus/perseus/storage/sqlite3"
	"tildegit.org/andinus/perseus/user"
)

// Register takes in registration details and returns an error. If
// error doesn't equal nil then the registration was unsuccessful.
// uInfo should have username & password.
func Register(db *sqlite3.DB, uInfo map[string]string) error {
	u := user.User{}
	u.SetID(genID(64))
	u.SetUsername(strings.ToLower(uInfo["username"]))

	// Validate username
	re := regexp.MustCompile("^[a-z0-9]*$")
	if !re.MatchString(u.Username()) {
		return errors.New("auth/register.go: invalid username")
	}

	// Validate password
	if len(uInfo["password"]) < 8 {
		return errors.New("auth/register.go: password too short")
	}

	pass, err := hashPass(uInfo["password"])
	if err != nil {
		log.Printf("auth/register.go: %s\n",
			"hashPass func failed")
		return err
	}
	u.SetPassword(pass)

	err = u.AddUser(db)
	return err
}