diff --git a/UPGRADE.md b/UPGRADE.md index d474008130..53790a67bc 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -359,6 +359,13 @@ The following methods have been moved. - `GetLoginRequest(challenge string) (*swagger.LoginRequest, *swagger.APIResponse, error)` - `GetConsentRequest(challenge string) (*swagger.ConsentRequest, *swagger.APIResponse, error)` +Additionally, the following methods have been removed as they were of very little use and also mixed the Client Credentials +flow with the Authorize Code Flow which lead to weird usage. It's much easier to configure `clientcredentials.Config` or +`oauth2.Config` yourself. + +* `GetOAuth2ClientConfig() (*clientcredentials.Config)` +* `GetOAuth2Config() (*oauth2.Config)` + ### Improvements #### Unknown request body payloads result in error diff --git a/sdk/go/hydra/sdk.go b/sdk/go/hydra/sdk.go index b75a7b457c..ff6782d452 100644 --- a/sdk/go/hydra/sdk.go +++ b/sdk/go/hydra/sdk.go @@ -22,6 +22,7 @@ package hydra import ( "context" + "strings" "github.com/ory/hydra/sdk/go/hydra/swagger" "github.com/pkg/errors" @@ -55,68 +56,39 @@ type Configuration struct { Scopes []string } -func removeTrailingSlash(path string) string { - for len(path) > 0 && path[len(path)-1] == '/' { - path = path[0 : len(path)-1] - } - return path -} - -func (s *CodeGenSDK) GetOAuth2ClientConfig() *clientcredentials.Config { - return s.oAuth2ClientConfig -} -func (s *CodeGenSDK) GetOAuth2Config() *oauth2.Config { - return s.oAuth2Config -} - // NewSDK instantiates a new CodeGenSDK instance or returns an error. func NewSDK(c *Configuration) (*CodeGenSDK, error) { if c.EndpointURL == "" { return nil, errors.New("Please specify the ORY Hydra Endpoint URL") } - if c.ClientSecret == "" { - return nil, errors.New("Please specify an OAuth 2.0 Client Secret") - } - if c.ClientID == "" { - return nil, errors.New("Please specify an OAuth 2.0 Client ID") - } - if len(c.Scopes) == 0 { - c.Scopes = []string{} - } - - c.EndpointURL = removeTrailingSlash(c.EndpointURL) - oAuth2Config := &oauth2.Config{ - ClientSecret: c.ClientSecret, - ClientID: c.ClientID, - Scopes: c.Scopes, - Endpoint: oauth2.Endpoint{ - TokenURL: c.EndpointURL + "/oauth2/token", - AuthURL: c.EndpointURL + "/oauth2/auth", - }, - } - - oAuth2ClientConfig := &clientcredentials.Config{ - ClientSecret: c.ClientSecret, - ClientID: c.ClientID, - Scopes: c.Scopes, - TokenURL: c.EndpointURL + "/oauth2/token", - } - oAuth2Client := oAuth2ClientConfig.Client(context.Background()) + c.EndpointURL = strings.TrimLeft(c.EndpointURL, "/") o := swagger.NewOAuth2ApiWithBasePath(c.EndpointURL) - o.Configuration.Transport = oAuth2Client.Transport - o.Configuration.Username = c.ClientID - o.Configuration.Password = c.ClientSecret - j := swagger.NewJsonWebKeyApiWithBasePath(c.EndpointURL) - j.Configuration.Transport = oAuth2Client.Transport - sdk := &CodeGenSDK{ - OAuth2Api: o, - JsonWebKeyApi: j, - Configuration: c, - oAuth2ClientConfig: oAuth2ClientConfig, - oAuth2Config: oAuth2Config, + OAuth2Api: o, + JsonWebKeyApi: j, + Configuration: c, + } + + if c.ClientSecret != "" && c.ClientID != "" { + if len(c.Scopes) == 0 { + c.Scopes = []string{} + } + + oAuth2ClientConfig := &clientcredentials.Config{ + ClientSecret: c.ClientSecret, + ClientID: c.ClientID, + Scopes: c.Scopes, + TokenURL: c.EndpointURL + "/oauth2/token", + } + oAuth2Client := oAuth2ClientConfig.Client(context.Background()) + o.Configuration.Transport = oAuth2Client.Transport + o.Configuration.Username = c.ClientID + o.Configuration.Password = c.ClientSecret + j.Configuration.Transport = oAuth2Client.Transport + + sdk.oAuth2ClientConfig = oAuth2ClientConfig } return sdk, nil diff --git a/sdk/go/hydra/sdk_api.go b/sdk/go/hydra/sdk_api.go index 9af379679c..bc99c3721a 100644 --- a/sdk/go/hydra/sdk_api.go +++ b/sdk/go/hydra/sdk_api.go @@ -22,15 +22,10 @@ package hydra import ( "github.com/ory/hydra/sdk/go/hydra/swagger" - "golang.org/x/oauth2" - "golang.org/x/oauth2/clientcredentials" ) // SDK helps developers interact with ORY Hydra using a Go API. type SDK interface { - GetOAuth2ClientConfig() *clientcredentials.Config - GetOAuth2Config() *oauth2.Config - JWKApi OAuth2API } diff --git a/sdk/go/hydra/sdk_test.go b/sdk/go/hydra/sdk_test.go index 1274579b86..95139c8260 100644 --- a/sdk/go/hydra/sdk_test.go +++ b/sdk/go/hydra/sdk_test.go @@ -27,38 +27,15 @@ import ( "github.com/stretchr/testify/assert" ) -func TestInterface(t *testing.T) { - var sdk SDK - var err error - sdk, err = NewSDK(&Configuration{ - EndpointURL: "http://localhost:4444/", - ClientID: "foo", - ClientSecret: "bar", - }) - assert.NoError(t, err) - assert.NotNil(t, sdk) -} - func TestErrorHandlers(t *testing.T) { for k, c := range []Configuration{ - { - EndpointURL: "http://localhost:4444/", - ClientSecret: "bar", - Scopes: []string{"foo"}, - }, - { - EndpointURL: "http://localhost:4444/", - ClientID: "bar", - Scopes: []string{"foo"}, - }, - { - ClientID: "foo", - ClientSecret: "bar", - Scopes: []string{"foo"}, - }, + {}, } { t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { - sdk, err := NewSDK(&c) + var sdk SDK + var err error + + sdk, err = NewSDK(&c) assert.Error(t, err) assert.Nil(t, sdk) })