Skip to content

Commit

Permalink
use new KubetoolsSecurityConfigurer
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Apr 3, 2023
1 parent f1e3f1e commit 712c7ca
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
2 changes: 1 addition & 1 deletion server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<dependency>
<groupId>io.github.mucsi96</groupId>
<artifactId>kubetools</artifactId>
<version>1.16-SNAPSHOT</version>
<version>1.17-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package mucsi96.traininglog.configuration;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizationCodeGrantFilter;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;

import io.github.mucsi96.kubetools.core.FilterChainExceptionHandlerFilter;
import io.github.mucsi96.kubetools.security.AutheliaHeaderAuthenticationFilter;
import io.github.mucsi96.kubetools.security.KubetoolsSecurityConfigurer;
import mucsi96.traininglog.oauth.AccessTokenResponseClient;
import mucsi96.traininglog.oauth.AuthorizedClientManager;
import mucsi96.traininglog.oauth.AuthorizedClientRepository;
Expand All @@ -33,24 +25,12 @@ public class SecurityConfiguration {
@Bean
SecurityFilterChain securityFilterChain(
HttpSecurity http,
@Qualifier("handlerExceptionResolver") HandlerExceptionResolver resolver,
KubetoolsSecurityConfigurer kubetoolsSecurityConfigurer,
AccessTokenResponseClient accessTokenResponseClient,
RedirectToHomeFilter redirectToHomeFilter,
AuthenticationManager authenticationManager)
throws Exception {

return http
.sessionManagement(configurer -> configurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.anonymous(AbstractHttpConfigurer::disable)
.csrf(AbstractHttpConfigurer::disable)
.headers(configurer -> configurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
.formLogin(AbstractHttpConfigurer::disable)
.logout(AbstractHttpConfigurer::disable)
RedirectToHomeFilter redirectToHomeFilter) throws Exception {
return kubetoolsSecurityConfigurer.configure(http)
.oauth2Client(configurer -> configurer
.authorizationCodeGrant(customizer -> customizer.accessTokenResponseClient(accessTokenResponseClient)))
.addFilter(new AutheliaHeaderAuthenticationFilter(authenticationManager))
.addFilterBefore(new FilterChainExceptionHandlerFilter(resolver),
AbstractPreAuthenticatedProcessingFilter.class)
.addFilterBefore(redirectToHomeFilter, OAuth2AuthorizationCodeGrantFilter.class)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ public void returns_not_authorized_if_no_preauth_headers_are_sent() throws Excep
}

@Test
public void returns_forbidden_if_bearer_token_is_not_sent() throws Exception {
public void returns_not_authorized_if_authorized_client_is_not_found() throws Exception {
MockHttpServletResponse response = mockMvc
.perform(
get("/withings/weight")
.headers(getAuthHeaders("guest")))
.andReturn().getResponse();

assertThat(response.getStatus()).isEqualTo(403);
assertThat(response.getStatus()).isEqualTo(401);
assertThat(JsonPath.parse(response.getContentAsString()).read("$._links.oauth2Login.href", String.class))
.isEqualTo("https://training-log.com:3000/api/oauth2/authorization/withings-client");
.isEqualTo("http://localhost/oauth2/authorization/withings-client");
}

@Test
Expand Down

0 comments on commit 712c7ca

Please sign in to comment.