summary refs log tree commit diff stats
path: root/auth
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-03-28 19:25:53 +0530
committerAndinus <andinus@nand.sh>2020-03-28 19:25:53 +0530
commit7b95d6b80dd2d1efb26f7c515383abd4f0dc9d42 (patch)
tree76dca236dc2d7c3be989896442cb3edfa1235e8d /auth
parentb0b83af1c45c2d5ee587dc96847e25c95a3d50b0 (diff)
downloadperseus-7b95d6b80dd2d1efb26f7c515383abd4f0dc9d42.tar.gz
Prepare for rewrite on several functions
User struct now exports everything, encapsulation is not necessary
over here. Instead of introducing a new variable uInfo we'll use
user.User and pass that. Handlers & related functions will be
rewritten to work with this change. This will make it easier to work
on later as the program grows. I'm also rethinking error handling.
Diffstat (limited to 'auth')
-rw-r--r--auth/login.go46
-rw-r--r--auth/register.go42
-rw-r--r--auth/token/add.go58
-rw-r--r--auth/token/generate.go14
-rw-r--r--auth/token/validate.go51
5 files changed, 0 insertions, 211 deletions
diff --git a/auth/login.go b/auth/login.go
deleted file mode 100644
index 99e74ba..0000000
--- a/auth/login.go
+++ /dev/null
@@ -1,46 +0,0 @@
-package auth
-
-import (
-	"log"
-
-	"tildegit.org/andinus/perseus/storage/sqlite3"
-	"tildegit.org/andinus/perseus/user"
-)
-
-// Login takes in login details and returns an error. If error doesn't
-// equal nil then consider login failed.
-func Login(db *sqlite3.DB, uInfo map[string]string) error {
-	// Acquire read lock on the database.
-	db.Mu.RLock()
-	defer db.Mu.RUnlock()
-
-	u := user.User{}
-	u.SetUsername(uInfo["username"])
-
-	// Get password for this user from the database.
-	stmt, err := db.Conn.Prepare("SELECT password FROM users WHERE username = ?")
-	if err != nil {
-		log.Printf("auth/login.go: %s\n",
-			"failed to prepare statement")
-		return err
-	}
-	defer stmt.Close()
-
-	var pass string
-	err = stmt.QueryRow(u.Username()).Scan(&pass)
-	if err != nil {
-		log.Printf("auth/login.go: %s\n",
-			"query failed")
-		return err
-	}
-	u.SetPassword(pass)
-
-	// Check user's password.
-	err = checkPass(uInfo["password"], u.Password())
-	if err != nil {
-		log.Printf("auth/login.go: %s%s\n",
-			"user login failed, username: ", u.Username())
-	}
-
-	return err
-}
diff --git a/auth/register.go b/auth/register.go
deleted file mode 100644
index f946072..0000000
--- a/auth/register.go
+++ /dev/null
@@ -1,42 +0,0 @@
-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
-}
diff --git a/auth/token/add.go b/auth/token/add.go
deleted file mode 100644
index eadc6dc..0000000
--- a/auth/token/add.go
+++ /dev/null
@@ -1,58 +0,0 @@
-package token
-
-import (
-	"log"
-	"time"
-
-	"tildegit.org/andinus/perseus/storage/sqlite3"
-	"tildegit.org/andinus/perseus/user"
-)
-
-// AddToken will generate a random token, add it to database and
-// return the token.
-func AddToken(db *sqlite3.DB, uInfo map[string]string) (token string, err error) {
-	// Acquire write lock on the database.
-	db.Mu.Lock()
-	defer db.Mu.Unlock()
-
-	token = genToken(64)
-
-	u := user.User{}
-	u.SetUsername(uInfo["username"])
-
-	// Set user id from username.
-	err = u.GetID(db)
-	if err != nil {
-		log.Printf("auth/token.go: %s\n",
-			"failed to get id from username")
-		return
-	}
-
-	// Start the transaction
-	tx, err := db.Conn.Begin()
-	if err != nil {
-		log.Printf("auth/token.go: %s\n",
-			"failed to begin transaction")
-		return
-	}
-
-	stmt, err := db.Conn.Prepare(`
-INSERT INTO access(id, token, genTime) values(?, ?, ?)`)
-	if err != nil {
-		log.Printf("auth/token.go: %s\n",
-			"failed to prepare statement")
-		return
-	}
-	defer stmt.Close()
-
-	_, err = stmt.Exec(u.ID(), u.Username(), time.Now().UTC())
-	if err != nil {
-		log.Printf("auth/token.go: %s\n",
-			"failed to execute statement")
-		return
-	}
-
-	tx.Commit()
-	return
-
-}
diff --git a/auth/token/generate.go b/auth/token/generate.go
deleted file mode 100644
index 0c717d9..0000000
--- a/auth/token/generate.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package token
-
-import (
-	"crypto/rand"
-	"encoding/base64"
-)
-
-// genToken generates a random token string of length n. Don't forget to
-// seed the random number generator otherwise it won't be random.
-func genToken(n int) string {
-	b := make([]byte, n/2)
-	rand.Read(b)
-	return base64.StdEncoding.EncodeToString(b)
-}
diff --git a/auth/token/validate.go b/auth/token/validate.go
deleted file mode 100644
index f1ee149..0000000
--- a/auth/token/validate.go
+++ /dev/null
@@ -1,51 +0,0 @@
-package token
-
-import (
-	"errors"
-	"log"
-
-	"tildegit.org/andinus/perseus/storage/sqlite3"
-	"tildegit.org/andinus/perseus/user"
-)
-
-// ValToken will validate the token and returns an error. If error
-// doesn't equal nil then consider token invalid.
-func ValToken(db *sqlite3.DB, uInfo map[string]string) error {
-	// Acquire read lock on the database.
-	db.Mu.RLock()
-	defer db.Mu.RUnlock()
-
-	u := user.User{}
-	u.SetUsername(uInfo["username"])
-
-	// Set user id from username.
-	err := u.GetID(db)
-	if err != nil {
-		log.Printf("auth/token.go: %s\n",
-			"failed to get id from username")
-		return err
-	}
-
-	// Check if user's token is valid.
-	stmt, err := db.Conn.Prepare("SELECT token FROM access WHERE id = ?")
-	if err != nil {
-		log.Printf("auth/token.go: %s\n",
-			"failed to prepare statement")
-		return err
-	}
-	defer stmt.Close()
-
-	var token string
-	err = stmt.QueryRow(u.ID()).Scan(&token)
-	if err != nil {
-		log.Printf("auth/token.go: %s\n",
-			"query failed")
-		return err
-	}
-
-	if token != uInfo["token"] {
-		err = errors.New("token mismatch")
-	}
-
-	return err
-}