From 2179040833d35ebcaa57ca67458755df101eb39d Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Mon, 20 May 2024 17:35:01 +0100 Subject: [PATCH] Changed the data structure, will add the things when the time comes --- faxSrc/models/authentication.go | 45 ++++++++++++++++++++++++++++++--- faxSrc/models/dbAccess.go | 20 ++------------- faxSrc/usefulQueries.sql | 2 +- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/faxSrc/models/authentication.go b/faxSrc/models/authentication.go index 8e0b6c5..31c86cf 100644 --- a/faxSrc/models/authentication.go +++ b/faxSrc/models/authentication.go @@ -24,10 +24,16 @@ type Claims struct { jwt.RegisteredClaims } +type SessionData struct { + User string + Token string + ExpirationTime int64 +} + // Private functions -func openDB() *SQLite { - var d SQLite +func openDB() *Database { + var d Database err := d.Initialize("../src/data.db") if err != nil { log.Fatal("Failed to get into DB: ", err) @@ -78,8 +84,6 @@ func insertToken(token string, user string, expirationDate int64) error { ); ` - log.Println(query) - _, err := d.DB.Exec(query) if err != nil { log.Fatal("Failed to insert data: ", err) @@ -89,6 +93,35 @@ func insertToken(token string, user string, expirationDate int64) error { return nil } +func getSessionData(token string) (*SessionData, error) { + d := openDB() + + query := ` + SELECT user, token, expires FROM sessions WHERE token='` + token + `'; + ` + + rows, err := d.DB.Query(query) + if err != nil { + log.Fatal("Failed to get user data: ", err) + return nil, err + } + defer rows.Close() + + var p SessionData + for rows.Next() { + if err := rows.Scan(&p.User, &p.Token, &p.ExpirationTime); err != nil { + log.Fatal("Failed to read row.", err) + } + } + + // No password found (user doesn't exist?) + if len(p.Token) == 0 { + return nil, nil + } + + return &p, nil +} + // Public functions func ValidateCredentials(lf *LoginForm) bool { @@ -110,3 +143,7 @@ func StoreSessionToken(token string, user string, expirationDate int64) error { return nil } + +func GetSession(token string) (*SessionData, error) { + return getSessionData(token) +} diff --git a/faxSrc/models/dbAccess.go b/faxSrc/models/dbAccess.go index 5ed593e..a55c4c4 100644 --- a/faxSrc/models/dbAccess.go +++ b/faxSrc/models/dbAccess.go @@ -15,12 +15,12 @@ type DBAccess interface { updateq(query string) error } -type SQLite struct { +type Database struct { DB *sql.DB Path string } -func (s *SQLite) Initialize(path string) error { +func (s *Database) Initialize(path string) error { db, err := sql.Open("sqlite3", path) if err != nil { return err @@ -31,19 +31,3 @@ func (s *SQLite) Initialize(path string) error { return nil } - -func (s *SQLite) Createq(query string) error { - return nil -} - -func (s *SQLite) Selectq(query string) error { - return nil -} - -func (s *SQLite) Deleteq(query string) error { - return nil -} - -func (s *SQLite) Updateq(query string) error { - return nil -} diff --git a/faxSrc/usefulQueries.sql b/faxSrc/usefulQueries.sql index ac17793..880e6f9 100644 --- a/faxSrc/usefulQueries.sql +++ b/faxSrc/usefulQueries.sql @@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS users( CREATE TABLE IF NOT EXISTS sessions( id INTEGER PRIMARY KEY AUTOINCREMENT, - user TEXT NOT NULL UNIQUE, + user TEXT NOT NULL, token TEXT NOT NULL, expires INTEGER );