Skip to content

Misconfigured OAuth2LoginAuthenticationFilter when combining OAuth2 login and OAuth2 client configuration #16105

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
BWohlbrecht opened this issue Nov 15, 2024 · 2 comments
Assignees
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: bug A general bug
Milestone

Comments

@BWohlbrecht
Copy link

Describe the bug
When configuring the SecurityFilterChain with both oAuth2Login and oAuth2Client sections, the resulting OAuth2LoginAuthenticationFilter is configured with the ClientRegistrationRepository and the OAuth2AuthorizedClientRepository specified in the oAuth2Client section instead of the corresponding instances in the oAuth2Login section.

The same seems to apply to other configured filters.

To Reproduce
Configure a SecurityFilterChain bean with oAuth2Login providing a custom ClientRegistrationRepository and OAuth2AuthorizedClientRepository. The correct beans are injected in the OAuth2LoginAuthenticationFilter on creation.

Then add the oAuth2Client section with different beans for ClientRegistrationRepository and OAuth2AuthorizedClientRepository.
Now OAuth2LoginAuthenticationFilter is constructed with the beans from the oAuth2Client section despite other instances being provided in the oAuth2Login section.

Expected behavior
The ClientRegistrationRepository and OAuth2AuthorizedClientRepository in the oAuth2Login section should always take precedent when constructing the OAuth2LoginAuthenticationFilter even when different instances are provided in the oAuth2Client section.

This is important for use cases where some ClientRegistrations are dedicated to login and others for authorizing use of a web API.

Sample
https://github.com/BWohlbrecht/spring-security-oauth2-sample

@BWohlbrecht BWohlbrecht added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Nov 15, 2024
@BWohlbrecht BWohlbrecht changed the title [OAuth2] Misconfigured OAuth2LoginAuthenticationFilter when combining OAuth2 Login and OAuth2 client configuration [OAuth2] Misconfigured OAuth2LoginAuthenticationFilter when combining OAuth2 login and OAuth2 client configuration Nov 15, 2024
@BWohlbrecht
Copy link
Author

Just to make it clear:

Using this bean definition for the SecurityFilterChain

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http,
		ClientRegistrationRepository loginRegistrationRepository,
		OAuth2AuthorizedClientRepository loginClientRepository
	) throws Exception {
	return http
		.oauth2Login(login -> {
			login
				.clientRegistrationRepository(loginRegistrationRepository)
				.authorizedClientRepository(loginClientRepository);
		})
		.build();
}

the OAuth2LoginAuthenticationFilter is constructed correctly with the supplied ClientRegistrationRepository and OAuth2AuthorizedClientRepository.

However when adding the following section

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http,
		ClientRegistrationRepository loginRegistrationRepository,
		OAuth2AuthorizedClientRepository loginClientRepository,

		ClientRegistrationRepository authorizationRegistrationRepository,
		OAuth2AuthorizedClientRepository authorizationClientRepository
	) throws Exception {
	return http
		.oauth2Login(login -> {
			login
				.clientRegistrationRepository(loginRegistrationRepository)
				.authorizedClientRepository(loginClientRepository);
		})
		.oauth2Client(client -> {
			client
				.clientRegistrationRepository(authorizationRegistrationRepository)
				.authorizedClientRepository(authorizationClientRepository);
		})
		.build();
}

the OAuth2LoginAuthenticationFilter is constructed with the ClientRegistrationRepository and OAuth2AuthorizedClientRepository supplied in the oAuth2Client section, while the instances from the oAuth2Login section are ignored.

@sjohnr sjohnr self-assigned this Nov 20, 2024
@sjohnr sjohnr added the in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) label Nov 20, 2024
@sjohnr sjohnr added this to the 6.2.9 milestone Nov 20, 2024
@sjohnr
Copy link
Member

sjohnr commented Nov 20, 2024

Thanks for reporting this @BWohlbrecht. It looks like both configurers are set up to share objects via builder.setSharedObject(...) which causes them to always resolve the same instances (whichever is set last). We'll look at a fix to use distinct instances, but I will check on why it is using shared objects before proceeding and report back.

@sjohnr sjohnr modified the milestones: 6.2.9, 6.3.6, 6.3.x Dec 12, 2024
@sjohnr sjohnr removed the status: waiting-for-triage An issue we've not yet triaged label Jan 8, 2025
@sjohnr sjohnr changed the title [OAuth2] Misconfigured OAuth2LoginAuthenticationFilter when combining OAuth2 login and OAuth2 client configuration Misconfigured OAuth2LoginAuthenticationFilter when combining OAuth2 login and OAuth2 client configuration Jan 22, 2025
@sjohnr sjohnr closed this as completed in 211fa52 Jan 22, 2025
@sjohnr sjohnr modified the milestones: 6.3.x, 6.3.7 Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants