8000 feat: Authorization Grants using JWT by Xopek · Pull Request #546 · ory/fosite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Authorization Grants using JWT #546

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

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5bd00a4
feat: add ability to disable client authentication in token endpoint …
Nov 26, 2020
95798e7
feat(oauth2): add handler for authorize grant via jwt bearer
Nov 30, 2020
7e04502
fix(oauth2): mark jwt as used, if kid is provided in token
Dec 1, 2020
df5b6f1
refactor(oauth2): change err msg when no public key found for auth gr…
Dec 1, 2020
fd1ae59
feat(storage): add implementation of JWTAuthGrantStorage in MemoryStore
Dec 1, 2020
1509763
feat(compose): add support for AuthorizeJwtGrantHandler in compose
Dec 1, 2020
02992f7
fix(oauth2): fix incorrect message when validating assertion prerequi…
Dec 2, 2020
0e14733
fix(oauth2): fix nil pointer if all public keys didn't match in Autho…
Dec 2, 2020
18267a6
refactor(oauth2): change order of token validation in AuthorizeJwtGra…
Dec 3, 2020
b406a62
test: added unit tests for AuthorizeJwtGrantHandler
Dec 3, 2020
0d11ebd
fix(oauth2): fill session with subject, scopes, audience correctly in…
Dec 7, 2020
ca7f8a9
test: fix test assertion
Dec 9, 2020
f1a93b0
fix(oauth2): fix audience grant in AuthorizeJwtGrantHandler
Dec 9, 2020
9cba91b
feat(oauth2): add additional check for exp claim in AuthorizeJwtGrant…
Dec 9, 2020
665b957
cases for jwt bearer authorization grand creation
seliverstov-tinkoff Dec 3, 2020
db0aafa
draft introspection cases of jwt bearer
seliverstov-tinkoff Dec 4, 2020
db506b9
add clients for jwt bearer test
seliverstov-tinkoff Dec 8, 2020
38afdae
introspect token expired test
seliverstov-tinkoff Dec 9, 2020
a9de527
add constants for tests and few test on introspect token
seliverstov-tinkoff Dec 9, 2020
af01a48
use jose for jwt generation in tests
seliverstov-tinkoff Dec 10, 2020
7dea172
rename jwt bearer tests
seliverstov-tinkoff Dec 10, 2020
92bee79
introspection auth header store in suite
seliverstov-tinkoff Dec 11, 2020
ade7901
fix TestSuccessResponseWithMultipleScopesToken test
seliverstov-tinkoff Dec 11, 2020
6cf3d51
refactor: client authentification handling
Dec 16, 2020
27babf5
fix(oauth2): fix code style, naming and jwt token validation
Jan 19, 2021
418ddc3
fix(oauth2): mark jwt as used only after all checks
Jan 19, 2021
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
14 changes: 9 additions & 5 deletions access_request_handler.go
10000
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import (
// client MUST authenticate with the authorization server as described
// in Section 3.2.1.
func (f *Fosite) NewAccessRequest(ctx context.Context, r *http.Request, session Session) (AccessRequester, error) {
var err error
accessRequest := NewAccessRequest(session)

if r.Method != "POST" {
Expand All @@ -80,14 +79,19 @@ func (f *Fosite) NewAccessRequest(ctx context.Context, r *http.Request, session
return accessRequest, errorsx.WithStack(ErrInvalidRequest.WithHint("Request parameter 'grant_type' is missing"))
}

client, err := f.AuthenticateClient(ctx, r, r.PostForm)
if err != nil {
return accessRequest, err
client, clientErr := f.AuthenticateClient(ctx, r, r.PostForm)
if clientErr == nil {
accessRequest.Client = client
}
accessRequest.Client = client

var found = false
for _, loader := range f.TokenEndpointHandlers {
if !loader.CanHandleTokenEndpointRequest(accessRequest) {
continue
}
if !loader.CanSkipClientAuth(accessRequest) && clientErr != nil {
return accessRequest, clientErr
}
if err := loader.HandleTokenEndpointRequest(ctx, accessRequest); err == nil {
found = true
} else if errors.Is(err, ErrUnknownRequest) {
Expand Down
211 changes: 211 additions & 0 deletions access_request_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func TestNewAccessRequest(t *testing.T) {
ctrl := gomock.NewController(t)
store := internal.NewMockStorage(ctrl)
handler := internal.NewMockTokenEndpointHandler(ctrl)
handler.EXPECT().CanHandleTokenEndpointRequest(gomock.Any()).Return(true).AnyTimes()
handler.EXPECT().CanSkipClientAuth(gomock.Any()).Return(false).AnyTimes()
hasher := internal.NewMockHasher(ctrl)
defer ctrl.Finish()

Expand Down Expand Up @@ -94,6 +96,7 @@ func TestNewAccessRequest(t *testing.T) {
mock: func() {
store.EXPECT().GetClient(gomock.Any(), gomock.Eq("foo")).Return(nil, errors.New(""))
},
handlers: TokenEndpointHandlers{handler},
},
{
header: http.Header{
Expand All @@ -118,6 +121,7 @@ func TestNewAccessRequest(t *testing.T) {
mock: func() {
store.EXPECT().GetClient(gomock.Any(), gomock.Eq("foo")).Return(nil, errors.New(""))
},
handlers: TokenEndpointHandlers{handler},
},
{
header: http.Header{
Expand All @@ -134,6 +138,7 @@ func TestNewAccessRequest(t *testing.T) {
client.Secret = []byte("foo")
hasher.EXPECT().Compare(context.TODO(), gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(errors.New(""))
},
handlers: TokenEndpointHandlers{handler},
},
{
header: http.Header{
Expand Down Expand Up @@ -221,6 +226,212 @@ func TestNewAccessRequest(t *testing.T) {
}
}

func TestNewAccessRequestWithoutClientAuth(t *testing.T) {
ctrl := gomock.NewController(t)
store := internal.NewMockStorage(ctrl)
handler := internal.NewMockTokenEndpointHandler(ctrl)
handler.EXPECT().CanHandleTokenEndpointRequest(gomock.Any()).Return(true).AnyTimes()
handler.EXPECT().CanSkipClientAuth(gomock.Any()).Return(true).AnyTimes()
hasher := internal.NewMockHasher(ctrl)
defer ctrl.Finish()

client := &DefaultClient{}
fosite := &Fosite{Store: store, Hasher: hasher, AudienceMatchingStrategy: DefaultAudienceMatchingStrategy}
for k, c := range []struct {
header http.Header
form url.Values
mock func()
method string
expectErr error
expect *AccessRequest
handlers TokenEndpointHandlers
}{
{
form: url.Values{},
mock: func() {
store.EXPECT().GetClient(gomock.Any(), gomock.Any()).Times(0)
},
method: "POST",
expectErr: ErrInvalidRequest,
},
{
form: url.Values{
"grant_type": {"foo"},
},
mock: func() {
store.EXPECT().GetClient(gomock.Any(), gomock.Any()).Times(0)
},
method: "POST",
expectErr: ErrInvalidRequest,
handlers: TokenEndpointHandlers{}, // no handlers, so expect error
},
{
header: http.Header{
"Authorization": {basicAuth("foo", "bar")},
},
form: url.Values{
"grant_type": {"foo"},
},
mock: func() {
// despite error from storage, we should success, because client auth is not required
store.EXPECT().GetClient(gomock.Any(), "foo").Return(nil, errors.New("no client")).Times(1)
handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
},
method: "POST",
expect: &AccessRequest{
GrantTypes: Arguments{"foo"},
Request: Request{
Client: client,
},
},
handlers: TokenEndpointHandlers{handler},
},
{
form: url.Values{
"grant_type": {"foo"},
},
mock: func() {
handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
},
method: "POST",
expect: &AccessRequest{
GrantTypes: Arguments{"foo"},
Request: Request{
Client: client,
Copy link
Member

Choose a reason for hiding this comment

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

Where does this client come from? Shouldn't the client be nil? What I also don't fully understand is - store := internal.NewMockStorage(ctrl) does not seem to have any EXPECT calls?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is default empty client struct. When Request is created default value is used for client, so it is just an empty struct. So we are checking, that even without client auth (and hence storage wont be called, and client struct will stay empty) everything is working.

Considering storage - it is not expected to be called, because we are not passing any client credentials. However i agree that it is not very clear why is that. That's why i will add case, where we WILL pass client credentials and in other cases will add explicit instruction, that we are not expecting storage to be called.

Copy link
Contributor Author
@Xopek Xopek Jan 18, 2021

Choose a reason for hiding this comment

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

Also i am suggesting to rewrite the whole tests in this file completely, because each test affects other test, because they share mocks and other vars which results in that test cases are not isolated from each other. That's why new tests that were added in this PR were made using suite package of testify. Only in this file i write new test cases in the same style as old ones.

If you are not against this idea maybe i can rewrite them using suite, but i will made it in separate PR.

},
},
handlers: TokenEndpointHandlers{handler},
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
r := &http.Request{
Header: c.header,
PostForm: c.form,
Form: c.form,
Method: c.method,
}
c.mock()
ctx := NewContext()
fosite.TokenEndpointHandlers = c.handlers
ar, err := fosite.NewAccessRequest(ctx, r, new(DefaultSession))

if c.expectErr != nil {
assert.EqualError(t, err, c.expectErr.Error())
} else {
require.NoError(t, err)
AssertObjectKeysEqual(t, c.expect, ar, "GrantTypes", "Client")
assert.NotNil(t, ar.GetRequestedAt())
}
})
}
}

// In this test case one handler requires client auth and another handler not.
func TestNewAccessRequestWithMixedClientAuth(t *testing.T) {
ctrl := gomock.NewController(t)
store := internal.NewMockStorage(ctrl)

handlerWithClientAuth := internal.NewMockTokenEndpointHandler(ctrl)
handlerWithClientAuth.EXPECT().CanHandleTokenEndpointRequest(gomock.Any()).Return(true).AnyTimes()
handlerWithClientAuth.EXPECT().CanSkipClientAuth(gomock.Any()).Return(false).AnyTimes()

handlerWithoutClientAuth := internal.NewMockTokenEndpointHandler(ctrl)
handlerWithoutClientAuth.EXPECT().CanHandleTokenEndpointRequest(gomock.Any()).Return(true).AnyTimes()
handlerWithoutClientAuth.EXPECT().CanSkipClientAuth(gomock.Any()).Return(true).AnyTimes()

hasher := internal.NewMockHasher(ctrl)
defer ctrl.Finish()

client := &DefaultClient{}
fosite := &Fosite{Store: store, Hasher: hasher, AudienceMatchingStrategy: DefaultAudienceMatchingStrategy}
for k, c := range []struct {
header http.Header
form url.Values
mock func()
method string
expectErr error
expect *AccessRequest
handlers TokenEndpointHandlers
}{
{
header: http.Header{
"Authorization": {basicAuth("foo", "bar")},
},
form: url.Values{
"grant_type": {"foo"},
},
mock: func() {
store.EXPECT().GetClient(gomock.Any(), gomock.Eq("foo")).Return(client, nil)
client.Public = false
client.Secret = []byte("foo")
hasher.EXPECT().Compare(context.TODO(), gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(errors.New("hash err"))
handlerWithoutClientAuth.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
},
method: "POST",
expectErr: ErrInvalidClient,
handlers: TokenEndpointHandlers{handlerWithoutClientAuth, handlerWithClientAuth},
},
{
header: http.Header{
"Authorization": {basicAuth("foo", "bar")},
},
form: url.Values{
"grant_type": {"foo"},
},
mock: func() {
store.EXPECT().GetClient(gomock.Any(), gomock.Eq("foo")).Return(client, nil)
client.Public = false
client.Secret = []byte("foo")
hasher.EXPECT().Compare(context.TODO(), gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil)
handlerWithoutClientAuth.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
handlerWithClientAuth.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
},
method: "POST",
expect: &AccessRequest{
GrantTypes: Arguments{"foo"},
Request: Request{
Client: client,
},
},
handlers: TokenEndpointHandlers{handlerWithoutClientAuth, handlerWithClientAuth},
},
{
header: http.Header{},
form: url.Values{
"grant_type": {"foo"},
},
mock: func() {
store.EXPECT().GetClient(gomock.Any(), gomock.Any()).Times(0)
handlerWithoutClientAuth.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
},
method: "POST",
expectErr: ErrInvalidRequest,
handlers: TokenEndpointHandlers{handlerWithoutClientAuth, handlerWithClientAuth},
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
r := &http.Request{
Header: c.header,
PostForm: c.form,
Form: c.form,
Method: c.method,
}
c.mock()
ctx := NewContext()
fosite.TokenEndpointHandlers = c.handlers
ar, err := fosite.NewAccessRequest(ctx, r, new(DefaultSession))

if c.expectErr != nil {
assert.EqualError(t, err, c.expectErr.Error())
} else {
require.NoError(t, err)
AssertObjectKeysEqual(t, c.expect, ar, "GrantTypes", "Client")
assert.NotNil(t, ar.GetRequestedAt())
}
})
}
}

func basicAuth(username, password string) string {
return "Basic " + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
}
1 change: 1 addition & 0 deletions compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func ComposeAllEnabled(config *Config, storage interface{}, secret []byte, key *
OAuth2ClientCredentialsGrantFactory,
OAuth2RefreshTokenGrantFactory,
OAuth2ResourceOwnerPasswordCredentialsFactory,
OAuth2AuthorizeJWTGrantFactory,

OpenIDConnectExplicitFactory,
OpenIDConnectImplicitFactory,
Expand Down
20 changes: 20 additions & 0 deletions compose/compose_oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,23 @@ func OAuth2StatelessJWTIntrospectionFactory(config *Config, storage interface{},
ScopeStrategy: config.GetScopeStrategy(),
}
}

// OAuth2AuthorizeExplicitFactory creates an OAuth2 authorize jwt grant (using JWTs as Authorization Grants) handler
// and registers an access token, refresh token and authorize code validator.
func OAuth2AuthorizeJWTGrantFactory(config *Config, storage interface{}, strategy interface{}) interface{} {
return &oauth2.AuthorizeJWTGrantHandler{
AuthorizeJWTGrantStorage: storage.(oauth2.AuthorizeJWTGrantStorage),
ScopeStrategy: config.GetScopeStrategy(),
AudienceMatchingStrategy: config.GetAudienceStrategy(),
TokenURL: config.TokenURL,
SkipClientAuth: config.GrantTypeJWTBearerCanSkipClientAuth,
JWTIDOptional: config.GrantTypeJWTBearerIDOptional,
JWTIssuedDateOptional: config.GrantTypeJWTBearerIssuedDateOptional,
JWTMaxDuration: config.GetJWTMaxDuration(),
HandleHelper: &oauth2.HandleHelper{
AccessTokenStrategy: strategy.(oauth2.AccessTokenStrategy),
AccessTokenStorage: storage.(oauth2.AccessTokenStorage),
AccessTokenLifespan: config.GetAccessTokenLifespan(),
},
}
}
21 changes: 21 additions & 0 deletions compose/config.go
3D11
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ type Config struct {
// UseLegacyErrorFormat controls whether the legacy error format (with `error_debug`, `error_hint`, ...)
// should be used or not.
UseLegacyErrorFormat bool

// GrantTypeJWTBearerCanSkipClientAuth indicates, if client authentication can be skipped, when using jwt as assertion.
GrantTypeJWTBearerCanSkipClientAuth bool

// GrantTypeJWTBearerIDOptional indicates, if jti (JWT ID) claim required or not in JWT.
GrantTypeJWTBearerIDOptional bool

// GrantTypeJWTBearerIssuedDateOptional indicates, if "iat" (issued at) claim required or not in JWT.
GrantTypeJWTBearerIssuedDateOptional bool

// GrantTypeJWTBearerMaxDuration sets the maximum time after JWT issued date, during which the JWT is considered valid.
GrantTypeJWTBearerMaxDuration time.Duration
}

// GetScopeStrategy returns the scope strategy to be used. Defaults to glob scope strategy.
Expand Down Expand Up @@ -198,3 +210,12 @@ func (c *Config) GetMinParameterEntropy() int {
return c.MinParameterEntropy
}
}

// GetJWTMaxDuration returns the maximum time after JWT issued date, during which the JWT is considered valid.
// Defaults to 30 days.
func (c *Config) GetJWTMaxDuration() time.Duration {
if c.GrantTypeJWTBearerMaxDuration == 0 {
return time.Hour * 24 * 30
}
return c.GrantTypeJWTBearerMaxDuration
}
1 change: 1 addition & 0 deletions generate-mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mockgen -package internal -destination internal/transactional.go github.com/ory/
mockgen -package internal -destination internal/oauth2_storage.go github.com/ory/fosite/handler/oauth2 CoreStorage
mockgen -package internal -destination internal/oauth2_strategy.go github.com/ory/fosite/handler/oauth2 CoreStrategy
mockgen -package internal -destination internal/authorize_code_storage.go github.com/ory/fosite/handler/oauth2 AuthorizeCodeStorage
mockgen -package internal -destination internal/oauth2_auth_jwt_storage.go github.com/ory/fosite/handler/oauth2 AuthorizeJWTGrantStorage
mockgen -package internal -destination internal/access_token_storage.go github.com/ory/fosite/handler/oauth2 AccessTokenStorage
mockgen -package internal -destination internal/refresh_token_strategy.go github.com/ory/fosite/handler/oauth2 RefreshTokenStorage
mockgen -package internal -destination internal/oauth2_client_storage.go github.com/ory/fosite/handler/oauth2 ClientCredentialsGrantStorage
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require (
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535
github.com/dgraph-io/ristretto v0.0.3 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/golang/mock v1.4.3
github.com/golang/mock v1.4.4
github.com/golang/protobuf v1.4.0 // indirect
github.com/gorilla/mux v1.7.3
github.com/gorilla/websocket v1.4.2
Expand Down
Loading
0