-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
feat: add support for none response_type #776
Open
dtam-cybozu
wants to merge
1
commit into
ory:master
Choose a base branch
from
dtam-cybozu:none-response-type
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,64 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package oauth2 | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/ory/x/errorsx" | ||
|
||
"github.com/ory/fosite" | ||
) | ||
|
||
var _ fosite.AuthorizeEndpointHandler = (*NoneResponseTypeHandler)(nil) | ||
|
||
// NoneResponseTypeHandler is a response handler for when the None response type is requested | ||
// as defined in https://openid.net/specs/oauth-v2-multiple-response-types-1_0.html#none | ||
type NoneResponseTypeHandler struct { | ||
Config interface { | ||
fosite.ScopeStrategyProvider | ||
fosite.AudienceStrategyProvider | ||
fosite.RedirectSecureCheckerProvider | ||
fosite.OmitRedirectScopeParamProvider | ||
} | ||
} | ||
|
||
func (c *NoneResponseTypeHandler) secureChecker(ctx context.Context) func(context.Context, *url.URL) bool { | ||
if c.Config.GetRedirectSecureChecker(ctx) == nil { | ||
return fosite.IsRedirectURISecure | ||
} | ||
return c.Config.GetRedirectSecureChecker(ctx) | ||
} | ||
|
||
func (c *NoneResponseTypeHandler) HandleAuthorizeEndpointRequest(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error { | ||
if !ar.GetResponseTypes().ExactOne("none") { | ||
return nil | ||
} | ||
|
||
ar.SetDefaultResponseMode(fosite.ResponseModeQuery) | ||
|
||
if !c.secureChecker(ctx)(ctx, ar.GetRedirectURI()) { | ||
return errorsx.WithStack(fosite.ErrInvalidRequest.WithHint("Redirect URL is using an insecure protocol, http is only allowed for hosts with suffix 'localhost', for example: http://myapp.localhost/.")) | ||
} | ||
|
||
client := ar.GetClient() | ||
for _, scope := range ar.GetRequestedScopes() { | ||
if !c.Config.GetScopeStrategy(ctx)(client.GetScopes(), scope) { | ||
return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The OAuth 2.0 Client is not allowed to request scope '%s'.", scope)) | ||
} | ||
} | ||
|
||
if err := c.Config.GetAudienceStrategy(ctx)(client.GetAudience(), ar.GetRequestedAudience()); err != nil { | ||
return err | ||
} | ||
|
||
resp.AddParameter("state", ar.GetState()) | ||
if !c.Config.GetOmitRedirectScopeParam(ctx) { | ||
resp.AddParameter("scope", strings.Join(ar.GetGrantedScopes(), " ")) | ||
} | ||
ar.SetResponseTypeHandled("none") | ||
return nil | ||
} |
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,182 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package oauth2 | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ory/fosite" | ||
) | ||
|
||
func TestNone_HandleAuthorizeEndpointRequest(t *testing.T) { | ||
handler := NoneResponseTypeHandler{ | ||
Config: &fosite.Config{ | ||
ScopeStrategy: fosite.HierarchicScopeStrategy, | ||
AudienceMatchingStrategy: fosite.DefaultAudienceMatchingStrategy, | ||
}, | ||
} | ||
for _, c := range []struct { | ||
handler NoneResponseTypeHandler | ||
areq *fosite.AuthorizeRequest | ||
description string | ||
expectErr error | ||
expect func(t *testing.T, areq *fosite.AuthorizeRequest, aresp *fosite.AuthorizeResponse) | ||
}{ | ||
{ | ||
handler: handler, | ||
areq: &fosite.AuthorizeRequest{ | ||
ResponseTypes: fosite.Arguments{""}, | ||
Request: *fosite.NewRequest(), | ||
}, | ||
description: "should pass because not responsible for handling an empty response type", | ||
}, | ||
{ | ||
handler: handler, | ||
areq: &fosite.AuthorizeRequest{ | ||
ResponseTypes: fosite.Arguments{"foo"}, | ||
Request: *fosite.NewRequest(), | ||
}, | ||
description: "should pass because not responsible for handling an invalid response type", | ||
}, | ||
{ | ||
handler: handler, | ||
areq: &fosite.AuthorizeRequest{ | ||
ResponseTypes: fosite.Arguments{"none"}, | ||
Request: fosite.Request{ | ||
Client: &fosite.DefaultClient{ | ||
ResponseTypes: fosite.Arguments{"code", "none"}, | ||
RedirectURIs: []string{"http://asdf.com/cb"}, | ||
}, | ||
}, | ||
RedirectURI: parseUrl("http://asdf.com/cb"), | ||
}, | ||
description: "should fail because redirect uri is not https", | ||
expectErr: fosite.ErrInvalidRequest, | ||
}, | ||
{ | ||
handler: handler, | ||
areq: &fosite.AuthorizeRequest{ | ||
ResponseTypes: fosite.Arguments{"none"}, | ||
Request: fosite.Request{ | ||
Client: &fosite.DefaultClient{ | ||
ResponseTypes: fosite.Arguments{"code", "none"}, | ||
RedirectURIs: []string{"https://asdf.com/cb"}, | ||
Audience: []string{"https://www.ory.sh/api"}, | ||
}, | ||
RequestedAudience: []string{"https://www.ory.sh/not-api"}, | ||
}, | ||
RedirectURI: parseUrl("https://asdf.com/cb"), | ||
}, | ||
description: "should fail because audience doesn't match", | ||
expectErr: fosite.ErrInvalidRequest, | ||
}, | ||
{ | ||
handler: handler, | ||
areq: &fosite.AuthorizeRequest{ | ||
ResponseTypes: fosite.Arguments{"none"}, | ||
Request: fosite.Request{ | ||
Client: &fosite.DefaultClient{ | ||
ResponseTypes: fosite.Arguments{"code", "none"}, | ||
RedirectURIs: []string{"https://asdf.de/cb"}, | ||
Audience: []string{"https://www.ory.sh/api"}, | ||
}, | ||
RequestedAudience: []string{"https://www.ory.sh/api"}, | ||
GrantedScope: fosite.Arguments{"a", "b"}, | ||
Session: &fosite.DefaultSession{ | ||
ExpiresAt: map[fosite.TokenType]time.Time{fosite.AccessToken: time.Now().UTC().Add(time.Hour)}, | ||
}, | ||
RequestedAt: time.Now().UTC(), | ||
}, | ||
State: "superstate", | ||
RedirectURI: parseUrl("https://asdf.de/cb"), | ||
}, | ||
description: "should pass", | ||
expect: func(t *testing.T, areq *fosite.AuthorizeRequest, aresp *fosite.AuthorizeResponse) { | ||
assert.Equal(t, strings.Join(areq.GrantedScope, " "), aresp.GetParameters().Get("scope")) | ||
assert.Equal(t, areq.State, aresp.GetParameters().Get("state")) | ||
assert.Equal(t, fosite.ResponseModeQuery, areq.GetResponseMode()) | ||
}, | ||
}, | ||
{ | ||
handler: handler, | ||
areq: &fosite.AuthorizeRequest{ | ||
ResponseTypes: fosite.Arguments{"none"}, | ||
Request: fosite.Request{ | ||
Client: &fosite.DefaultClient{ | ||
ResponseTypes: fosite.Arguments{"none"}, | ||
RedirectURIs: []string{"https://asdf.de/cb"}, | ||
Audience: []string{"https://www.ory.sh/api"}, | ||
}, | ||
RequestedAudience: []string{"https://www.ory.sh/api"}, | ||
GrantedScope: fosite.Arguments{"a", "b"}, | ||
Session: &fosite.DefaultSession{ | ||
ExpiresAt: map[fosite.TokenType]time.Time{fosite.AccessToken: time.Now().UTC().Add(time.Hour)}, | ||
}, | ||
RequestedAt: time.Now().UTC(), | ||
}, | ||
State: "superstate", | ||
RedirectURI: parseUrl("https://asdf.de/cb"), | ||
}, | ||
description: "should pass with no response types other than none", | ||
expect: func(t *testing.T, areq *fosite.AuthorizeRequest, aresp *fosite.AuthorizeResponse) { | ||
assert.Equal(t, strings.Join(areq.GrantedScope, " "), aresp.GetParameters().Get("scope")) | ||
assert.Equal(t, areq.State, aresp.GetParameters().Get("state")) | ||
assert.Equal(t, fosite.ResponseModeQuery, areq.GetResponseMode()) | ||
}, | ||
}, | ||
{ | ||
handler: NoneResponseTypeHandler{ | ||
Config: &fosite.Config{ | ||
ScopeStrategy: fosite.HierarchicScopeStrategy, | ||
AudienceMatchingStrategy: fosite.DefaultAudienceMatchingStrategy, | ||
OmitRedirectScopeParam: true, | ||
}, | ||
}, | ||
areq: &fosite.AuthorizeRequest{ | ||
ResponseTypes: fosite.Arguments{"none"}, | ||
Request: fosite.Request{ | ||
Client: &fosite.DefaultClient{ | ||
ResponseTypes: fosite.Arguments{"code", "none"}, | ||
RedirectURIs: []string{"https://asdf.de/cb"}, | ||
Audience: []string{"https://www.ory.sh/api"}, | ||
}, | ||
RequestedAudience: []string{"https://www.ory.sh/api"}, | ||
GrantedScope: fosite.Arguments{"a", "b"}, | ||
Session: &fosite.DefaultSession{ | ||
ExpiresAt: map[fosite.TokenType]time.Time{fosite.AccessToken: time.Now().UTC().Add(time.Hour)}, | ||
}, | ||
RequestedAt: time.Now().UTC(), | ||
}, | ||
State: "superstate", | ||
RedirectURI: parseUrl("https://asdf.de/cb"), | ||
}, | ||
description: "should pass but no scope in redirect uri", | ||
expect: func(t *testing.T, areq *fosite.AuthorizeRequest, aresp *fosite.AuthorizeResponse) { | ||
assert.Empty(t, aresp.GetParameters().Get("scope")) | ||
assert.Equal(t, areq.State, aresp.GetParameters().Get("state")) | ||
assert.Equal(t, fosite.ResponseModeQuery, areq.GetResponseMode()) | ||
}, | ||
}, | ||
} { | ||
t.Run("case="+c.description, func(t *testing.T) { | ||
aresp := fosite.NewAuthorizeResponse() | ||
err := c.handler.HandleAuthorizeEndpointRequest(context.Background(), c.areq, aresp) | ||
if c.expectErr != nil { | ||
require.EqualError(t, err, c.expectErr.Error()) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
|
||
if c.expect != nil { | ||
c.expect(t, c.areq, aresp) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this encapsulation is not readable, since
secureChecker
has no more caller.You can just change
secureChecker
tosecureCheck
, pass theuri
to itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, actually most of this file is copy pasted from https://github.com/ory/fosite/blob/master/handler/oauth2/flow_authorize_code_auth.go so I've just copied the style from that file.