Skip to content

Commit

Permalink
refactor(sdk): consent SDK
Browse files Browse the repository at this point in the history
BREAKING CHANGE: SDK naming has changed for the following operations:

```patch
ory.
-   V0alpha2Api.AdminRevokeOAuth2ConsentSessions(cmd.Context()).
+   OAuth2Api.RevokeOAuth2ConsentSessions(context.Background()).
    Client(clientId).Execute()

ory.
-   V0alpha2Api.AdminListOAuth2SubjectConsentSessions(cmd.Context(), id).
+   OAuth2Api.RevokeOAuth2ConsentSessions(context.Background()).
    Client(clientId).Execute()

ory.
-   V0alpha2Api.AdminListOAuth2SubjectConsentSessions(context.Background()).
+   OAuth2Api.ListOAuth2ConsentSessions(context.Background()).
    Subject(subjectId).Execute()

ory.
-   V0alpha2Api.AdminRevokeOAuth2LoginSessions(context.Background()).
+   OAuth2Api.RevokeOAuth2LoginSessions(context.Background()).
    Subject(subjectId).Execute()

ory.
-   V0alpha2Api.AdminGetOAuth2LoginRequest(context.Background()).
+   OAuth2Api.GetOAuth2LoginRequest(context.Background()).
    LoginChallenge(challenge).Execute()

ory.
-   V0alpha2Api.AdminAcceptOAuth2LoginRequest(context.Background()).
+   OAuth2Api.AcceptOAuth2LoginRequest(context.Background()).
    AcceptOAuth2LoginRequest(body).
    LoginChallenge(challenge).Execute()

ory.
-   V0alpha2Api.AdminRejectOAuth2LoginRequest(context.Background()).
+   OAuth2Api.RejectOAuth2LoginRequest(context.Background()).
    RejectOAuth2Request(body).
    LoginChallenge(challenge).Execute()

ory.
-   V0alpha2Api.AdminGetOAuth2ConsentRequest(context.Background()).
+   OAuth2Api.GetOAuth2ConsentRequest(context.Background()).
    ConsentChallenge(challenge).Execute()

ory.
-   V0alpha2Api.AdminAcceptOAuth2ConsentRequest(context.Background()).
+   OAuth2Api.AcceptOAuth2ConsentRequest(context.Background()).
    AcceptOAuth2ConsentRequest(body).
    ConsentChallenge(challenge).Execute()

ory.
-   V0alpha2Api.AdminRejectOAuth2ConsentRequest(context.Background()).
+   OAuth2Api.RejectOAuth2ConsentRequest(context.Background()).
    RejectOAuth2Request().
    ConsentChallenge(challenge).Execute()

ory.
-   V0alpha2Api.AdminAcceptOAuth2LogoutRequest(context.Background()).
+   OAuth2Api.AcceptOAuth2LogoutRequest(context.Background()).
    LogoutChallenge(challenge).
    Execute()

ory.
-   V0alpha2Api.AdminRejectOAuth2LogoutRequest(context.Background()).
+   OAuth2Api.RejectOAuth2LogoutRequest(context.Background()).
    LogoutChallenge(challenge).
    Execute()

ory.
    V0alpha2Api.AdminGetOAuth2LogoutRequest(context.Background()).
+   OAuth2Api.GetOAuth2LogoutRequest(context.Background()).
    LogoutChallenge(challenge).
    Execute()

- var AlreadyHandledError HandledOAuth2LoginRequest
+ var AlreadyHandledError ErrorOAuth2LoginRequestAlreadyHandled

- var AlreadyHandledError HandledOAuth2LoginRequest
+ var AlreadyHandledError ErrorOAuth2ConsentRequestAlreadyHandled

- var OAuth2SuccessResponse SuccessfulOAuth2RequestResponse
+ var OAuth2SuccessResponse OAuth2RedirectTo
```
  • Loading branch information
aeneasr committed Oct 11, 2022
1 parent 6b60156 commit e800002
Show file tree
Hide file tree
Showing 45 changed files with 5,101 additions and 6,542 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7188,7 +7188,7 @@ Signed-off-by: Michael DeRazon <[email protected]> Signed-off-by: arekkas
- Add flags for newly introduced oidc client settings
([c4b902d](https://github.com/ory/hydra/commit/c4b902d8f86fa4ef03704fc16d17e921e5710e61)),
closes [#938](https://github.com/ory/hydra/issues/938)
- Add ListUserConsentSessions to OAuth2API interface
- Add ListUserConsentSessions to.OAuth2Api.interface
([#977](https://github.com/ory/hydra/issues/977))
([1bd8ab7](https://github.com/ory/hydra/commit/1bd8ab7d6bfe224e33f700959416b5c5e726bdbc))
- Add logout api endpoint ([#984](https://github.com/ory/hydra/issues/984))
Expand Down
4 changes: 2 additions & 2 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,13 @@ This has different impacts for the different code-generated client libraries.
##### Go

If you use the `hydra.SDK` interface only and the `hydra.NewSDK()` factory,
everything will work as before. If you rely on e.g. `hydra.NewOAuth2Api()`, you
everything will work as before. If you rely on e.g. `hydra.Ne.OAuth2Api.)`, you
will be affected by this change.

##### Others

All method signatures stayed the same, but the factory names for instantiating
the SDK client have changed. For example, `hydra.NewOAuth2Api()` is now
the SDK client have changed. For example, `hydra.Ne.OAuth2Api.)` is now
`hydra.NewAdminApi()` and `hydra.NewPublicApi()` - depending on which endpoints
you need to interact with.

Expand Down
14 changes: 7 additions & 7 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type createOAuth2Client struct {
Body Client
}

// swagger:route POST /admin/clients oauth2 createOAuth2Client
// swagger:route POST /admin/clients oAuth2 createOAuth2Client
//
// # Create OAuth 2.0 Client
//
Expand Down Expand Up @@ -245,7 +245,7 @@ type setOAuth2Client struct {
Body Client
}

// swagger:route PUT /admin/clients/{id} oauth2 setOAuth2Client
// swagger:route PUT /admin/clients/{id} oAuth2 setOAuth2Client
//
// # Set OAuth 2.0 Client
//
Expand Down Expand Up @@ -414,7 +414,7 @@ type patchOAuth2Client struct {
Body openapix.JSONPatchDocument
}

// swagger:route PATCH /admin/clients/{id} oauth2 patchOAuth2Client
// swagger:route PATCH /admin/clients/{id} oAuth2 patchOAuth2Client
//
// # Patch OAuth 2.0 Client
//
Expand Down Expand Up @@ -503,7 +503,7 @@ type listOAuth2ClientsParameters struct {
Owner string `json:"owner"`
}

// swagger:route GET /admin/clients oauth2 listOAuth2Clients
// swagger:route GET /admin/clients oAuth2 listOAuth2Clients
//
// # List OAuth 2.0 Clients
//
Expand Down Expand Up @@ -565,7 +565,7 @@ type adminGetOAuth2Client struct {
ID string `json:"id"`
}

// swagger:route GET /admin/clients/{id} oauth2 getOAuth2Client
// swagger:route GET /admin/clients/{id} oAuth2 getOAuth2Client
//
// # Get an OAuth 2.0 Client
//
Expand Down Expand Up @@ -669,7 +669,7 @@ type deleteOAuth2Client struct {
ID string `json:"id"`
}

// swagger:route DELETE /admin/clients/{id} oauth2 deleteOAuth2Client
// swagger:route DELETE /admin/clients/{id} oAuth2 deleteOAuth2Client
//
// # Delete OAuth 2.0 Client
//
Expand Down Expand Up @@ -715,7 +715,7 @@ type setOAuth2ClientLifespans struct {
Body Lifespans
}

// swagger:route PUT /admin/clients/{id}/lifespans oauth2 setOAuth2ClientLifespans
// swagger:route PUT /admin/clients/{id}/lifespans oAuth2 setOAuth2ClientLifespans
//
// # Set OAuth2 Client Token Lifespans
//
Expand Down
42 changes: 21 additions & 21 deletions client/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ func TestClientSDK(t *testing.T) {
c.GetConfig().Servers = hydra.ServerConfigurations{{URL: server.URL}}

t.Run("case=client default scopes are set", func(t *testing.T) {
result, _, err := c.Oauth2Api.CreateOAuth2Client(ctx).OAuth2Client(hydra.OAuth2Client{}).Execute()
result, _, err := c.OAuth2Api.CreateOAuth2Client(ctx).OAuth2Client(hydra.OAuth2Client{}).Execute()
require.NoError(t, err)
assert.EqualValues(t, conf.DefaultClientScope(ctx), strings.Split(*result.Scope, " "))

_, err = c.Oauth2Api.DeleteOAuth2Client(ctx, *result.ClientId).Execute()
_, err = c.OAuth2Api.DeleteOAuth2Client(ctx, *result.ClientId).Execute()
require.NoError(t, err)
})

Expand All @@ -115,7 +115,7 @@ func TestClientSDK(t *testing.T) {
// createClient.SecretExpiresAt = 10

// returned client is correct on Create
result, _, err := c.Oauth2Api.CreateOAuth2Client(ctx).OAuth2Client(createClient).Execute()
result, _, err := c.OAuth2Api.CreateOAuth2Client(ctx).OAuth2Client(createClient).Execute()
require.NoError(t, err)
assert.NotEmpty(t, result.UpdatedAt)
assert.NotEmpty(t, result.CreatedAt)
Expand All @@ -129,31 +129,31 @@ func TestClientSDK(t *testing.T) {

// secret is not returned on GetOAuth2Client
compareClient.ClientSecret = x.ToPointer("")
gresult, _, err := c.Oauth2Api.GetOAuth2Client(context.Background(), *createClient.ClientId).Execute()
gresult, _, err := c.OAuth2Api.GetOAuth2Client(context.Background(), *createClient.ClientId).Execute()
require.NoError(t, err)
assertx.EqualAsJSONExcept(t, compareClient, gresult, append(defaultIgnoreFields, "client_secret"))

// get client will return The request could not be authorized
gresult, _, err = c.Oauth2Api.GetOAuth2Client(context.Background(), "unknown").Execute()
gresult, _, err = c.OAuth2Api.GetOAuth2Client(context.Background(), "unknown").Execute()
require.Error(t, err)
assert.Empty(t, gresult)
assert.True(t, strings.Contains(err.Error(), "404"), err.Error())

// listing clients returns the only added one
results, _, err := c.Oauth2Api.ListOAuth2Clients(context.Background()).PageSize(100).Execute()
results, _, err := c.OAuth2Api.ListOAuth2Clients(context.Background()).PageSize(100).Execute()
require.NoError(t, err)
assert.Len(t, results, 1)
assertx.EqualAsJSONExcept(t, compareClient, results[0], append(defaultIgnoreFields, "client_secret"))

// SecretExpiresAt gets overwritten with 0 on Update
compareClient.ClientSecret = createClient.ClientSecret
uresult, _, err := c.Oauth2Api.SetOAuth2Client(context.Background(), *createClient.ClientId).OAuth2Client(createClient).Execute()
uresult, _, err := c.OAuth2Api.SetOAuth2Client(context.Background(), *createClient.ClientId).OAuth2Client(createClient).Execute()
require.NoError(t, err)
assertx.EqualAsJSONExcept(t, compareClient, uresult, append(defaultIgnoreFields, "client_secret"))

// create another client
updateClient := createTestClient("foo")
uresult, _, err = c.Oauth2Api.SetOAuth2Client(context.Background(), *createClient.ClientId).OAuth2Client(updateClient).Execute()
uresult, _, err = c.OAuth2Api.SetOAuth2Client(context.Background(), *createClient.ClientId).OAuth2Client(updateClient).Execute()
require.NoError(t, err)
assert.NotEqual(t, updateClient.ClientId, uresult.ClientId)
updateClient.ClientId = uresult.ClientId
Expand All @@ -162,34 +162,34 @@ func TestClientSDK(t *testing.T) {
// again, test if secret is not returned on Get
compareClient = updateClient
compareClient.ClientSecret = x.ToPointer("")
gresult, _, err = c.Oauth2Api.GetOAuth2Client(context.Background(), *updateClient.ClientId).Execute()
gresult, _, err = c.OAuth2Api.GetOAuth2Client(context.Background(), *updateClient.ClientId).Execute()
require.NoError(t, err)
assertx.EqualAsJSONExcept(t, compareClient, gresult, append(defaultIgnoreFields, "client_secret"))

// client can not be found after being deleted
_, err = c.Oauth2Api.DeleteOAuth2Client(context.Background(), *updateClient.ClientId).Execute()
_, err = c.OAuth2Api.DeleteOAuth2Client(context.Background(), *updateClient.ClientId).Execute()
require.NoError(t, err)

_, _, err = c.Oauth2Api.GetOAuth2Client(context.Background(), *updateClient.ClientId).Execute()
_, _, err = c.OAuth2Api.GetOAuth2Client(context.Background(), *updateClient.ClientId).Execute()
require.Error(t, err)
})

t.Run("case=public client is transmitted without secret", func(t *testing.T) {
result, _, err := c.Oauth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(hydra.OAuth2Client{
result, _, err := c.OAuth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(hydra.OAuth2Client{
TokenEndpointAuthMethod: x.ToPointer("none"),
}).Execute()
require.NoError(t, err)

assert.Equal(t, "", x.FromPointer[string](result.ClientSecret))

result, _, err = c.Oauth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(createTestClient("")).Execute()
result, _, err = c.OAuth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(createTestClient("")).Execute()
require.NoError(t, err)

assert.Equal(t, "secret", x.FromPointer[string](result.ClientSecret))
})

t.Run("case=id can not be set", func(t *testing.T) {
_, res, err := c.Oauth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(hydra.OAuth2Client{ClientId: x.ToPointer(uuidx.NewV4().String())}).Execute()
_, res, err := c.OAuth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(hydra.OAuth2Client{ClientId: x.ToPointer(uuidx.NewV4().String())}).Execute()
require.Error(t, err)
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
Expand All @@ -202,14 +202,14 @@ func TestClientSDK(t *testing.T) {
value := "http://foo.bar"

client := createTestClient("")
created, _, err := c.Oauth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(client).Execute()
created, _, err := c.OAuth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(client).Execute()
require.NoError(t, err)
client.ClientId = created.ClientId

expected := deepcopy.Copy(client).(hydra.OAuth2Client)
expected.RedirectUris = append(expected.RedirectUris, value)

result, _, err := c.Oauth2Api.PatchOAuth2Client(context.Background(), *client.ClientId).JsonPatch([]hydra.JsonPatch{{Op: op, Path: path, Value: value}}).Execute()
result, _, err := c.OAuth2Api.PatchOAuth2Client(context.Background(), *client.ClientId).JsonPatch([]hydra.JsonPatch{{Op: op, Path: path, Value: value}}).Execute()
require.NoError(t, err)
expected.CreatedAt = result.CreatedAt
expected.UpdatedAt = result.UpdatedAt
Expand All @@ -224,11 +224,11 @@ func TestClientSDK(t *testing.T) {
value := "foo"

client := createTestClient("")
created, res, err := c.Oauth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(client).Execute()
created, res, err := c.OAuth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(client).Execute()
require.NoError(t, err, "%s", ioutilx.MustReadAll(res.Body))
client.ClientId = created.ClientId

_, _, err = c.Oauth2Api.PatchOAuth2Client(context.Background(), *client.ClientId).JsonPatch([]hydra.JsonPatch{{Op: op, Path: path, Value: value}}).Execute()
_, _, err = c.OAuth2Api.PatchOAuth2Client(context.Background(), *client.ClientId).JsonPatch([]hydra.JsonPatch{{Op: op, Path: path, Value: value}}).Execute()
require.Error(t, err)
})

Expand All @@ -238,13 +238,13 @@ func TestClientSDK(t *testing.T) {
value := "http://foo.bar"

client := createTestClient("")
created, _, err := c.Oauth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(client).Execute()
created, _, err := c.OAuth2Api.CreateOAuth2Client(context.Background()).OAuth2Client(client).Execute()
require.NoError(t, err)
client.ClientId = created.ClientId

result1, _, err := c.Oauth2Api.PatchOAuth2Client(context.Background(), *client.ClientId).JsonPatch([]hydra.JsonPatch{{Op: op, Path: path, Value: value}}).Execute()
result1, _, err := c.OAuth2Api.PatchOAuth2Client(context.Background(), *client.ClientId).JsonPatch([]hydra.JsonPatch{{Op: op, Path: path, Value: value}}).Execute()
require.NoError(t, err)
result2, _, err := c.Oauth2Api.PatchOAuth2Client(context.Background(), *client.ClientId).JsonPatch([]hydra.JsonPatch{{Op: op, Path: path, Value: value}}).Execute()
result2, _, err := c.OAuth2Api.PatchOAuth2Client(context.Background(), *client.ClientId).JsonPatch([]hydra.JsonPatch{{Op: op, Path: path, Value: value}}).Execute()
require.NoError(t, err)

// secret hashes shouldn't change between these PUT calls
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_create_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ To encrypt an auto-generated OAuth2 Client Secret, use flags ` + "`--pgp-key`" +

secret := flagx.MustGetString(cmd, flagClientSecret)
//nolint:bodyclose
client, _, err := m.Oauth2Api.CreateOAuth2Client(cmd.Context()).OAuth2Client(clientFromFlags(cmd)).Execute()
client, _, err := m.OAuth2Api.CreateOAuth2Client(cmd.Context()).OAuth2Client(clientFromFlags(cmd)).Execute()
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_delete_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ To delete OAuth 2.0 Clients with the owner of "[email protected]", run:
)

for _, c := range args {
_, err := m.Oauth2Api.DeleteOAuth2Client(cmd.Context(), c).Execute() //nolint:bodyclose
_, err := m.OAuth2Api.DeleteOAuth2Client(cmd.Context(), c).Execute() //nolint:bodyclose
if err != nil {
failed[c] = cmdx.PrintOpenAPIError(cmd, err)
continue
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_get_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewGetClientsCmd() *cobra.Command {

clients := make([]hydra.OAuth2Client, 0, len(args))
for _, id := range args {
client, _, err := m.Oauth2Api.GetOAuth2Client(cmd.Context(), id).Execute() //nolint:bodyclose
client, _, err := m.OAuth2Api.GetOAuth2Client(cmd.Context(), id).Execute() //nolint:bodyclose
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_import_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Please be aware that this command does not update existing clients. If the clien

for src, cc := range clients {
for _, c := range cc {
result, _, err := m.Oauth2Api.CreateOAuth2Client(cmd.Context()).OAuth2Client(c).Execute() //nolint:bodyclose
result, _, err := m.OAuth2Api.CreateOAuth2Client(cmd.Context()).OAuth2Client(c).Execute() //nolint:bodyclose
if err != nil {
failed[src] = cmdx.PrintOpenAPIError(cmd, err)
continue
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_list_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewListClientsCmd() *cobra.Command {
return err
}

list, resp, err := m.Oauth2Api.ListOAuth2Clients(cmd.Context()).PageSize(int64(pageSize)).PageToken(pageToken).Execute()
list, resp, err := m.OAuth2Api.ListOAuth2Clients(cmd.Context()).PageSize(int64(pageSize)).PageToken(pageToken).Execute()
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ To encrypt an auto-generated OAuth2 Client Secret, use flags ` + "`--pgp-key`" +
id := args[0]
cc := clientFromFlags(cmd)

client, _, err := m.Oauth2Api.SetOAuth2Client(context.Background(), id).OAuth2Client(cc).Execute() //nolint:bodyclose
client, _, err := m.OAuth2Api.SetOAuth2Client(context.Background(), id).OAuth2Client(cc).Execute() //nolint:bodyclose
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}
Expand Down
Loading

0 comments on commit e800002

Please sign in to comment.