Skip to content

Commit

Permalink
Rename config validator variable and add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Beorn Facchini <beornf@gmail.com>
  • Loading branch information
beornf committed Aug 18, 2017
1 parent 870601e commit 07e4ff8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
8 changes: 4 additions & 4 deletions compose/compose_oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func OAuth2TokenRevocationFactory(config *Config, storage interface{}, strategy
// an access token and refresh token validator.
func OAuth2TokenIntrospectionFactory(config *Config, storage interface{}, strategy interface{}) interface{} {
return &oauth2.CoreValidator{
CoreStrategy: strategy.(oauth2.CoreStrategy),
CoreStorage: storage.(oauth2.CoreStorage),
AccessOnly: config.GetAccessOnly(),
ScopeStrategy: config.GetScopeStrategy(),
CoreStrategy: strategy.(oauth2.CoreStrategy),
CoreStorage: storage.(oauth2.CoreStorage),
ScopeStrategy: config.GetScopeStrategy(),
DisableRefreshTokenValidation: config.DisableRefreshTokenValidation,
}
}

Expand Down
9 changes: 2 additions & 7 deletions compose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Config struct {
// HashCost sets the cost of the password hashing cost. Defaults to 12.
HashCost int

// AccessOnly sets whether the introspection endpoint only validates access tokens.
AccessOnly bool
// DisableRefreshTokenValidation sets the introspection endpoint to disable refresh token validation.
DisableRefreshTokenValidation bool

ScopeStrategy fosite.ScopeStrategy
}
Expand Down Expand Up @@ -64,8 +64,3 @@ func (c *Config) GetHashCost() int {
}
return c.HashCost
}

// GetAccessOnly returns whether the introspection endpoint only validates access tokens.
func (c *Config) GetAccessOnly() bool {
return c.AccessOnly
}
12 changes: 4 additions & 8 deletions handler/oauth2/introspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ import (
type CoreValidator struct {
CoreStrategy
CoreStorage
AccessOnly bool
ScopeStrategy fosite.ScopeStrategy
ScopeStrategy fosite.ScopeStrategy
DisableRefreshTokenValidation bool
}

func (c *CoreValidator) IntrospectToken(ctx context.Context, token string, tokenType fosite.TokenType, accessRequest fosite.AccessRequester, scopes []string) (err error) {
if c.AccessOnly {
if err = c.introspectAccessToken(ctx, token, accessRequest, scopes); err == nil {
return nil
}

return err
if c.DisableRefreshTokenValidation {
return c.introspectAccessToken(ctx, token, accessRequest, scopes)
}

switch tokenType {
Expand Down
8 changes: 8 additions & 0 deletions handler/oauth2/introspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ func TestIntrospectToken(t *testing.T) {
},
expectErr: fosite.ErrTokenExpired,
},
{
description: "should fail because access token invalid",
setup: func() {
v.DisableRefreshTokenValidation = true
chgen.EXPECT().ValidateAccessToken(nil, areq, "1234").Return(errors.WithStack(fosite.ErrInvalidTokenFormat))
},
expectErr: fosite.ErrInvalidTokenFormat,
},
{
description: "should pass",
setup: func() {
Expand Down

0 comments on commit 07e4ff8

Please sign in to comment.