Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do omit exp if ExpiresAt is zero value #334

Merged
merged 2 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion introspection_response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ func (f *Fosite) WriteIntrospectionResponse(rw http.ResponseWriter, r Introspect
return
}

expiresAt := int64(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test (or modify an existing one) for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do later.

if !r.GetAccessRequester().GetSession().GetExpiresAt(AccessToken).IsZero() {
expiresAt = r.GetAccessRequester().GetSession().GetExpiresAt(AccessToken).Unix()
}

rw.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(rw).Encode(struct {
Active bool `json:"active"`
Expand All @@ -218,7 +223,7 @@ func (f *Fosite) WriteIntrospectionResponse(rw http.ResponseWriter, r Introspect
Active: true,
ClientID: r.GetAccessRequester().GetClient().GetID(),
Scope: strings.Join(r.GetAccessRequester().GetGrantedScopes(), " "),
ExpiresAt: r.GetAccessRequester().GetSession().GetExpiresAt(AccessToken).Unix(),
ExpiresAt: expiresAt,
IssuedAt: r.GetAccessRequester().GetRequestedAt().Unix(),
Subject: r.GetAccessRequester().GetSession().GetSubject(),
Audience: r.GetAccessRequester().GetGrantedAudience(),
Expand Down
76 changes: 76 additions & 0 deletions introspection_response_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
package fosite_test

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

. "github.com/ory/fosite"
"github.com/ory/fosite/internal"
Expand Down Expand Up @@ -61,3 +66,74 @@ func TestWriteIntrospectionResponse(t *testing.T) {
AccessRequester: NewAccessRequest(nil),
})
}

func TestWriteIntrospectionResponseBody(t *testing.T) {
f := new(Fosite)
ires := &IntrospectionResponse{}
rw := httptest.NewRecorder()

for _, c := range []struct {
description string
setup func()
active bool
hasExp bool
}{
{
description: "should success for not expired access token",
setup: func() {
ires.Active = true
ires.TokenType = AccessToken
sess := &DefaultSession{}
sess.SetExpiresAt(ires.TokenType, time.Now().Add(time.Hour*2))
ires.AccessRequester = NewAccessRequest(sess)
},
active: true,
hasExp: true,
},
{
description: "should success for expired access token",
setup: func() {
ires.Active = false
ires.TokenType = AccessToken
sess := &DefaultSession{}
sess.SetExpiresAt(ires.TokenType, time.Now().Add(-time.Hour*2))
ires.AccessRequester = NewAccessRequest(sess)
},
active: false,
hasExp: false,
},
{
description: "should success for ExpiresAt not set access token",
setup: func() {
ires.Active = true
ires.TokenType = AccessToken
sess := &DefaultSession{}
sess.SetExpiresAt(ires.TokenType, time.Time{})
ires.AccessRequester = NewAccessRequest(sess)
},
active: true,
hasExp: false,
},
} {
t.Run(c.description, func(t *testing.T) {
c.setup()
f.WriteIntrospectionResponse(rw, ires)
var params struct {
Active bool `json:"active"`
Exp *int64 `json:"exp"`
Iat *int64 `json:"iat"`
}
err := json.NewDecoder(rw.Body).Decode(&params)
require.NoError(t, err)
assert.Equal(t, c.active, params.Active)
if c.active {
assert.NotNil(t, params.Iat)
if c.hasExp {
assert.NotNil(t, params.Exp)
} else {
assert.Nil(t, params.Exp)
}
}
})
}
}