2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-12-16 15:15:33 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package storage // import "miniflux.app/v2/internal/storage"
|
2017-12-16 15:15:33 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/crypto"
|
|
|
|
"miniflux.app/v2/internal/model"
|
2017-12-16 15:15:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// UserSessions returns the list of sessions for the given user.
|
|
|
|
func (s *Storage) UserSessions(userID int64) (model.UserSessions, error) {
|
2019-10-30 01:48:07 -04:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
user_id,
|
|
|
|
token,
|
|
|
|
created_at,
|
|
|
|
user_agent,
|
|
|
|
ip
|
|
|
|
FROM
|
|
|
|
user_sessions
|
|
|
|
WHERE
|
|
|
|
user_id=$1 ORDER BY id DESC
|
|
|
|
`
|
2017-12-16 15:15:33 -05:00
|
|
|
rows, err := s.db.Query(query, userID)
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch user sessions: %v`, err)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var sessions model.UserSessions
|
|
|
|
for rows.Next() {
|
|
|
|
var session model.UserSession
|
|
|
|
err := rows.Scan(
|
|
|
|
&session.ID,
|
|
|
|
&session.UserID,
|
|
|
|
&session.Token,
|
|
|
|
&session.CreatedAt,
|
|
|
|
&session.UserAgent,
|
|
|
|
&session.IP,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch user session row: %v`, err)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sessions = append(sessions, &session)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sessions, nil
|
|
|
|
}
|
|
|
|
|
2020-12-22 23:28:34 -05:00
|
|
|
// CreateUserSessionFromUsername creates a new user session.
|
|
|
|
func (s *Storage) CreateUserSessionFromUsername(username, userAgent, ip string) (sessionID string, userID int64, err error) {
|
|
|
|
token := crypto.GenerateRandomString(64)
|
|
|
|
|
|
|
|
tx, err := s.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, fmt.Errorf(`store: unable to start transaction: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.QueryRow(`SELECT id FROM users WHERE username = LOWER($1)`, username).Scan(&userID)
|
2017-12-16 15:15:33 -05:00
|
|
|
if err != nil {
|
2020-12-22 23:28:34 -05:00
|
|
|
tx.Rollback()
|
2019-10-30 01:48:07 -04:00
|
|
|
return "", 0, fmt.Errorf(`store: unable to fetch user ID: %v`, err)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
2020-12-22 23:28:34 -05:00
|
|
|
_, err = tx.Exec(
|
|
|
|
`INSERT INTO user_sessions (token, user_id, user_agent, ip) VALUES ($1, $2, $3, $4)`,
|
|
|
|
token,
|
|
|
|
userID,
|
|
|
|
userAgent,
|
|
|
|
ip,
|
|
|
|
)
|
2017-12-16 15:15:33 -05:00
|
|
|
if err != nil {
|
2020-12-22 23:28:34 -05:00
|
|
|
tx.Rollback()
|
2019-10-30 01:48:07 -04:00
|
|
|
return "", 0, fmt.Errorf(`store: unable to create user session: %v`, err)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
2020-12-22 23:28:34 -05:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return "", 0, fmt.Errorf(`store: unable to commit transaction: %v`, err)
|
|
|
|
}
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
return token, userID, nil
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// UserSessionByToken finds a session by the token.
|
|
|
|
func (s *Storage) UserSessionByToken(token string) (*model.UserSession, error) {
|
|
|
|
var session model.UserSession
|
|
|
|
|
2019-10-30 01:48:07 -04:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
user_id,
|
|
|
|
token,
|
|
|
|
created_at,
|
|
|
|
user_agent,
|
|
|
|
ip
|
|
|
|
FROM
|
|
|
|
user_sessions
|
|
|
|
WHERE
|
|
|
|
token = $1
|
|
|
|
`
|
2017-12-16 15:15:33 -05:00
|
|
|
err := s.db.QueryRow(query, token).Scan(
|
|
|
|
&session.ID,
|
|
|
|
&session.UserID,
|
|
|
|
&session.Token,
|
|
|
|
&session.CreatedAt,
|
|
|
|
&session.UserAgent,
|
|
|
|
&session.IP,
|
|
|
|
)
|
|
|
|
|
2019-10-30 01:48:07 -04:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2018-04-10 00:52:24 -04:00
|
|
|
return nil, nil
|
2019-10-30 01:48:07 -04:00
|
|
|
case err != nil:
|
|
|
|
return nil, fmt.Errorf(`store: unable to fetch user session: %v`, err)
|
|
|
|
default:
|
|
|
|
return &session, nil
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveUserSessionByToken remove a session by using the token.
|
|
|
|
func (s *Storage) RemoveUserSessionByToken(userID int64, token string) error {
|
2019-10-30 01:48:07 -04:00
|
|
|
query := `DELETE FROM user_sessions WHERE user_id=$1 AND token=$2`
|
|
|
|
result, err := s.db.Exec(query, userID, token)
|
2017-12-16 15:15:33 -05:00
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to remove this user session: %v`, err)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
count, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to remove this user session: %v`, err)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if count != 1 {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: nothing has been removed`)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveUserSessionByID remove a session by using the ID.
|
|
|
|
func (s *Storage) RemoveUserSessionByID(userID, sessionID int64) error {
|
2019-10-30 01:48:07 -04:00
|
|
|
query := `DELETE FROM user_sessions WHERE user_id=$1 AND id=$2`
|
|
|
|
result, err := s.db.Exec(query, userID, sessionID)
|
2017-12-16 15:15:33 -05:00
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to remove this user session: %v`, err)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
count, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to remove this user session: %v`, err)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if count != 1 {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: nothing has been removed`)
|
2017-12-16 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-16 21:48:17 -05:00
|
|
|
|
2019-09-11 23:10:34 -04:00
|
|
|
// CleanOldUserSessions removes user sessions older than specified days.
|
|
|
|
func (s *Storage) CleanOldUserSessions(days int) int64 {
|
2019-10-30 01:48:07 -04:00
|
|
|
query := `
|
|
|
|
DELETE FROM
|
|
|
|
user_sessions
|
|
|
|
WHERE
|
|
|
|
id IN (SELECT id FROM user_sessions WHERE created_at < now() - interval '%d days')
|
|
|
|
`
|
|
|
|
result, err := s.db.Exec(fmt.Sprintf(query, days))
|
2017-12-16 21:48:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
return n
|
|
|
|
}
|