summary refs log tree commit diff stats
path: root/auth
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-03-27 17:23:02 +0530
committerAndinus <andinus@nand.sh>2020-03-27 17:23:02 +0530
commit5c62f93da80611fc03b96f73f8787513e86c0477 (patch)
tree3ed98e21d785fb045d4656ef8c813c6054cce499 /auth
parentf22adf24e886cf4382a26b4777c5b481b21c26f5 (diff)
downloadperseus-5c62f93da80611fc03b96f73f8787513e86c0477.tar.gz
Add Login func
Diffstat (limited to 'auth')
-rw-r--r--auth/login.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/auth/login.go b/auth/login.go
new file mode 100644
index 0000000..66d1b21
--- /dev/null
+++ b/auth/login.go
@@ -0,0 +1,46 @@
+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, loginInfo map[string]string) error {
+	// Acquire read lock on the database.
+	db.Mu.RLock()
+	defer db.Mu.RUnlock()
+
+	u := user.User{}
+	u.SetUsername(loginInfo["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(loginInfo["password"], u.Password())
+	if err != nil {
+		log.Printf("auth/login.go: %s%s\n",
+			"user login failed, username: ", u.Username())
+	}
+
+	return err
+}