Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
425 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package oauth2 | ||
|
||
import ( | ||
"github.com/ory-am/fosite" | ||
"github.com/pkg/errors" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type JWTAccessTokenStrategy interface { | ||
AccessTokenStrategy | ||
JWTStrategy | ||
} | ||
|
||
type StatelessJWTValidator struct { | ||
JWTAccessTokenStrategy | ||
ScopeStrategy fosite.ScopeStrategy | ||
} | ||
|
||
func (v *StatelessJWTValidator) IntrospectToken(ctx context.Context, token string, tokenType fosite.TokenType, accessRequest fosite.AccessRequester, scopes []string) (err error) { | ||
or, err := v.JWTAccessTokenStrategy.ValidateJWT(fosite.AccessToken, token) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, scope := range scopes { | ||
if scope == "" { | ||
continue | ||
} | ||
|
||
if !v.ScopeStrategy(or.GetGrantedScopes(), scope) { | ||
return errors.WithStack(fosite.ErrInvalidScope) | ||
} | ||
} | ||
|
||
accessRequest.Merge(or) | ||
return nil | ||
} | ||
|
||
// Revocation is not supported with the stateless validator. If you need revocation, use the | ||
// CoreValidator struct instead. | ||
func (v *StatelessJWTValidator) RevokeToken(ctx context.Context, token string, tokenType fosite.TokenType) error { | ||
return errors.Wrap(fosite.ErrMisconfiguration, "Token revocation is not supported") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package oauth2 | ||
|
||
import ( | ||
"encoding/base64" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ory-am/fosite" | ||
"github.com/ory-am/fosite/internal" | ||
"github.com/ory-am/fosite/token/jwt" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIntrospectJWT(t *testing.T) { | ||
strat := &RS256JWTStrategy{ | ||
RS256JWTStrategy: &jwt.RS256JWTStrategy{ | ||
PrivateKey: internal.MustRSAKey(), | ||
}, | ||
} | ||
|
||
v := &StatelessJWTValidator{ | ||
JWTAccessTokenStrategy: strat, | ||
ScopeStrategy: fosite.HierarchicScopeStrategy, | ||
} | ||
|
||
for k, c := range []struct { | ||
description string | ||
token func() string | ||
expectErr error | ||
scopes []string | ||
}{ | ||
{ | ||
description: "should fail because jwt is expired", | ||
token: func() string { | ||
jwt := jwtExpiredCase(fosite.AccessToken) | ||
token, _, err := strat.GenerateAccessToken(nil, jwt) | ||
assert.NoError(t, err) | ||
return token | ||
}, | ||
expectErr: fosite.ErrTokenExpired, | ||
}, | ||
{ | ||
description: "should pass because scope was granted", | ||
token: func() string { | ||
jwt := jwtValidCase(fosite.AccessToken) | ||
jwt.GrantedScopes = []string{"foo", "bar"} | ||
token, _, err := strat.GenerateAccessToken(nil, jwt) | ||
assert.NoError(t, err) | ||
return token | ||
}, | ||
scopes: []string{"foo"}, | ||
}, | ||
{ | ||
description: "should fail because scope was not granted", | ||
token: func() string { | ||
jwt := jwtValidCase(fosite.AccessToken) | ||
token, _, err := strat.GenerateAccessToken(nil, jwt) | ||
assert.NoError(t, err) | ||
return token | ||
}, | ||
scopes: []string{"foo"}, | ||
expectErr: fosite.ErrInvalidScope, | ||
}, | ||
{ | ||
description: "should fail because signature is invalid", | ||
token: func() string { | ||
jwt := jwtValidCase(fosite.AccessToken) | ||
token, _, err := strat.GenerateAccessToken(nil, jwt) | ||
assert.NoError(t, err) | ||
parts := strings.Split(token, ".") | ||
dec, err := base64.RawURLEncoding.DecodeString(parts[1]) | ||
assert.NoError(t, err) | ||
s := strings.Replace(string(dec), "peter", "piper", -1) | ||
parts[1] = base64.RawURLEncoding.EncodeToString([]byte(s)) | ||
return strings.Join(parts, ".") | ||
}, | ||
expectErr: fosite.ErrTokenSignatureMismatch, | ||
}, | ||
{ | ||
description: "should pass", | ||
token: func() string { | ||
jwt := jwtValidCase(fosite.AccessToken) | ||
token, _, err := strat.GenerateAccessToken(nil, jwt) | ||
assert.NoError(t, err) | ||
return token | ||
}, | ||
}, | ||
} { | ||
if c.scopes == nil { | ||
c.scopes = []string{} | ||
} | ||
areq := fosite.NewAccessRequest(nil) | ||
err := v.IntrospectToken(nil, c.token(), fosite.AccessToken, areq, c.scopes) | ||
|
||
assert.True(t, errors.Cause(err) == c.expectErr, "(%d) %s\n%s\n%s", k, c.description, err, c.expectErr) | ||
|
||
if err == nil { | ||
assert.Equal(t, "peter", areq.Session.GetSubject()) | ||
} | ||
|
||
t.Logf("Passed test case %d", k) | ||
} | ||
} | ||
|
||
func BenchmarkIntrospectJWT(b *testing.B) { | ||
strat := &RS256JWTStrategy{ | ||
RS256JWTStrategy: &jwt.RS256JWTStrategy{ | ||
PrivateKey: internal.MustRSAKey(), | ||
}, | ||
} | ||
|
||
v := &StatelessJWTValidator{ | ||
JWTAccessTokenStrategy: strat, | ||
} | ||
|
||
jwt := jwtValidCase(fosite.AccessToken) | ||
token, _, err := strat.GenerateAccessToken(nil, jwt) | ||
assert.NoError(b, err) | ||
areq := fosite.NewAccessRequest(nil) | ||
|
||
for n := 0; n < b.N; n++ { | ||
err = v.IntrospectToken(nil, token, fosite.AccessToken, areq, []string{}) | ||
} | ||
|
||
assert.NoError(b, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.