1
0
Fork 0

Session management refactoring

This commit is contained in:
Frédéric Guillot 2017-12-16 18:07:53 -08:00
parent 58acd1d5e3
commit 00257988ef
26 changed files with 465 additions and 276 deletions

View file

@ -12,7 +12,7 @@ import (
"github.com/miniflux/miniflux/sql"
)
const schemaVersion = 9
const schemaVersion = 10
// Migrate run database migrations.
func (s *Storage) Migrate() {

77
storage/session.go Normal file
View file

@ -0,0 +1,77 @@
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package storage
import (
"database/sql"
"fmt"
"github.com/miniflux/miniflux/helper"
"github.com/miniflux/miniflux/model"
)
// CreateSession creates a new session.
func (s *Storage) CreateSession() (*model.Session, error) {
session := model.Session{
ID: helper.GenerateRandomString(32),
Data: &model.SessionData{CSRF: helper.GenerateRandomString(64)},
}
query := "INSERT INTO sessions (id, data) VALUES ($1, $2)"
_, err := s.db.Exec(query, session.ID, session.Data)
if err != nil {
return nil, fmt.Errorf("unable to create session: %v", err)
}
return &session, nil
}
// UpdateSessionField updates only one session field.
func (s *Storage) UpdateSessionField(sessionID, field string, value interface{}) error {
query := `UPDATE sessions
SET data = jsonb_set(data, '{%s}', to_jsonb($1::text), true)
WHERE id=$2`
_, err := s.db.Exec(fmt.Sprintf(query, field), value, sessionID)
if err != nil {
return fmt.Errorf("unable to update session field: %v", err)
}
return nil
}
// Session returns the given session.
func (s *Storage) Session(id string) (*model.Session, error) {
var session model.Session
query := "SELECT id, data FROM sessions WHERE id=$1"
err := s.db.QueryRow(query, id).Scan(
&session.ID,
&session.Data,
)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("session not found: %s", id)
} else if err != nil {
return nil, fmt.Errorf("unable to fetch session: %v", err)
}
return &session, nil
}
// FlushAllSessions removes all sessions from the database.
func (s *Storage) FlushAllSessions() (err error) {
_, err = s.db.Exec(`DELETE FROM user_sessions`)
if err != nil {
return err
}
_, err = s.db.Exec(`DELETE FROM sessions`)
if err != nil {
return err
}
return nil
}

View file

@ -1,48 +0,0 @@
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package storage
import (
"database/sql"
"fmt"
"github.com/miniflux/miniflux/helper"
"github.com/miniflux/miniflux/model"
)
// CreateToken creates a new token.
func (s *Storage) CreateToken() (*model.Token, error) {
token := model.Token{
ID: helper.GenerateRandomString(32),
Value: helper.GenerateRandomString(64),
}
query := "INSERT INTO tokens (id, value) VALUES ($1, $2)"
_, err := s.db.Exec(query, token.ID, token.Value)
if err != nil {
return nil, fmt.Errorf("unable to create token: %v", err)
}
return &token, nil
}
// Token returns a Token.
func (s *Storage) Token(id string) (*model.Token, error) {
var token model.Token
query := "SELECT id, value FROM tokens WHERE id=$1"
err := s.db.QueryRow(query, id).Scan(
&token.ID,
&token.Value,
)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("token not found: %s", id)
} else if err != nil {
return nil, fmt.Errorf("unable to fetch token: %v", err)
}
return &token, nil
}

View file

@ -127,9 +127,3 @@ func (s *Storage) RemoveUserSessionByID(userID, sessionID int64) error {
return nil
}
// FlushAllSessions removes all user sessions from the database.
func (s *Storage) FlushAllSessions() (err error) {
_, err = s.db.Exec(`DELETE FROM user_sessions`)
return
}