Skip to content

Commit

Permalink
feat: add owner/name filter to list clients (#2637)
Browse files Browse the repository at this point in the history
Closes #1485

Co-authored-by: aeneasr <3372410+aeneasr@users.noreply.github.com>
  • Loading branch information
pike1212 and aeneasr authored Aug 2, 2021
1 parent 4f74591 commit ea6fdfd
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 175 deletions.
11 changes: 0 additions & 11 deletions client/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,6 @@ type patchDocument struct {
From string `json:"from"`
}

// swagger:parameters listOAuth2Clients
type swaggerListClientsParameter struct {
// The maximum amount of policies returned, upper bound is 500 policies
// in: query
Limit int `json:"limit"`

// The offset from where to start looking.
// in: query
Offset int `json:"offset"`
}

// A list of clients.
// swagger:response oAuth2ClientList
type swaggerListClientsResult struct {
Expand Down
27 changes: 26 additions & 1 deletion client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@ func (h *Handler) updateClient(ctx context.Context, c *Client) error {
return nil
}

// swagger:parameters listOAuth2Clients
type Filter struct {
// The maximum amount of clients to returned, upper bound is 500 clients.
// in: query
Limit int `json:"limit"`

// The offset from where to start looking.
// in: query
Offset int `json:"offset"`

// The name of the clients to filter by.
// in: query
Name string `json:"name"`

// The owner of the clients to filter by.
// in: query
Owner string `json:"owner"`
}

// swagger:route GET /clients admin listOAuth2Clients
//
// List OAuth 2.0 Clients
Expand All @@ -244,8 +263,14 @@ func (h *Handler) updateClient(ctx context.Context, c *Client) error {
// 500: jsonError
func (h *Handler) List(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
limit, offset := pagination.Parse(r, 100, 0, 500)
filters := Filter{
Limit: limit,
Offset: offset,
Name: r.URL.Query().Get("client_name"),
Owner: r.URL.Query().Get("owner"),
}

c, err := h.r.ClientManager().GetClients(r.Context(), limit, offset)
c, err := h.r.ClientManager().GetClients(r.Context(), filters)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
Expand Down
2 changes: 1 addition & 1 deletion client/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Storage interface {

DeleteClient(ctx context.Context, id string) error

GetClients(ctx context.Context, limit, offset int) ([]Client, error)
GetClients(ctx context.Context, filters Filter) ([]Client, error)

CountClients(ctx context.Context) (int, error)

Expand Down
25 changes: 21 additions & 4 deletions client/manager_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestHelperCreateGetUpdateDeleteClient(k string, m Storage) func(t *testing.

assert.NoError(t, m.CreateClient(ctx, &Client{
OutfacingID: "2-1234",
Name: "name",
Name: "name2",
Secret: "secret",
RedirectURIs: []string{"http://redirect"},
TermsOfServiceURI: "foo",
Expand All @@ -141,7 +141,7 @@ func TestHelperCreateGetUpdateDeleteClient(k string, m Storage) func(t *testing.

compare(t, c, d, k)

ds, err := m.GetClients(ctx, 100, 0)
ds, err := m.GetClients(ctx, Filter{Limit: 100, Offset: 0})
assert.NoError(t, err)
assert.Len(t, ds, 2)
assert.NotEqual(t, ds[0].OutfacingID, ds[1].OutfacingID)
Expand All @@ -150,13 +150,30 @@ func TestHelperCreateGetUpdateDeleteClient(k string, m Storage) func(t *testing.
assert.Equal(t, ds[0].SecretExpiresAt, 0)
assert.Equal(t, ds[1].SecretExpiresAt, 1)

ds, err = m.GetClients(ctx, 1, 0)
ds, err = m.GetClients(ctx, Filter{Limit: 1, Offset: 0})
assert.NoError(t, err)
assert.Len(t, ds, 1)

ds, err = m.GetClients(ctx, 100, 100)
ds, err = m.GetClients(ctx, Filter{Limit: 100, Offset: 100})
assert.NoError(t, err)

// get by name
ds, err = m.GetClients(ctx, Filter{Limit: 100, Offset: 0, Name: "name"})
assert.NoError(t, err)
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].Name, "name")

// get by name not exist
ds, err = m.GetClients(ctx, Filter{Limit: 100, Offset: 0, Name: "bad name"})
assert.NoError(t, err)
assert.Len(t, ds, 0)

// get by owner
ds, err = m.GetClients(ctx, Filter{Limit: 100, Offset: 0, Owner: "aeneas"})
assert.NoError(t, err)
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].Owner, "aeneas")

err = m.UpdateClient(ctx, &Client{
OutfacingID: "2-1234",
Name: "name-new",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ replace github.com/dgrijalva/jwt-go => github.com/form3tech-oss/jwt-go v3.2.1+in

replace github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2

replace github.com/gobuffalo/packr => github.com/gobuffalo/packr v1.30.1
replace github.com/oleiade/reflections => github.com/oleiade/reflections v1.0.1

require (
Expand Down Expand Up @@ -53,7 +54,6 @@ require (
github.com/sawadashota/encrypta v0.0.2
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518 // indirect
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.7.1
github.com/toqueteos/webbrowser v1.2.0
Expand Down
Loading

0 comments on commit ea6fdfd

Please sign in to comment.