Skip to content

Commit

Permalink
oauth2/ropc: Set expires at for password credentials flow (ory#210)
Browse files Browse the repository at this point in the history
Signed-off-by: Beorn Facchini <[email protected]>
  • Loading branch information
budougumi0617 committed Aug 21, 2017
1 parent a33a647 commit bc383cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions handler/oauth2/flow_resource_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package oauth2

import (
"fmt"
"time"

"context"

Expand Down Expand Up @@ -50,6 +51,8 @@ func (c *ResourceOwnerPasswordCredentialsGrantHandler) HandleTokenEndpointReques

// Credentials must not be passed around, potentially leaking to the database!
delete(request.GetRequestForm(), "password")

request.GetSession().SetExpiresAt(fosite.AccessToken, time.Now().Add(c.AccessTokenLifespan))
return nil
}

Expand Down
9 changes: 8 additions & 1 deletion handler/oauth2/flow_resource_owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestResourceOwnerFlow_HandleTokenEndpointRequest(t *testing.T) {
store := internal.NewMockResourceOwnerPasswordCredentialsGrantStorage(ctrl)
defer ctrl.Finish()

areq := fosite.NewAccessRequest(nil)
areq := fosite.NewAccessRequest(new(fosite.DefaultSession))
areq.Form = url.Values{}

h := ResourceOwnerPasswordCredentialsGrantHandler{
Expand All @@ -32,6 +32,7 @@ func TestResourceOwnerFlow_HandleTokenEndpointRequest(t *testing.T) {
description string
setup func()
expectErr error
check func(areq *fosite.AccessRequest)
}{
{
description: "should fail because not responsible",
Expand Down Expand Up @@ -64,12 +65,18 @@ func TestResourceOwnerFlow_HandleTokenEndpointRequest(t *testing.T) {
setup: func() {
store.EXPECT().Authenticate(nil, "peter", "pan").Return(nil)
},
check: func(areq *fosite.AccessRequest) {
assert.NotEmpty(t, areq.GetSession().GetExpiresAt(fosite.AccessToken))
},
},
} {
c.setup()
err := h.HandleTokenEndpointRequest(nil, areq)
assert.True(t, errors.Cause(err) == c.expectErr, "(%d) %s\n%s\n%s", k, c.description, err, c.expectErr)
t.Logf("Passed test case %d", k)
if c.check != nil {
c.check(areq)
}
}
}

Expand Down

0 comments on commit bc383cf

Please sign in to comment.