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

fix: patch should not reset client secret #2872

Merged
merged 3 commits into from
Nov 26, 2021
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
10 changes: 10 additions & 0 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,21 @@ func (h *Handler) Patch(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
return
}

oldSecret := c.Secret

if err := x.ApplyJSONPatch(patchJSON, c, "/id"); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}

// fix for #2869
// GetConcreteClient returns a client with the hashed secret, however updateClient expects
// an empty secret if the secret hasn't changed. As such we need to check if the patch has
// updated the secret or not
if oldSecret == c.Secret {
c.Secret = ""
}

if err := h.updateClient(r.Context(), c); err != nil {
h.r.Writer().WriteError(w, r, err)
return
Expand Down
19 changes: 19 additions & 0 deletions client/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,23 @@ func TestClientSDK(t *testing.T) {
_, err = c.Admin.PatchOAuth2Client(admin.NewPatchOAuth2ClientParams().WithID(client.ClientID).WithBody(models.PatchRequest{{Op: &op, Path: &path, Value: value}}))
require.Error(t, err)
})

t.Run("case=patch should not alter secret if not requested", func(t *testing.T) {
op := "replace"
path := "/client_uri"
value := "http://foo.bar"

client := createTestClient("")
client.ClientID = "patch3_client"
_, err := c.Admin.CreateOAuth2Client(admin.NewCreateOAuth2ClientParams().WithBody(client))
require.NoError(t, err)

result1, err := c.Admin.PatchOAuth2Client(admin.NewPatchOAuth2ClientParams().WithID(client.ClientID).WithBody(models.PatchRequest{{Op: &op, Path: &path, Value: value}}))
require.NoError(t, err)
result2, err := c.Admin.PatchOAuth2Client(admin.NewPatchOAuth2ClientParams().WithID(client.ClientID).WithBody(models.PatchRequest{{Op: &op, Path: &path, Value: value}}))
require.NoError(t, err)

// secret hashes shouldn't change between these PUT calls
require.Equal(t, result1.Payload.ClientSecret, result2.Payload.ClientSecret)
})
}