Update authentication, reauth, logout to use new transactions

This commit is contained in:
2025-02-17 18:58:34 +11:00
parent 417daf0028
commit 2c61cec55c
17 changed files with 306 additions and 121 deletions

View File

@@ -1,32 +1,34 @@
package jwt
import (
"database/sql"
"context"
"projectreshoot/db"
"github.com/pkg/errors"
)
// Revoke a token by adding it to the database
func RevokeToken(conn *sql.DB, t Token) error {
func RevokeToken(ctx context.Context, tx *db.SafeTX, t Token) error {
jti := t.GetJTI()
exp := t.GetEXP()
query := `INSERT INTO jwtblacklist (jti, exp) VALUES (?, ?)`
_, err := conn.Exec(query, jti, exp)
_, err := tx.Exec(ctx, query, jti, exp)
if err != nil {
return errors.Wrap(err, "conn.Exec")
return errors.Wrap(err, "tx.Exec")
}
return nil
}
// Check if a token has been revoked. Returns true if not revoked.
func CheckTokenNotRevoked(conn *sql.DB, t Token) (bool, error) {
func CheckTokenNotRevoked(ctx context.Context, tx *db.SafeTX, t Token) (bool, error) {
jti := t.GetJTI()
query := `SELECT 1 FROM jwtblacklist WHERE jti = ? LIMIT 1`
rows, err := conn.Query(query, jti)
defer rows.Close()
rows, err := tx.Query(ctx, query, jti)
if err != nil {
return false, errors.Wrap(err, "conn.Exec")
return false, errors.Wrap(err, "tx.Query")
}
// NOTE: rows.Close()
defer rows.Close()
revoked := rows.Next()
return !revoked, nil
}