2022-12-31 15:34:38 -08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2023-04-03 19:11:03 -07:00
|
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
|
|
|
"github.com/jackc/pgx/v5"
|
2022-12-31 15:34:38 -08:00
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Token struct {
|
|
|
|
UserID xid.ID
|
|
|
|
TokenID xid.ID
|
|
|
|
Invalidated bool
|
2023-03-30 07:50:30 -07:00
|
|
|
APIOnly bool `db:"api_only"`
|
|
|
|
ReadOnly bool
|
2022-12-31 15:34:38 -08:00
|
|
|
Created time.Time
|
|
|
|
Expires time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) TokenValid(ctx context.Context, userID, tokenID xid.ID) (valid bool, err error) {
|
|
|
|
sql, args, err := sq.Select("*").From("tokens").
|
|
|
|
Where("user_id = ?", userID).
|
|
|
|
Where("token_id = ?", tokenID).
|
|
|
|
ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
var t Token
|
|
|
|
err = pgxscan.Get(ctx, db, &t, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == pgx.ErrNoRows {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errors.Wrap(err, "getting from database")
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
return !t.Invalidated && t.Created.Before(now) && t.Expires.After(now), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Tokens(ctx context.Context, userID xid.ID) (ts []Token, err error) {
|
|
|
|
sql, args, err := sq.Select("*").From("tokens").
|
|
|
|
Where("user_id = ?", userID).
|
|
|
|
Where("expires > ?", time.Now()).
|
|
|
|
OrderBy("created").
|
|
|
|
ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Select(ctx, db, &ts, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "getting from database")
|
|
|
|
}
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3 months, might be customizable later
|
|
|
|
const ExpiryTime = 3 * 30 * 24 * time.Hour
|
|
|
|
|
|
|
|
// SaveToken saves a token to the database.
|
2023-03-30 07:50:30 -07:00
|
|
|
func (db *DB) SaveToken(ctx context.Context, userID xid.ID, tokenID xid.ID, apiOnly, readOnly bool) (t Token, err error) {
|
2022-12-31 15:34:38 -08:00
|
|
|
sql, args, err := sq.Insert("tokens").
|
2023-03-30 07:50:30 -07:00
|
|
|
SetMap(map[string]any{
|
|
|
|
"user_id": userID,
|
|
|
|
"token_id": tokenID,
|
|
|
|
"expires": time.Now().Add(ExpiryTime),
|
|
|
|
"api_only": apiOnly,
|
|
|
|
"read_only": readOnly,
|
|
|
|
}).
|
2022-12-31 15:34:38 -08:00
|
|
|
Suffix("RETURNING *").
|
|
|
|
ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return t, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, db, &t, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return t, errors.Wrap(err, "inserting token")
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) InvalidateToken(ctx context.Context, userID xid.ID, tokenID xid.ID) (t Token, err error) {
|
|
|
|
sql, args, err := sq.Update("tokens").
|
2023-03-13 09:01:36 -07:00
|
|
|
Where("user_id = ?", userID).
|
|
|
|
Where("token_id = ?", tokenID).
|
2022-12-31 15:34:38 -08:00
|
|
|
Set("invalidated", true).
|
|
|
|
Suffix("RETURNING *").
|
|
|
|
ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return t, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, db, &t, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return t, errors.Wrap(err, "invalidating token")
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
2023-03-08 01:32:18 -08:00
|
|
|
|
2023-03-11 16:31:10 -08:00
|
|
|
func (db *DB) InvalidateAllTokens(ctx context.Context, tx pgx.Tx, userID xid.ID) error {
|
2023-03-08 01:32:18 -08:00
|
|
|
sql, args, err := sq.Update("tokens").Where("user_id = ?", userID).Set("invalidated", true).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
2023-03-11 16:31:10 -08:00
|
|
|
_, err = tx.Exec(ctx, sql, args...)
|
2023-03-08 01:32:18 -08:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|