From 9571ccc4d87907067df98edeaa78f0c167fcff43 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 13 May 2023 22:32:24 -0500 Subject: begin total refactor --- db/session.go | 139 ---------------------------------------------------------- 1 file changed, 139 deletions(-) delete mode 100644 db/session.go (limited to 'db/session.go') diff --git a/db/session.go b/db/session.go deleted file mode 100644 index 7ebb6df..0000000 --- a/db/session.go +++ /dev/null @@ -1,139 +0,0 @@ -package db - -import ( - "database/sql" - "crypto/rand" - "encoding/base64" - "errors" - "time" -) - - -func (p *Phlox) CreateSession(user User) (Session, error) { - bytes := make([]byte, 32) - _, err := rand.Read(bytes) - if err != nil { - return Session{}, err - } - - sessionid := base64.StdEncoding.EncodeToString(bytes) - userid := user.Id - now := time.Now().UTC() - nowStr := now.Format(time.RFC3339) - - _, err = p.db.Exec( - "insert into sessions (sessionid, userid, created, modified) values (?, ?, ?, ?);", - sessionid, - userid, - nowStr, nowStr, - ) - if err != nil { - return Session{}, err - } - - return Session{ - Id: sessionid, - UserId: userid, - Created: now, - Modified: now, - }, nil -} - - -func (p *Phlox) DeleteSession(session Session) error { - _, err := p.db.Exec("delete from sessions where sessionid = ?;", session.Id) - return err -} - - -func extractSession(s Scanner) (Session, error) { - var ( - session Session - createdStr string - modifiedStr string - ) - - // scan - err := s.Scan(&session.Id, &session.UserId, &createdStr, &modifiedStr) - if err != nil { - return Session{}, err - } - - // parse times - session.Created, err = time.Parse(time.RFC3339, createdStr) - if err != nil { - return Session{}, err - } - session.Modified, err = time.Parse(time.RFC3339, modifiedStr) - if err != nil { - return Session{}, err - } - - return session, nil -} - - -func (p *Phlox) CheckSession(session Session) (bool, error) { - row := p.db.QueryRow("select * from sessions where sessionid = ?", session.Id) - session, err := extractSession(row) - if err != nil { - if errors.Is(err, sql.ErrNoRows) { - // no row returned, so invalid session - return false, nil - } else { - // some other error - return false, err - } - } - - return true, nil -} - - - -func (p *Phlox) CheckSessionId(id string) (bool, error) { - session := Session{ Id: id } - return p.CheckSession(session) -} - - -func (p *Phlox) TouchSession(session Session) error { - now := time.Now().UTC().Format(time.RFC3339) - _, err := p.db.Exec( - "update sessions set modified = ? where sessionid = ?;", - now, session.Id, - ) - return err -} - - -func (p *Phlox) TouchSessionId(id string) error { - return p.TouchSession(Session{ Id: id }) -} - - -func (p *Phlox) CleanSessions(maxIdle time.Duration) error { - expire := time.Now().UTC().Add(-maxIdle).Format(time.RFC3339) - _, err := p.db.Exec("delete from sessions where modified < ?;", expire) - return err -} - - -func (p *Phlox) AllSessions() ([]Session, error) { - sessions := make([]Session, 0) - rows, err := p.db.Query("select * from sessions;") - if err != nil { - return sessions, err - } - defer rows.Close() - - for rows.Next() { - session, err := extractSession(rows) - if err != nil { - return sessions, err - } - sessions = append(sessions, session) - } - - return sessions, nil -} -- cgit v1.2.1