Skip to content

Commit

Permalink
chore: refactor code to remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
mitar committed Nov 16, 2021
1 parent 8760dff commit 86ae885
Showing 1 changed file with 10 additions and 45 deletions.
55 changes: 10 additions & 45 deletions persistence/sql/persister_oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ func (p *Persister) RevokeAccessToken(ctx context.Context, id string) error {
return p.deleteSessionByRequestID(ctx, id, sqlTableAccess)
}

func (p *Persister) FlushInactiveAccessTokens(ctx context.Context, notAfter time.Time, limit int, batchSize int) error {
func (p *Persister) flushInactiveTokens(ctx context.Context, notAfter time.Time, limit int, batchSize int, table tableName, lifespan time.Duration) error {
/* #nosec G201 table is static */
// The value of notAfter should be the minimum between input parameter and access token max expire based on its configured age
requestMaxExpire := time.Now().Add(-p.config.AccessTokenLifespan())
// The value of notAfter should be the minimum between input parameter and token max expire based on its configured age
requestMaxExpire := time.Now().Add(-lifespan)
if requestMaxExpire.Before(notAfter) {
notAfter = requestMaxExpire
}
Expand All @@ -374,7 +374,7 @@ func (p *Persister) FlushInactiveAccessTokens(ctx context.Context, notAfter time
// Select tokens' signatures with limit
q := p.Connection(ctx).RawQuery(
fmt.Sprintf("SELECT signature FROM %s WHERE requested_at < ? ORDER BY signature LIMIT %d",
OAuth2RequestSQL{Table: sqlTableAccess}.TableName(), limit),
OAuth2RequestSQL{Table: table}.TableName(), limit),
notAfter,
)
if err := q.All(&signatures); err == sql.ErrNoRows {
Expand All @@ -393,7 +393,7 @@ func (p *Persister) FlushInactiveAccessTokens(ctx context.Context, notAfter time

if i != j {
err = p.Connection(ctx).RawQuery(
fmt.Sprintf("DELETE FROM %s WHERE signature in (?)", OAuth2RequestSQL{Table: sqlTableAccess}.TableName()),
fmt.Sprintf("DELETE FROM %s WHERE signature in (?)", OAuth2RequestSQL{Table: table}.TableName()),
signatures[i:j],
).Exec()
if err != nil {
Expand All @@ -404,47 +404,12 @@ func (p *Persister) FlushInactiveAccessTokens(ctx context.Context, notAfter time
return sqlcon.HandleError(err)
}

func (p *Persister) FlushInactiveRefreshTokens(ctx context.Context, notAfter time.Time, limit int, batchSize int) error {
/* #nosec G201 table is static */
// The value of notAfter should be the minimum between input parameter and refresh token max expire based on its configured age
requestMaxExpire := time.Now().Add(-p.config.RefreshTokenLifespan())
if requestMaxExpire.Before(notAfter) {
notAfter = requestMaxExpire
}

signatures := []string{}

// Select tokens' signatures with limit
q := p.Connection(ctx).RawQuery(
fmt.Sprintf("SELECT signature FROM %s WHERE requested_at < ? ORDER BY signature LIMIT %d",
OAuth2RequestSQL{Table: sqlTableRefresh}.TableName(), limit),
notAfter,
)
if err := q.All(&signatures); err == sql.ErrNoRows {
return errorsx.WithStack(fosite.ErrNotFound)
} else if err != nil {
return errorsx.WithStack(err)
}

// Delete tokens in batch
var err error
for i := 0; i < len(signatures); i += batchSize {
j := i + batchSize
if j > len(signatures) {
j = len(signatures)
}
func (p *Persister) FlushInactiveAccessTokens(ctx context.Context, notAfter time.Time, limit int, batchSize int) error {
return p.flushInactiveTokens(ctx, notAfter, limit, batchSize, sqlTableAccess, p.config.AccessTokenLifespan())
}

if i != j {
err = p.Connection(ctx).RawQuery(
fmt.Sprintf("DELETE FROM %s WHERE signature in (?)", OAuth2RequestSQL{Table: sqlTableRefresh}.TableName()),
signatures[i:j],
).Exec()
if err != nil {
break
}
}
}
return sqlcon.HandleError(err)
func (p *Persister) FlushInactiveRefreshTokens(ctx context.Context, notAfter time.Time, limit int, batchSize int) error {
return p.flushInactiveTokens(ctx, notAfter, limit, batchSize, sqlTableRefresh, p.config.RefreshTokenLifespan())
}

func (p *Persister) DeleteAccessTokens(ctx context.Context, clientID string) error {
Expand Down

0 comments on commit 86ae885

Please sign in to comment.