refactor to improve database operability

This commit is contained in:
2026-01-11 22:21:44 +11:00
parent 1b25e2f0a5
commit ae4094d426
13 changed files with 136 additions and 57 deletions

View File

@@ -1,8 +1,6 @@
package jwt
import (
"database/sql"
"github.com/pkg/errors"
)
@@ -20,14 +18,15 @@ import (
// revocation check is skipped.
//
// Parameters:
// - tx: Database transaction for checking token revocation status
// - tx: Database transaction for checking token revocation status.
// Accepts *sql.Tx or any ORM transaction implementing DBTransaction interface.
// - tokenString: The JWT token string to validate
//
// Returns:
// - *AccessToken: The validated token with all claims, or nil if validation fails
// - error: Detailed error if validation fails (expired, revoked, invalid signature, etc.)
func (gen *TokenGenerator) ValidateAccess(
tx *sql.Tx,
tx DBTransaction,
tokenString string,
) (*AccessToken, error) {
if tokenString == "" {
@@ -86,10 +85,10 @@ func (gen *TokenGenerator) ValidateAccess(
}
valid, err := token.CheckNotRevoked(tx)
if err != nil && gen.db != nil {
if err != nil && gen.beginTx != nil {
return nil, errors.Wrap(err, "token.CheckNotRevoked")
}
if !valid && gen.db != nil {
if !valid && gen.beginTx != nil {
return nil, errors.New("Token has been revoked")
}
return token, nil
@@ -109,14 +108,15 @@ func (gen *TokenGenerator) ValidateAccess(
// revocation check is skipped.
//
// Parameters:
// - tx: Database transaction for checking token revocation status
// - tx: Database transaction for checking token revocation status.
// Accepts *sql.Tx or any ORM transaction implementing DBTransaction interface.
// - tokenString: The JWT token string to validate
//
// Returns:
// - *RefreshToken: The validated token with all claims, or nil if validation fails
// - error: Detailed error if validation fails (expired, revoked, invalid signature, etc.)
func (gen *TokenGenerator) ValidateRefresh(
tx *sql.Tx,
tx DBTransaction,
tokenString string,
) (*RefreshToken, error) {
if tokenString == "" {
@@ -170,10 +170,10 @@ func (gen *TokenGenerator) ValidateRefresh(
}
valid, err := token.CheckNotRevoked(tx)
if err != nil && gen.db != nil {
if err != nil && gen.beginTx != nil {
return nil, errors.Wrap(err, "token.CheckNotRevoked")
}
if !valid && gen.db != nil {
if !valid && gen.beginTx != nil {
return nil, errors.New("Token has been revoked")
}
return token, nil