refactor: rename keycloakId to authentikId in codebase
Some checks failed
CI / Get Changed Files (pull_request) Successful in 6s
CI / prettier (pull_request) Failing after 46s
CI / Checkstyle Main (pull_request) Successful in 49s
CI / eslint (pull_request) Failing after 1m2s
CI / test-build (pull_request) Failing after 1m9s

This commit is contained in:
Jan K9f 2025-04-02 15:49:58 +02:00
parent 7eebd12699
commit 8317349507
Signed by: jank
GPG key ID: B9F475106B20F144
12 changed files with 270 additions and 48 deletions

View file

@ -12,7 +12,7 @@ Content-Type: application/json
Authorization: Bearer {{token}}
{
"keycloakId": "52cc0208-a3bd-4367-94c5-0404b016a003",
"authentikId": "52cc0208-a3bd-4367-94c5-0404b016a003",
"username": "john.doe"
}

View file

@ -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();
}

View file

@ -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();

View file

@ -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;
}

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -45,15 +45,15 @@ public class UserControllerTest {
@BeforeEach
void setUp() {
getUserDto = new GetUserDto();
getUserDto.setKeycloakId(TEST_ID);
getUserDto.setAuthentikId(TEST_ID);
getUserDto.setUsername("testuser");
testUser = new UserEntity();
testUser.setKeycloakId(TEST_ID);
testUser.setAuthentikId(TEST_ID);
testUser.setUsername("testuser");
createUserDto = new CreateUserDto();
createUserDto.setKeycloakId(TEST_ID);
createUserDto.setAuthentikId(TEST_ID);
createUserDto.setUsername("testuser");
}
@ -64,7 +64,7 @@ public class UserControllerTest {
mockMvc.perform(get("/user/" + TEST_ID))
.andExpect(status().isOk())
.andExpect(jsonPath("$.keycloakId").value(TEST_ID))
.andExpect(jsonPath("$.authentikId").value(TEST_ID))
.andExpect(jsonPath("$.username").value("testuser"));
}
@ -85,7 +85,7 @@ public class UserControllerTest {
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(createUserDto)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.keycloakId").value(TEST_ID))
.andExpect(jsonPath("$.authentikId").value(TEST_ID))
.andExpect(jsonPath("$.username").value("testuser"));
}
@ -107,7 +107,7 @@ public class UserControllerTest {
mockMvc.perform(get("/user")
.header("Authorization", AUTH_TOKEN))
.andExpect(status().isOk())
.andExpect(jsonPath("$.keycloakId").value(TEST_ID))
.andExpect(jsonPath("$.authentikId").value(TEST_ID))
.andExpect(jsonPath("$.username").value("testuser"));
}