Skip to content

Commit

Permalink
add include_credentials query param to /admin/identities list call
Browse files Browse the repository at this point in the history
  • Loading branch information
borisroman committed Jun 28, 2023
1 parent 1e65662 commit 1e1f74e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
15 changes: 14 additions & 1 deletion identity/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"io"
"net/http"
"strconv"
"time"

"github.com/ory/x/pagination/migrationpagination"
Expand Down Expand Up @@ -137,6 +138,13 @@ type listIdentitiesParameters struct {
// required: false
// in: query
CredentialsIdentifier string `json:"credentials_identifier"`

// IncludeCredentials when set to true includes all of the identity's credentials.
//
// default: false
// required: false
// in: query
IncludeCredentials bool `json:"include_credentials"`
}

// swagger:route GET /admin/identities identity listIdentities
Expand All @@ -159,8 +167,13 @@ type listIdentitiesParameters struct {
func (h *Handler) list(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
page, itemsPerPage := x.ParsePagination(r)

includeCredentials := false
if param, err := strconv.ParseBool(r.URL.Query().Get("include_credentials")); err == nil {
includeCredentials = param
}

params := ListIdentityParameters{Expand: ExpandDefault, Page: page, PerPage: itemsPerPage, CredentialsIdentifier: r.URL.Query().Get("credentials_identifier")}
if params.CredentialsIdentifier != "" {
if includeCredentials || params.CredentialsIdentifier != "" {
params.Expand = ExpandEverything
}

Expand Down
26 changes: 25 additions & 1 deletion identity/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ func TestHandler(t *testing.T) {
})

t.Run("case=should list all identities", func(t *testing.T) {
for name, ts := range map[string]*httptest.Server{"public": publicTS, "admin": adminTS} {
for name, ts := range map[string]*httptest.Server{"admin": adminTS} {
t.Run("endpoint="+name, func(t *testing.T) {
res := get(t, ts, "/identities", http.StatusOK)
assert.False(t, res.Get("0.credentials").Exists(), "credentials config should be omitted: %s", res.Raw)
Expand All @@ -1224,6 +1224,30 @@ func TestHandler(t *testing.T) {
}
})

t.Run("case=should list all identities without credentials", func(t *testing.T) {
for name, ts := range map[string]*httptest.Server{"admin": adminTS} {
t.Run("endpoint="+name, func(t *testing.T) {
res := get(t, ts, "/identities?include_credentials=false", http.StatusOK)
assert.False(t, res.Get("0.credentials").Exists(), "credentials config should be omitted: %s", res.Raw)
assert.True(t, res.Get("0.metadata_public").Exists(), "metadata_public config should be included: %s", res.Raw)
assert.True(t, res.Get("0.metadata_admin").Exists(), "metadata_admin config should be included: %s", res.Raw)
assert.EqualValues(t, "baz", res.Get(`#(traits.bar=="baz").traits.bar`).String(), "%s", res.Raw)
})
}
})

t.Run("case=should list all identities with credentials", func(t *testing.T) {
for name, ts := range map[string]*httptest.Server{"admin": adminTS} {
t.Run("endpoint="+name, func(t *testing.T) {
res := get(t, ts, "/identities?include_credentials=true", http.StatusOK)
assert.True(t, res.Get("0.credentials").Exists(), "credentials config should be included: %s", res.Raw)
assert.True(t, res.Get("0.metadata_public").Exists(), "metadata_public config should be included: %s", res.Raw)
assert.True(t, res.Get("0.metadata_admin").Exists(), "metadata_admin config should be included: %s", res.Raw)
assert.EqualValues(t, "baz", res.Get(`#(traits.bar=="baz").traits.bar`).String(), "%s", res.Raw)
})
}
})

t.Run("case=should not be able to update an identity that does not exist yet", func(t *testing.T) {
for name, ts := range map[string]*httptest.Server{"public": publicTS, "admin": adminTS} {
t.Run("endpoint="+name, func(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions internal/client-go/api_identity.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/httpclient/api_identity.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions spec/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -3296,6 +3296,15 @@
"schema": {
"type": "string"
}
},
{
"description": "IncludeCredentials when set to true includes all of the identity's credentials.",
"in": "query",
"name": "include_credentials",
"schema": {
"default": false,
"type": "boolean"
}
}
],
"responses": {
Expand Down
7 changes: 7 additions & 0 deletions spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@
"description": "CredentialsIdentifier is the identifier (username, email) of the credentials to look up.",
"name": "credentials_identifier",
"in": "query"
},
{
"type": "boolean",
"default": false,
"description": "IncludeCredentials when set to true includes all of the identity's credentials.",
"name": "include_credentials",
"in": "query"
}
],
"responses": {
Expand Down

0 comments on commit 1e1f74e

Please sign in to comment.