Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions server/grants/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grants
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"slices"
Expand Down Expand Up @@ -109,6 +110,11 @@ func (g *refresh) Authorize(ctx context.Context, req *Request, client storage.Cl
return nil, &oauth2.Error{Type: oauth2.InvalidRequest, Status: http.StatusInternalServerError}
}
userIdent = &ui
if !ui.BlockedUntil.IsZero() && g.now().Before(ui.BlockedUntil) {
g.logger.WarnContext(ctx, "refresh: refused, account is locked",
"client_id", refreshToken.ClientID, "user_id", refreshToken.Claims.UserID, "blocked_until", ui.BlockedUntil)
return nil, &oauth2.Error{Type: oauth2.InvalidGrant, Description: fmt.Sprintf("Account is locked until %s.", ui.BlockedUntil.Format(time.RFC3339)), Status: http.StatusBadRequest}
}
}

authTime := time.Time{}
Expand Down
33 changes: 33 additions & 0 deletions server/server_grant_refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,3 +1040,36 @@ func TestRefreshDoesNotLogConnectorData(t *testing.T) {
// no log line carries the connector data itself.
require.NotContains(t, logs, "connector_data=")
}

// TestRefreshLockedAccountDenied covers issue #5176: an account locked after
// login (BlockedUntil in the future) must not keep refreshing from its cached
// identity, the same way the browser login and the password grant refuse a
// locked account.
func TestRefreshLockedAccountDenied(t *testing.T) {
httpServer, s := newTestServer(t, func(c *Config) {
c.PasswordConnector = "local"
c.SessionConfig = testSessionConfig()
})
defer httpServer.Close()
seedLocalPasswordDB(t, s)

first := doPasswordGrant(t, s)

ctx := t.Context()
require.NoError(t, s.storage.UpdateUserIdentity(ctx, "user-id", "local", func(ui storage.UserIdentity) (storage.UserIdentity, error) {
ui.BlockedUntil = time.Now().Add(time.Hour)
return ui, nil
}))

rr := doRefreshGrant(t, s, first, 0)
require.Equal(t, http.StatusBadRequest, rr.Code, "locked account refreshed: %s", rr.Body.String())
require.Contains(t, rr.Body.String(), "invalid_grant")

// Clear the block: the same refresh token works again.
require.NoError(t, s.storage.UpdateUserIdentity(ctx, "user-id", "local", func(ui storage.UserIdentity) (storage.UserIdentity, error) {
ui.BlockedUntil = time.Time{}
return ui, nil
}))
rr = doRefreshGrant(t, s, first, 0)
require.Equal(t, http.StatusOK, rr.Code, "unlocked account refresh failed: %s", rr.Body.String())
}
Loading