Skip to content

Commit

Permalink
integration: tests for resource owner password credentials grant
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Mar 21, 2016
1 parent c13298c commit f503615
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 23 deletions.
2 changes: 1 addition & 1 deletion authorize_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestWriteAuthorizeError(t *testing.T) {
mock: func() {
req.EXPECT().IsRedirectURIValid().Return(false)
rw.EXPECT().Header().Return(header)
rw.EXPECT().WriteHeader(http.StatusOK)
rw.EXPECT().WriteHeader(http.StatusBadRequest)
rw.EXPECT().Write(gomock.Any())
},
checkHeader: func(k int) {
Expand Down
6 changes: 3 additions & 3 deletions fosite-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/ory-am/fosite/client"
hmac "github.com/ory-am/fosite/enigma/hmac"
jwt "github.com/ory-am/fosite/enigma/jwt"
"github.com/ory-am/fosite/fosite-example/store"
exampleStore "github.com/ory-am/fosite/fosite-example/store"
coreclient "github.com/ory-am/fosite/handler/core/client"
"github.com/ory-am/fosite/handler/core/explicit"
"github.com/ory-am/fosite/handler/core/implicit"
Expand All @@ -25,15 +25,15 @@ import (
"golang.org/x/oauth2/clientcredentials"
)

var store = &store.Store{
var store = &exampleStore.Store{
Clients: map[string]client.Client{
"my-client": &client.SecureClient{
ID: "my-client",
Secret: []byte(`$2a$10$IxMdI6d.LIRZPpSfEwNoeu4rY3FhDREsxFJXikcgdRRAStxUlsuEO`), // = "foobar"
RedirectURIs: []string{"http://localhost:3846/callback"},
},
},
Users: map[string]store.UserRelation{
Users: map[string]exampleStore.UserRelation{
"peter": {
Username: "peter",
Password: "foobar",
Expand Down
12 changes: 6 additions & 6 deletions handler/core/strategy/hmacsha.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ type HMACSHAStrategy struct {
}

func (h HMACSHAStrategy) GenerateAccessToken(_ context.Context, _ *http.Request, requester fosite.Requester) (token string, signature string, err error) {
return h.Enigma.Generate(requester.GetClient().GetHashedSecret())
return h.Enigma.Generate()
}

func (h HMACSHAStrategy) ValidateAccessToken(_ context.Context, token string, _ *http.Request, requester fosite.Requester) (signature string, err error) {
return h.Enigma.Validate(requester.GetClient().GetHashedSecret(), token)
return h.Enigma.Validate(token)
}

func (h HMACSHAStrategy) GenerateRefreshToken(_ context.Context, _ *http.Request, requester fosite.Requester) (token string, signature string, err error) {
return h.Enigma.Generate(requester.GetClient().GetHashedSecret())
return h.Enigma.Generate()
}

func (h HMACSHAStrategy) ValidateRefreshToken(_ context.Context, token string, _ *http.Request, requester fosite.Requester) (signature string, err error) {
return h.Enigma.Validate(requester.GetClient().GetHashedSecret(), token)
return h.Enigma.Validate(token)
}

func (h HMACSHAStrategy) GenerateAuthorizeCode(_ context.Context, _ *http.Request, requester fosite.Requester) (token string, signature string, err error) {
return h.Enigma.Generate(requester.GetClient().GetHashedSecret())
return h.Enigma.Generate()
}

func (h HMACSHAStrategy) ValidateAuthorizeCode(_ context.Context, token string, _ *http.Request, requester fosite.Requester) (signature string, err error) {
return h.Enigma.Validate(requester.GetClient().GetHashedSecret(), token)
return h.Enigma.Validate(token)
}
8 changes: 0 additions & 8 deletions integration/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@ package integration_test

import (
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {

retCode := m.Run()

os.Exit(retCode)
}

func TestBasic(t *testing.T) {
f := newFosite()
ts := mockServer(t, f, nil)
Expand Down
83 changes: 83 additions & 0 deletions integration/client_credentials_grant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package integration_test

import (
"testing"

"github.com/ory-am/fosite/handler/core"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
"github.com/stretchr/testify/require"
"github.com/ory-am/fosite/handler/core/owner"
)

func TestClientCredentialsFlow(t *testing.T) {
for _, strategy := range []core.AccessTokenStrategy{
hmacStrategy,
} {
runClientCredentialsFlowTest(t, strategy)
}
}

func runClientCredentialsFlowTest(t *testing.T, strategy core.AccessTokenStrategy) {
f := newFosite()
ts := mockServer(t, f, nil)
defer ts.Close()

oauthClient := newOAuth2Client(ts)
var username string
var password string
for k, c := range []struct {
description string
setup func()
err bool
}{
{
description: "should fail because handler not registered",
setup: func() {},
err: true,
},
{
description: "should fail because unknown client",
setup: func() {
f.TokenEndpointHandlers.Append(&owner.ResourceOwnerPasswordCredentialsGrantHandler{
AccessTokenStrategy: strategy,
Store: fositeStore,
AccessTokenLifespan: accessTokenLifespan,
})
},
err: true,
},
{
description: "should fail because user does not exist",
setup: func() {
username = "not-existent"
password = "wrong"
},
err: true,
},
{
description: "should fail because wrong credentials",
setup: func() {
username = "peter"
password = "wrong"
},
err: true,
},
{
description: "should pass",
setup: func() {
username = "peter"
password = "foobar"
},
},
} {
c.setup()

token, err := oauthClient.PasswordCredentialsToken(oauth2.NoContext, username, password)
require.Equal(t, c.err, err != nil, "(%d) %s\n%s\n%s", k, c.description, c.err, err)
if !c.err {
assert.NotEmpty(t, token.AccessToken, "(%d) %s\n%s", k, c.description, token)
}
t.Logf("Passed test case %d", k)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"github.com/ory-am/fosite/handler/core/client"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2/clientcredentials"
"github.com/stretchr/testify/require"
)

func TestClientCredentialsFlow(t *testing.T) {
func TestResourceOwnerPasswordCredentialsGrant(t *testing.T) {
for _, strategy := range []core.AccessTokenStrategy{
hmacStrategy,
} {
runClientCredentialsFlowTest(t, strategy)
runResourceOwnerPasswordCredentialsGrantTest(t, strategy)
}
}

func runClientCredentialsFlowTest(t *testing.T, strategy core.AccessTokenStrategy) {
func runResourceOwnerPasswordCredentialsGrantTest(t *testing.T, strategy core.AccessTokenStrategy) {
f := newFosite()
ts := mockServer(t, f, nil)
defer ts.Close()
Expand Down

0 comments on commit f503615

Please sign in to comment.