Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: Improve handling of legacy id field #927

Merged
merged 1 commit into from
Jul 15, 2018
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: 3 additions & 3 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@

[[constraint]]
name = "github.com/ory/go-convenience"
version = "0.0.2"
version = "0.0.4"

[[constraint]]
branch = "master"
Expand Down
1 change: 1 addition & 0 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request, ps httprouter.P
}

c.ID = ps.ByName("id")
c.ClientID = ps.ByName("id")
if err := h.Validator.Validate(&c); err != nil {
h.H.WriteError(w, r, err)
return
Expand Down
2 changes: 1 addition & 1 deletion client/manager_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (m *MemoryManager) GetConcreteClient(id string) (*Client, error) {
defer m.RUnlock()

for _, c := range m.Clients {
if c.GetID() == id {
if c.ID == id {
c.ClientID = c.ID
return &c, nil
}
Expand Down
44 changes: 44 additions & 0 deletions client/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package client_test

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -152,4 +153,47 @@ func TestClientSDK(t *testing.T) {

assert.Equal(t, "secret", result.ClientSecret)
})

t.Run("case=id should be set properly", func(t *testing.T) {
for k, tc := range []struct {
client hydra.OAuth2Client
expectID string
}{
{
client: hydra.OAuth2Client{},
},
{
client: hydra.OAuth2Client{Id: "set-properly-1"},
expectID: "set-properly-1",
},
{
client: hydra.OAuth2Client{ClientId: "set-properly-2"},
expectID: "set-properly-2",
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
result, response, err := c.CreateOAuth2Client(tc.client)
require.NoError(t, err)
require.EqualValues(t, http.StatusCreated, response.StatusCode, "%s", response.Payload)

assert.NotEmpty(t, result.Id)
assert.NotEmpty(t, result.ClientId)
assert.EqualValues(t, result.Id, result.ClientId)

id := result.Id
if tc.expectID != "" {
assert.EqualValues(t, tc.expectID, result.Id)
assert.EqualValues(t, tc.expectID, result.ClientId)
id = tc.expectID
}

result, response, err = c.GetOAuth2Client(id)
require.NoError(t, err)
require.EqualValues(t, http.StatusOK, response.StatusCode, "%s", response.Payload)

assert.EqualValues(t, id, result.Id)
assert.EqualValues(t, id, result.ClientId)
})
}
})
}
13 changes: 5 additions & 8 deletions client/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/ory/fosite"
"github.com/ory/go-convenience/stringslice"
"github.com/ory/go-convenience/stringsx"
"github.com/pborman/uuid"
"github.com/pkg/errors"
)
Expand All @@ -47,14 +48,10 @@ func NewValidator(
}

func (v *Validator) Validate(c *Client) error {
if c.ID == "" && c.ClientID == "" {
c.ID = uuid.New()
c.ClientID = c.ID
} else if c.ID == "" && c.ClientID != "" {
c.ID = c.ClientID
} else if c.ID != "" && c.ClientID == "" {
c.ClientID = c.ID
} else if c.ID != c.ClientID {
id := uuid.New()
c.ID = stringsx.Coalesce(c.ID, c.ClientID, id)
c.ClientID = stringsx.Coalesce(c.ClientID, c.ID, id)
if c.ID != c.ClientID {
return errors.WithStack(fosite.ErrInvalidRequest.WithHint("Field id and client_id must match."))
}

Expand Down