Skip to content

Commit

Permalink
all: resolve regression issues introduced by 0.6.0 - closes #118
Browse files Browse the repository at this point in the history
* oauth2: introspection handler excess calls - closes #117
* oauth2: inaccurate expires_in time - closes #72
  • Loading branch information
Aeneas Rekkas (arekkas) committed Oct 17, 2016
1 parent 7957b1f commit 0b64877
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 25 deletions.
6 changes: 3 additions & 3 deletions handler/openid/flow_hybrid.go
Expand Up @@ -19,7 +19,7 @@ type OpenIDConnectHybridHandler struct {
IDTokenHandleHelper *IDTokenHandleHelper
ScopeStrategy fosite.ScopeStrategy

Enigma *jwt.RS256JWTStrategy
Enigma *jwt.RS256JWTStrategy
}

func (c *OpenIDConnectHybridHandler) HandleAuthorizeEndpointRequest(ctx context.Context, req *http.Request, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
Expand Down Expand Up @@ -69,7 +69,7 @@ func (c *OpenIDConnectHybridHandler) HandleAuthorizeEndpointRequest(ctx context.
if err != nil {
return err
}
claims.CodeHash = []byte(base64.URLEncoding.EncodeToString([]byte(hash[:c.Enigma.GetSigningMethodLength()/2])))
claims.CodeHash = []byte(base64.URLEncoding.EncodeToString([]byte(hash[:c.Enigma.GetSigningMethodLength() / 2])))
}

if ar.GetResponseTypes().Has("token") {
Expand All @@ -84,7 +84,7 @@ func (c *OpenIDConnectHybridHandler) HandleAuthorizeEndpointRequest(ctx context.
if err != nil {
return err
}
claims.AccessTokenHash = []byte(base64.URLEncoding.EncodeToString([]byte(hash[:c.Enigma.GetSigningMethodLength()/2])))
claims.AccessTokenHash = []byte(base64.URLEncoding.EncodeToString([]byte(hash[:c.Enigma.GetSigningMethodLength() / 2])))
}

if !ar.GetGrantedScopes().Has("openid") {
Expand Down
6 changes: 3 additions & 3 deletions handler/openid/flow_implicit.go
Expand Up @@ -16,9 +16,9 @@ import (
type OpenIDConnectImplicitHandler struct {
AuthorizeImplicitGrantTypeHandler *oauth2.AuthorizeImplicitGrantTypeHandler
*IDTokenHandleHelper
ScopeStrategy fosite.ScopeStrategy
ScopeStrategy fosite.ScopeStrategy

RS256JWTStrategy *jwt.RS256JWTStrategy
RS256JWTStrategy *jwt.RS256JWTStrategy
}

func (c *OpenIDConnectImplicitHandler) HandleAuthorizeEndpointRequest(ctx context.Context, req *http.Request, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
Expand Down Expand Up @@ -63,7 +63,7 @@ func (c *OpenIDConnectImplicitHandler) HandleAuthorizeEndpointRequest(ctx contex
return err
}

claims.AccessTokenHash = []byte(base64.URLEncoding.EncodeToString([]byte(hash[:c.RS256JWTStrategy.GetSigningMethodLength()/2])))
claims.AccessTokenHash = []byte(base64.URLEncoding.EncodeToString([]byte(hash[:c.RS256JWTStrategy.GetSigningMethodLength() / 2])))
} else {
resp.AddFragment("state", ar.GetState())
}
Expand Down
1 change: 0 additions & 1 deletion handler/openid/flow_implicit_test.go
Expand Up @@ -112,7 +112,6 @@ func TestImplicit_HandleAuthorizeEndpointRequest(t *testing.T) {
Subject: "peter",
},
Headers: &jwt.Headers{},
DefaultSession: new(fosite.DefaultSession),
}
areq.Form.Add("nonce", "some-random-foo-nonce-wow")
},
Expand Down
42 changes: 38 additions & 4 deletions handler/openid/strategy_jwt.go
Expand Up @@ -22,19 +22,53 @@ type Session interface {

// IDTokenSession is a session container for the id token
type DefaultSession struct {
Claims *jwt.IDTokenClaims
Headers *jwt.Headers
*fosite.DefaultSession
Claims *jwt.IDTokenClaims
Headers *jwt.Headers
ExpiresAt map[fosite.TokenType]time.Time
Username string
Subject string
}

func NewDefaultSession() *DefaultSession {
return &DefaultSession{
Claims: &jwt.IDTokenClaims{},
Headers: &jwt.Headers{},
DefaultSession: &fosite.DefaultSession{},
}
}

func (s *DefaultSession) SetExpiresAt(key fosite.TokenType, exp time.Time) {
if s.ExpiresAt == nil {
s.ExpiresAt = make(map[fosite.TokenType]time.Time)
}
s.ExpiresAt[key] = exp
}

func (s *DefaultSession) GetExpiresAt(key fosite.TokenType) time.Time {
if s.ExpiresAt == nil {
s.ExpiresAt = make(map[fosite.TokenType]time.Time)
}

if _, ok := s.ExpiresAt[key]; !ok {
return time.Time{}
}
return s.ExpiresAt[key]
}

func (s *DefaultSession) GetUsername() string {
if s == nil {
return ""
}
return s.Username
}

func (s *DefaultSession) GetSubject() string {
if s == nil {
return ""
}

return s.Subject
}

func (s *DefaultSession) IDTokenHeaders() *jwt.Headers {
if s.Headers == nil {
s.Headers = &jwt.Headers{}
Expand Down
8 changes: 4 additions & 4 deletions integration/introspect_token_test.go
Expand Up @@ -46,28 +46,28 @@ func runIntrospectTokenTest(t *testing.T, strategy oauth2.AccessTokenStrategy) {
},
{
prepare: func(s *gorequest.SuperAgent) *gorequest.SuperAgent {
return s.Set("Authorization", "bearer "+a.AccessToken)
return s.Set("Authorization", "bearer " + a.AccessToken)
},
isActive: true,
scopes: "fosite",
},
{
prepare: func(s *gorequest.SuperAgent) *gorequest.SuperAgent {
return s.Set("Authorization", "bearer "+a.AccessToken)
return s.Set("Authorization", "bearer " + a.AccessToken)
},
isActive: true,
scopes: "",
},
{
prepare: func(s *gorequest.SuperAgent) *gorequest.SuperAgent {
return s.Set("Authorization", "bearer "+a.AccessToken)
return s.Set("Authorization", "bearer " + a.AccessToken)
},
isActive: false,
scopes: "foo",
},
{
prepare: func(s *gorequest.SuperAgent) *gorequest.SuperAgent {
return s.Set("Authorization", "bearer "+b.AccessToken)
return s.Set("Authorization", "bearer " + b.AccessToken)
},
isActive: false,
scopes: "",
Expand Down
2 changes: 0 additions & 2 deletions integration/oidc_explicit_test.go
Expand Up @@ -6,7 +6,6 @@ import (

"fmt"

"github.com/ory-am/fosite"
"github.com/ory-am/fosite/compose"
"github.com/ory-am/fosite/handler/openid"
"github.com/ory-am/fosite/internal"
Expand All @@ -22,7 +21,6 @@ func TestOpenIDConnectExplicitFlow(t *testing.T) {
Subject: "peter",
},
Headers: &jwt.Headers{},
DefaultSession: &fosite.DefaultSession{},
},
}
f := compose.ComposeAllEnabled(new(compose.Config), fositeStore, []byte("some-secret-thats-random"), internal.MustRSAKey())
Expand Down
4 changes: 1 addition & 3 deletions integration/oidc_implicit_hybrid_test.go
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"

"github.com/ory-am/fosite"
"github.com/ory-am/fosite/compose"
"github.com/ory-am/fosite/handler/openid"
"github.com/ory-am/fosite/internal"
Expand All @@ -27,7 +26,6 @@ func TestOIDCImplicitFlow(t *testing.T) {
Subject: "peter",
},
Headers: &jwt.Headers{},
DefaultSession: &fosite.DefaultSession{},
},
}
f := compose.ComposeAllEnabled(new(compose.Config), fositeStore, []byte("some-secret-thats-random"), internal.MustRSAKey())
Expand Down Expand Up @@ -76,7 +74,7 @@ func TestOIDCImplicitFlow(t *testing.T) {
c.setup()

var callbackURL *url.URL
authURL := strings.Replace(oauthClient.AuthCodeURL(state), "response_type=code", "response_type="+c.responseType, -1) + "&nonce=" + c.nonce
authURL := strings.Replace(oauthClient.AuthCodeURL(state), "response_type=code", "response_type=" + c.responseType, -1) + "&nonce=" + c.nonce
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
callbackURL = req.URL
Expand Down
2 changes: 1 addition & 1 deletion integration/refresh_token_grant_test.go
Expand Up @@ -11,7 +11,7 @@ import (
hst "github.com/ory-am/fosite/handler/oauth2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
oauth2 "golang.org/x/oauth2"
"golang.org/x/oauth2"
)

func TestRefreshTokenFlow(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions integration/revoke_token_test.go
Expand Up @@ -30,22 +30,22 @@ func runRevokeTokenTest(t *testing.T, strategy oauth2.AccessTokenStrategy) {
token, err := oauthClient.Token(goauth.NoContext)
assert.Nil(t, err)

resp, _, errs := gorequest.New().Post(ts.URL+"/revoke").
resp, _, errs := gorequest.New().Post(ts.URL + "/revoke").
SetBasicAuth(oauthClient.ClientID, oauthClient.ClientSecret).
Type("form").
SendStruct(map[string]string{"token": "asdf"}).End()
assert.Len(t, errs, 0)
assert.Equal(t, 200, resp.StatusCode)

resp, _, errs = gorequest.New().Post(ts.URL+"/revoke").
resp, _, errs = gorequest.New().Post(ts.URL + "/revoke").
SetBasicAuth(oauthClient.ClientID, oauthClient.ClientSecret).
Type("form").
SendStruct(map[string]string{"token": token.AccessToken}).End()
assert.Len(t, errs, 0)
assert.Equal(t, 200, resp.StatusCode)

hres, _, errs := gorequest.New().Get(ts.URL+"/info").
Set("Authorization", "bearer "+token.AccessToken).
hres, _, errs := gorequest.New().Get(ts.URL + "/info").
Set("Authorization", "bearer " + token.AccessToken).
End()
require.Len(t, errs, 0)
assert.Equal(t, http.StatusUnauthorized, hres.StatusCode)
Expand Down

0 comments on commit 0b64877

Please sign in to comment.