-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Move the OAuth2AuthorizationEndpointFilter further back in the chain to allow for stateless session authentication #797
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
Comments
This is related to #551 / https://stackoverflow.com/questions/69484979/spring-authorization-server-how-to-use-login-form-hosted-on-a-separate-applicat/69577014#69577014 however, in the case of stateless session it doesn't work like that because this solution requires the |
I tested locally with a one-line change to the builder.addFilterBefore(postProcess(authorizationEndpointFilter), AnonymousAuthenticationFilter.class);
// instead of
// builder.addFilterBefore(postProcess(authorizationEndpointFilter), AbstractPreAuthenticatedProcessingFilter.class); This change makes it work with the sessionless authentication and the existing tests do not fail ( |
@its-felix, thanks for reaching out! I believe there are a number of good reasons why the filter is placed where it is, but I can't enumerate them all confidently without more research. I'm curious, have you tried configuring your own instance of |
@sjohnr I understand this would be a critical change. I understand this would not be required for most usecases. Making it customizable would be great though. Yes, I currently run https://github.com/gw2auth/oauth2-server (https://gw2auth.com/) with a custom build of this project (https://github.com/its-felix/spring-authorization-server/tree/maven-publish) I also have a good number of integration test for the authorization server (see https://github.com/gw2auth/oauth2-server/blob/main/src/test/java/com/gw2auth/oauth2/server/oauth2/OAuth2ServerTest.java). I introduced stateless sessions and this change with this commit: gw2auth/oauth2-server@49cfaca |
@its-felix I'd like to understand your use case better.
I don't see how this would work? The
Where is the user logging into? I'm assuming the IdP integrated with the Authorization Server? |
Hi @jgrandja , the user (resource owner) will be authenticated later in the chain. They are authenticated by either This is the authentication flow for unauthenticated users (
This is the authentication flow for authenticated users (
In summary:
As I said, I currently actively use this approach. https://gw2auth.com uses exactly this including the change in the linked PR to make this work. |
@its-felix Thanks for the detailed explanation. I now understand your flow and the issue you are having. I'll check to see if we can move the I understand you don't want the underlying container to manage sessions but have you considered delegating this to Spring Session? Also, Spring Security provides session management protection out-of-the-box that you won't be able to take advantage of with the custom solution you have implemented. I'm assuming you're aware of this but wanted to point it out either way. |
Hi @jgrandja , thanks for getting back quickly :) I decided to implement this on my own because I want to migrate some of the non oauth2 Server related endpoints of this application to AWS Lambda which will have to validate sessions on its own. In the future my infrastructure will look roughly like this: gw2auth.com -> |
@its-felix According to Section 4.1.1 Authorization Request:
The current flow has been implemented to spec. IMO, this makes sense because there is no point in authenticating the resource owner if the authorization request is invalid. Instead, the validation should occur first to enable a fail-fast approach and short-circuit the request if it's invalid. Furthermore, when the authorization request is valid but the resource owner is not authenticated, the valid authorization request can be saved in the This is the main reason the I'm curious on...
Can you provide more info on the type of requests? Are these requests to your API endpoints? If so, can you provide details on the type of API it is. FYI, another customization you can apply to move you ahead with your issue is providing a custom OAuth2AuthorizationServerConfigurer<HttpSecurity> authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer<>();
authorizationServerConfigurer
.authorizationEndpoint(authorizationEndpoint ->
authorizationEndpoint
.authorizationRequestConverter(customAuthorizationRequestConverter) The default |
@its-felix In addition to the suggestion above:
You can also configure a custom The custom I'm going to close this issue and associated PR as the 2 solutions provided will move you forward. |
This is for all subsequent requests (including "UI" requests returning HTML pages). Since the My application doesn't provide any API endpoints which are meant for "external" use (beyond the oauth2-authorization-server endpoints). All API endpoints are exclusively to be used by the applications UI code from Javascript. Thank you for the hints! I will go forward with the suggested approach using a custom |
Just FYI and for future readers, I was able to implement the expected behavior using the proposed solution (custom I'm now using the latest release (0.3.1) again instead of the temporary custom build. |
Excellent! Great to hear it's all working out for you @its-felix 👍 |
Expected Behavior
When using stateless sessions ("no session") it should still be possible for Authentication Filters to take place before the
OAuth2AuthorizationEndpointFilter
expects an authentication.Current Behavior
The
OAuth2AuthorizationEndpointFilter
is added beforeAbstractPreAuthenticatedProcessingFilter
.Whenever this filter is executed without already having an authenticated user (which is always the case at this point in the chain for stateless sessions) it will proceed the chain, expecting to be called again from a new http request once the user logged in.
In the case of stateless sessions, the chain will later authenticate the user but the
OAuth2AuthorizationEndpointFilter
will not be executed again.Context
I would like to completely get rid of sessions in my authorization server.
I have set the
SessionCreationPolicy
toSTATELESS
. Users login usingoauth2Login
and theoauth2Login.successHandler
sends a cookie containing a signed JWT containing the users information.For all requests after an login I have enabled the
oauth2ResourceServer
with a customBearerTokenResolver
(to read the JWT from the cookie). This leads to the problem described above when trying to use it together with the authorization server.Some (WIP) code for context:
Authorization Server Config
Other SecurityConfigurations
The text was updated successfully, but these errors were encountered: