From f6c226e97aafe460148a74d9ac2482129e5002e0 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Fri, 8 Nov 2024 17:04:43 +0200 Subject: [PATCH] Fix errors with trying to fetch sessions from database --- tokens/sessions.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tokens/sessions.go b/tokens/sessions.go index 0760f92..8f13c48 100644 --- a/tokens/sessions.go +++ b/tokens/sessions.go @@ -2,19 +2,15 @@ package tokens import ( "crypto/sha256" + "encoding/base64" "log" "time" "git.qowevisa.me/Qowevisa/fin-check-api/db" ) -func getSalt() []byte { - return []byte("w40DJV3v1flySvFdxHWbBSJsIOaakkVs5FG7brq4oi1#nEz2fEZxpUfyBwkkww7f") -} - func CreateSessionFromToken(token string, userID uint) error { - salt := getSalt() - sessionID := sha256.New().Sum(append(salt, []byte(token)...)) + sessionID := getSessionIDFromToken(token) dbc := db.Connect() session := &db.Session{ ID: string(sessionID), @@ -28,11 +24,10 @@ func CreateSessionFromToken(token string, userID uint) error { } func ValidateSessionToken(token string) bool { - salt := getSalt() - sessionID := sha256.New().Sum(append(salt, []byte(token)...)) + sessionID := getSessionIDFromToken(token) dbc := db.Connect() session := &db.Session{} - if err := dbc.Find(session, sessionID).Error; err != nil { + if err := dbc.Debug().Find(session, db.Session{ID: sessionID}).Error; err != nil { log.Printf("DBERROR: %v\n", err) return false } @@ -47,12 +42,17 @@ func ValidateSessionToken(token string) bool { } func GetSession(token string) (*db.Session, error) { - salt := getSalt() - sessionID := sha256.New().Sum(append(salt, []byte(token)...)) + sessionID := getSessionIDFromToken(token) dbc := db.Connect() session := &db.Session{} - if err := dbc.Find(session, sessionID).Error; err != nil { + if err := dbc.Find(session, db.Session{ID: sessionID}).Error; err != nil { return nil, err } return session, nil } + +func getSessionIDFromToken(token string) string { + salt := []byte("w40DJV3v1flySvFdxHWbBSJsIOaakkVs5FG7brq4oi1#nEz2fEZxpUfyBwkkww7f") + bytes := sha256.New().Sum(append(salt, []byte(token)...)) + return base64.URLEncoding.EncodeToString(bytes) +}