-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from peter-szrnka/211-gms-72-keycloak-sso-fea…
…ture GMS-72 keycloak sso feature
- Loading branch information
Showing
145 changed files
with
4,139 additions
and
1,503 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docker run --name keycloak -p 7000:8080 -d -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:24.0.1 start-dev |
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
13 changes: 13 additions & 0 deletions
13
code/gms-backend/src/main/java/io/github/gms/auth/VerificationService.java
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,13 @@ | ||
package io.github.gms.auth; | ||
|
||
import io.github.gms.auth.model.AuthenticationResponse; | ||
import io.github.gms.common.dto.LoginVerificationRequestDto; | ||
|
||
/** | ||
* @author Peter Szrnka | ||
* @since 1.0 | ||
*/ | ||
public interface VerificationService { | ||
|
||
AuthenticationResponse verify(LoginVerificationRequestDto dto); | ||
} |
72 changes: 72 additions & 0 deletions
72
code/gms-backend/src/main/java/io/github/gms/auth/VerificationServiceImpl.java
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,72 @@ | ||
package io.github.gms.auth; | ||
|
||
import dev.samstevens.totp.code.CodeVerifier; | ||
import io.github.gms.auth.model.AuthenticationResponse; | ||
import io.github.gms.auth.model.GmsUserDetails; | ||
import io.github.gms.auth.service.TokenGeneratorService; | ||
import io.github.gms.auth.types.AuthResponsePhase; | ||
import io.github.gms.common.dto.LoginVerificationRequestDto; | ||
import io.github.gms.common.enums.JwtConfigType; | ||
import io.github.gms.functions.user.UserConverter; | ||
import io.github.gms.functions.user.UserLoginAttemptManagerService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Map; | ||
|
||
import static io.github.gms.common.util.Constants.CONFIG_AUTH_TYPE_NOT_KEYCLOAK_SSO; | ||
|
||
/** | ||
* @author Peter Szrnka | ||
* @since 1.0 | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
@Profile(CONFIG_AUTH_TYPE_NOT_KEYCLOAK_SSO) | ||
public class VerificationServiceImpl implements VerificationService { | ||
|
||
private final UserAuthService userAuthService; | ||
private final UserConverter converter; | ||
private final CodeVerifier verifier; | ||
private final UserLoginAttemptManagerService userLoginAttemptManagerService; | ||
private final TokenGeneratorService tokenGeneratorService; | ||
|
||
@Override | ||
public AuthenticationResponse verify(LoginVerificationRequestDto dto) { | ||
try { | ||
if (userLoginAttemptManagerService.isBlocked(dto.getUsername())) { | ||
return AuthenticationResponse.builder() | ||
.phase(AuthResponsePhase.BLOCKED) | ||
.build(); | ||
} | ||
|
||
GmsUserDetails userDetails = (GmsUserDetails) userAuthService.loadUserByUsername(dto.getUsername()); | ||
|
||
if (Boolean.FALSE.equals(userDetails.getAccountNonLocked())) { // User locked in LDAP | ||
return AuthenticationResponse.builder() | ||
.phase(AuthResponsePhase.BLOCKED) | ||
.build(); | ||
} | ||
|
||
if (!verifier.isValidCode(userDetails.getMfaSecret(), dto.getVerificationCode())) { | ||
userLoginAttemptManagerService.updateLoginAttempt(dto.getUsername()); | ||
return AuthenticationResponse.builder().phase(AuthResponsePhase.FAILED).build(); | ||
} | ||
|
||
Map<JwtConfigType, String> authenticationDetails = tokenGeneratorService.getAuthenticationDetails(userDetails); | ||
|
||
// Verify codes | ||
return AuthenticationResponse.builder() | ||
.currentUser(converter.toUserInfoDto(userDetails, false)) | ||
.phase(AuthResponsePhase.COMPLETED) | ||
.token(authenticationDetails.get(JwtConfigType.ACCESS_JWT)) | ||
.refreshToken(authenticationDetails.get(JwtConfigType.REFRESH_JWT)) | ||
.build(); | ||
} catch (Exception e) { | ||
return AuthenticationResponse.builder() | ||
.phase(AuthResponsePhase.FAILED) | ||
.build(); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
code/gms-backend/src/main/java/io/github/gms/auth/config/AbstractSecurityConfig.java
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,67 @@ | ||
package io.github.gms.auth.config; | ||
|
||
import io.github.gms.common.filter.SecureHeaderInitializerFilter; | ||
import org.springframework.boot.autoconfigure.security.servlet.PathRequest; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.security.config.Customizer; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer; | ||
import org.springframework.security.config.annotation.web.configurers.HttpBasicConfigurer; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Peter Szrnka | ||
* @since 1.0 | ||
*/ | ||
public abstract class AbstractSecurityConfig { | ||
|
||
private static final String[] FILTER_URL = new String[]{"/", "/system/status", "/healthcheck", "/setup/**", | ||
"/login", "/authenticate", "/verify", "/logoutUser", "/api/**", "/info/me", "/actuator/**", | ||
"/gms-app/**", "/favicon.ico", "/assets/**", "/index.html**", "/*.js**", "/*.css**", "/*.json**", | ||
"/manifest.webmanifest", "/reset_password"}; | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http, | ||
AuthenticationEntryPoint authenticationEntryPoint, | ||
SecureHeaderInitializerFilter secureHeaderInitializerFilter) throws Exception { | ||
http | ||
.cors(cors -> Customizer.withDefaults()) | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.exceptionHandling(e -> e.authenticationEntryPoint(authenticationEntryPoint)) | ||
.sessionManagement(sessionManagement -> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
.authorizeHttpRequests(authorizeHttpRequest -> | ||
authorizeHttpRequest.requestMatchers(FILTER_URL).permitAll() | ||
.requestMatchers(FILTER_URL).permitAll() | ||
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll().anyRequest() | ||
.authenticated() | ||
); | ||
|
||
http.addFilterBefore(secureHeaderInitializerFilter, UsernamePasswordAuthenticationFilter.class); | ||
http.formLogin(FormLoginConfigurer<HttpSecurity>::disable); | ||
http.httpBasic(HttpBasicConfigurer<HttpSecurity>::disable); | ||
|
||
return http.build(); | ||
} | ||
|
||
@Bean | ||
CorsConfigurationSource corsConfigurationSource() { | ||
CorsConfiguration configuration = new CorsConfiguration(); | ||
configuration.setAllowedOrigins(List.of("http://localhost:4200")); | ||
configuration.setAllowedMethods(List.of("*")); | ||
configuration.setAllowedHeaders(List.of("*")); | ||
configuration.setAllowCredentials(true); | ||
|
||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
source.registerCorsConfiguration("/**", configuration); | ||
return source; | ||
} | ||
} |
Oops, something went wrong.