refactor: rename keycloakId to authentikId in codebase
Some checks failed
Some checks failed
This commit is contained in:
parent
7eebd12699
commit
8317349507
12 changed files with 270 additions and 48 deletions
|
@ -48,7 +48,7 @@ public class DepositController {
|
|||
Stripe.apiKey = stripeKey;
|
||||
|
||||
KeycloakUserDto userData = getKeycloakUserInfo(token);
|
||||
Optional<UserEntity> optionalUserEntity = this.userRepository.findOneByKeycloakId(userData.getSub());
|
||||
Optional<UserEntity> optionalUserEntity = this.userRepository.findOneByAuthentikId(userData.getSub());
|
||||
|
||||
SessionCreateParams params = SessionCreateParams.builder()
|
||||
.addLineItem(SessionCreateParams.LineItem.builder()
|
||||
|
@ -76,7 +76,7 @@ public class DepositController {
|
|||
private KeycloakUserDto getKeycloakUserInfo(String token) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Authorization", token);
|
||||
ResponseEntity<KeycloakUserDto> response = this.restTemplate.exchange("http://localhost:9090/realms/LF12/protocol/openid-connect/userinfo", HttpMethod.GET, new HttpEntity<>(headers), KeycloakUserDto.class);
|
||||
ResponseEntity<KeycloakUserDto> response = this.restTemplate.exchange("https://oauth.simonis.lol/application/o/userinfo/", HttpMethod.GET, new HttpEntity<>(headers), KeycloakUserDto.class);
|
||||
|
||||
return response.getBody();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package de.szut.casino.security;
|
|||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
@ -19,6 +20,8 @@ public class SecurityConfig {
|
|||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.cors(Customizer.withDefaults())
|
||||
.csrf(csrf -> csrf.disable())
|
||||
.authorizeHttpRequests(auth -> {
|
||||
auth.requestMatchers("/swagger/**", "/swagger-ui/**", "/health").permitAll()
|
||||
.anyRequest().authenticated();
|
||||
|
@ -35,7 +38,7 @@ public class SecurityConfig {
|
|||
CorsConfiguration configuration = new CorsConfiguration();
|
||||
configuration.setAllowedOrigins(List.of("http://localhost:4200"));
|
||||
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
|
||||
configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));
|
||||
configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token", "Access-Control-Allow-Origin"));
|
||||
configuration.setExposedHeaders(List.of("x-auth-token"));
|
||||
configuration.setAllowCredentials(true);
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
|
|
|
@ -19,14 +19,14 @@ public class UserEntity {
|
|||
@GeneratedValue
|
||||
private Long id;
|
||||
@Column(unique = true)
|
||||
private String keycloakId;
|
||||
private String authentikId; // Changed from keycloakId to authentikId
|
||||
private String username;
|
||||
|
||||
@Column(precision = 19, scale = 2)
|
||||
private BigDecimal balance;
|
||||
|
||||
public UserEntity(String keycloakId, String username, BigDecimal balance) {
|
||||
this.keycloakId = keycloakId;
|
||||
public UserEntity(String authentikId, String username, BigDecimal balance) {
|
||||
this.authentikId = authentikId;
|
||||
this.username = username;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
|
|
@ -9,10 +9,11 @@ import java.math.BigDecimal;
|
|||
@Service
|
||||
public class UserMappingService {
|
||||
public GetUserDto mapToGetUserDto(UserEntity user) {
|
||||
return new GetUserDto(user.getKeycloakId(), user.getUsername(), user.getBalance());
|
||||
return new GetUserDto(user.getAuthentikId(), user.getUsername(), user.getBalance());
|
||||
}
|
||||
|
||||
public UserEntity mapToUserEntity(CreateUserDto createUserDto) {
|
||||
return new UserEntity(createUserDto.getAuthentikId(), createUserDto.getUsername(), BigDecimal.ZERO); }
|
||||
return new UserEntity(createUserDto.getAuthentikId(), createUserDto.getUsername(), BigDecimal.ZERO);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,8 +8,8 @@ import java.util.Optional;
|
|||
|
||||
@Service
|
||||
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
||||
@Query("SELECT u FROM UserEntity u WHERE u.keycloakId = ?1")
|
||||
Optional<UserEntity> findOneByKeycloakId(String keycloakId);
|
||||
@Query("SELECT u FROM UserEntity u WHERE u.authentikId = ?1")
|
||||
Optional<UserEntity> findOneByAuthentikId(String authentikId);
|
||||
|
||||
boolean existsByKeycloakId(String keycloakId);
|
||||
boolean existsByAuthentikId(String authentikId);
|
||||
}
|
||||
|
|
|
@ -31,33 +31,42 @@ public class UserService {
|
|||
return user;
|
||||
}
|
||||
|
||||
public GetUserDto getUser(String keycloakId) {
|
||||
Optional<UserEntity> user = this.userRepository.findOneByKeycloakId(keycloakId);
|
||||
public GetUserDto getUser(String authentikId) {
|
||||
Optional<UserEntity> user = this.userRepository.findOneByAuthentikId(authentikId);
|
||||
|
||||
return user.map(userEntity -> mappingService.mapToGetUserDto(userEntity)).orElse(null);
|
||||
}
|
||||
|
||||
public GetUserDto getCurrentUser(String token) {
|
||||
KeycloakUserDto userData = getKeycloakUserInfo(token);
|
||||
KeycloakUserDto userData = getAuthentikUserInfo(token);
|
||||
|
||||
if (userData == null) {
|
||||
return null;
|
||||
}
|
||||
Optional<UserEntity> user = this.userRepository.findOneByKeycloakId(userData.getSub());
|
||||
Optional<UserEntity> user = this.userRepository.findOneByAuthentikId(userData.getSub());
|
||||
|
||||
return user.map(userEntity -> mappingService.mapToGetUserDto(userEntity)).orElse(null);
|
||||
|
||||
}
|
||||
|
||||
private KeycloakUserDto getKeycloakUserInfo(String token) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Authorization", token);
|
||||
ResponseEntity<KeycloakUserDto> response = this.http.exchange("https://oauth.simonis.lol/application/o/userinfo/", HttpMethod.GET, new HttpEntity<>(headers), KeycloakUserDto.class);
|
||||
|
||||
return response.getBody();
|
||||
private KeycloakUserDto getAuthentikUserInfo(String token) {
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Authorization", token);
|
||||
ResponseEntity<KeycloakUserDto> response = this.http.exchange(
|
||||
"https://oauth.simonis.lol/application/o/userinfo/",
|
||||
HttpMethod.GET,
|
||||
new HttpEntity<>(headers),
|
||||
KeycloakUserDto.class
|
||||
);
|
||||
|
||||
return response.getBody();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error fetching user info from Authentik: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean exists(String keycloakId) {
|
||||
return userRepository.existsByKeycloakId(keycloakId);
|
||||
public boolean exists(String authentikId) {
|
||||
return userRepository.existsByAuthentikId(authentikId);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue