summary refs log tree commit diff stats
path: root/auth
diff options
context:
space:
mode:
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
+}