Skip to content

Commit

Permalink
feat: #185 new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KartVen committed Nov 18, 2024
1 parent cbb41bf commit 1b97824
Show file tree
Hide file tree
Showing 101 changed files with 966 additions and 1,921 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import pl.sknikod.kodemycommons.security.configuration.JwtConfiguration;

Expand All @@ -26,14 +29,14 @@ public class JwtAuthorizationFilter extends OncePerRequestFilter {

public JwtAuthorizationFilter(
List<String> permitPaths,
JwtConfiguration.JwtProperties jwtProperties
JwtProvider jwtProvider
) {
this.notFilterMatchers = permitPaths.stream().map(AntPathRequestMatcher::new).toList();
this.jwtProvider = new JwtProvider(jwtProperties);
this.jwtProvider = jwtProvider;
}

public JwtAuthorizationFilter(JwtConfiguration.JwtProperties jwtProperties) {
this(Collections.emptyList(), jwtProperties);
public JwtAuthorizationFilter(JwtProvider jwtProvider) {
this(Collections.emptyList(), jwtProvider);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@Getter
@RequiredArgsConstructor
public class JwtConfiguration {
private final JwtProperties jwtProperties;

@Getter
@Setter
@Component
Expand Down
7 changes: 5 additions & 2 deletions kodemy-auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ dependencies {

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'org.testcontainers:junit-jupiter:1.19.8'
testImplementation 'org.springframework.cloud:spring-cloud-contract-wiremock'
testImplementation 'org.springframework.cloud:spring-cloud-stream'
testImplementation 'org.springframework.cloud:spring-cloud-stream-test-binder'
testImplementation 'org.testcontainers:junit-jupiter:1.20.0'
constraints {
testImplementation('org.apache.commons:commons-compress:1.26.2') {
because '<1.25.x vulnerability'
}
}
testImplementation 'org.testcontainers:postgresql:1.19.8'
testImplementation 'org.testcontainers:postgresql:1.20.0'

testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemyauth.infrastructure.dao.RefreshTokenDao;
import pl.sknikod.kodemyauth.infrastructure.store.RefreshTokenStore;
import pl.sknikod.kodemyauth.infrastructure.module.auth.LogoutService;
import pl.sknikod.kodemyauth.infrastructure.module.auth.handler.LogoutRequestHandler;
import pl.sknikod.kodemyauth.infrastructure.module.auth.handler.LogoutSuccessHandler;
Expand Down Expand Up @@ -97,71 +97,21 @@ public ServletExceptionHandler servletExceptionHandler(ObjectMapper objectMapper
return new ServletExceptionHandler(objectMapper);
}

@Bean
public JwtConfiguration.JwtProperties jwtProperties() {
return new JwtConfiguration.JwtProperties();
}

@Bean
public JwtAuthorizationFilter jwtAuthorizationFilter(
OAuth2EndpointsProperties oAuth2EndpointsProperties,
JwtConfiguration.JwtProperties jwtProperties
JwtProvider jwtProvider
) {
final var permitPaths = List.of(
oAuth2EndpointsProperties.authorize + OAuth2Constant.OAUTH2_PROVIDER_SUFFIX,
oAuth2EndpointsProperties.callback + OAuth2Constant.OAUTH2_PROVIDER_SUFFIX
);
return new JwtAuthorizationFilter(permitPaths, jwtProperties);
}

@Bean
public OAuth2AuthorizationRequestRepository oAuth2AuthorizeRequestResolver(
StringRedisTemplate stringRedisTemplate
) {
return new OAuth2AuthorizationRequestRepository(stringRedisTemplate);
}

@Bean
public JwtProvider jwtProvider(JwtConfiguration.JwtProperties jwtProperties) {
return new JwtProvider(jwtProperties);
}

@Bean
public OAuth2LoginSuccessHandler oAuth2SuccessProcessHandler(
JwtProvider jwtProvider,
@Value("${app.security.oauth2.route.front}") String frontRoute,
@Value("${app.security.oauth2.endpoints.redirect}") String redirectEndpoint,
RefreshTokenDao refreshTokenRepositoryHandler,
RouteRedirectStrategy routeRedirectStrategy
) {
var redirectPath = (frontRoute.equals("/") ? null : frontRoute) + redirectEndpoint;
final var handler = new OAuth2LoginSuccessHandler(jwtProvider, redirectPath, refreshTokenRepositoryHandler);
handler.setRedirectStrategy(routeRedirectStrategy);
return handler;
}

@Bean
public OAuth2LoginFailureHandler oAuth2FailureProcessHandler(
RouteRedirectStrategy routeRedirectStrategy,
@Value("${app.security.oauth2.route.front}") String frontRoute,
@Value("${app.security.oauth2.endpoints.redirect}") String redirectEndpoint
) {
var redirectPath = (frontRoute.equals("/") ? null : frontRoute) + redirectEndpoint;
final var handler = new OAuth2LoginFailureHandler(redirectPath);
handler.setRedirectStrategy(routeRedirectStrategy);
return handler;
}

@Bean
public LogoutRequestHandler logoutRequestHandler(
LogoutService logoutService, JwtProvider jwtProvider) {
return new LogoutRequestHandler(logoutService, jwtProvider);
return new JwtAuthorizationFilter(permitPaths, jwtProvider);
}

@Bean
public LogoutSuccessHandler logoutSuccessHandler(
@Value("${network.route.gateway}") String gatewayRoute) {
return new LogoutSuccessHandler(gatewayRoute);
public JwtProvider jwtProvider(JwtConfiguration jwtConfiguration) {
return new JwtProvider(jwtConfiguration.getJwtProperties());
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.zalando.logbook.spring.LogbookClientHttpRequestInterceptor;
import pl.sknikod.kodemyauth.infrastructure.module.oauth2.util.OAuth2RestTemplate;

import java.util.Collections;

@Configuration
@Slf4j
public class WebConfiguration {
@Getter
@Setter
@Component
@NoArgsConstructor
@ConfigurationProperties(prefix = "network.databus")
public static class LanNetworkProperties {
private int connectTimeoutMs;
private int readTimeoutMs;
}

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
public RestTemplate restTemplate(
RestTemplateBuilder restTemplateBuilder, LogbookClientHttpRequestInterceptor logbookInterceptor
) {
restTemplateBuilder.requestFactory(() -> {
var requestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build());
return new BufferingClientHttpRequestFactory(requestFactory);
});
restTemplateBuilder.additionalInterceptors(Collections.singletonList(logbookInterceptor));
return restTemplateBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemyauth.infrastructure.dao.RefreshTokenDao;
import pl.sknikod.kodemyauth.infrastructure.store.RefreshTokenStore;
import pl.sknikod.kodemycommons.exception.InternalError500Exception;
import pl.sknikod.kodemycommons.security.UserPrincipal;

Expand All @@ -13,7 +13,7 @@
@Component
@RequiredArgsConstructor
public class LogoutService {
private final RefreshTokenDao refreshTokenRepositoryHandler;
private final RefreshTokenStore refreshTokenRepositoryHandler;

public Boolean logout(UserPrincipal userPrincipal, UUID bearerJti) {
return refreshTokenRepositoryHandler.invalidateByUserIdAnfBearerJti(userPrincipal.getId(), bearerJti)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemyauth.configuration.SecurityConfiguration;
import pl.sknikod.kodemyauth.infrastructure.dao.RefreshTokenDao;
import pl.sknikod.kodemyauth.infrastructure.database.RefreshToken;
import pl.sknikod.kodemyauth.infrastructure.database.Role;
import pl.sknikod.kodemyauth.infrastructure.database.RoleRepository;
import pl.sknikod.kodemyauth.infrastructure.database.User;
import pl.sknikod.kodemyauth.infrastructure.module.auth.model.RefreshTokensResponse;
import pl.sknikod.kodemyauth.infrastructure.store.RefreshTokenStore;
import pl.sknikod.kodemycommons.exception.InternalError500Exception;
import pl.sknikod.kodemycommons.security.JwtProvider;

Expand All @@ -26,12 +26,11 @@
@RequiredArgsConstructor
public class RefreshTokensService {
private final RoleRepository roleRepository;
private final RefreshTokenDao refreshTokenDao;
private final RefreshTokenStore refreshTokenStore;
private final JwtProvider jwtProvider;
private final SecurityConfiguration.RoleProperties roleProperties;

public RefreshTokensResponse refresh(UUID refresh, UUID bearerJti) {
return refreshTokenDao.findByTokenAndBearerJti(refresh, bearerJti)
return refreshTokenStore.findByTokenAndBearerJti(refresh, bearerJti)
.flatMapTry(this::generateTokensAndInvalidate)
.map(tokens -> new RefreshTokensResponse(
tokens._2.getToken().toString(), tokens._1.value()))
Expand All @@ -40,11 +39,11 @@ public RefreshTokensResponse refresh(UUID refresh, UUID bearerJti) {

private Try<Tuple2<JwtProvider.Token, RefreshToken>> generateTokensAndInvalidate(RefreshToken refreshToken) {
return Try.of(() -> jwtProvider.generateUserToken(map(refreshToken.getUser())))
.flatMapTry(bearerToken -> refreshTokenDao
.flatMapTry(bearerToken -> refreshTokenStore
.createAndGet(refreshToken.getUser(), bearerToken.id())
.map(newRefreshToken -> Tuple.of(bearerToken, newRefreshToken))
.onFailure(th -> log.error("Error during tokens generation", th)))
.peek(unused -> refreshTokenDao.invalidate(refreshToken));
.peek(unused -> refreshTokenStore.invalidate(refreshToken));
}

private JwtProvider.Input map(User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemyauth.infrastructure.module.auth.LogoutService;
import pl.sknikod.kodemycommons.exception.InternalError500Exception;
import pl.sknikod.kodemycommons.security.AuthFacade;
import pl.sknikod.kodemycommons.security.JwtProvider;

import java.util.UUID;

@Component
@Slf4j
@RequiredArgsConstructor
public class LogoutRequestHandler implements LogoutHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Null;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@RequiredArgsConstructor
@Component
public class LogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
private final String gatewayRoute;

public LogoutSuccessHandler(@Value("${network.route.gateway}") String gatewayRoute) {
this.gatewayRoute = gatewayRoute;
}

@Override
public void onLogoutSuccess(
HttpServletRequest request,
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 1b97824

Please sign in to comment.