Skip to content

Commit

Permalink
sdk: Remove the need for OAuth2 credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas committed May 21, 2018
1 parent ce8cc73 commit 0a25263
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 86 deletions.
7 changes: 7 additions & 0 deletions UPGRADE.md
Expand Up @@ -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
Expand Down
78 changes: 25 additions & 53 deletions sdk/go/hydra/sdk.go
Expand Up @@ -22,6 +22,7 @@ package hydra

import (
"context"
"strings"

"github.com/ory/hydra/sdk/go/hydra/swagger"
"github.com/pkg/errors"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions sdk/go/hydra/sdk_api.go
Expand Up @@ -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
}
Expand Down
33 changes: 5 additions & 28 deletions sdk/go/hydra/sdk_test.go
Expand Up @@ -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)
})
Expand Down

0 comments on commit 0a25263

Please sign in to comment.