-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Springs AuditorAware capabilities (#368)
- Loading branch information
1 parent
6d0090c
commit dd77c7c
Showing
11 changed files
with
192 additions
and
22 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/SpringSecurityAuditorAware.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,29 @@ | ||
package ca.bc.gov.nrs.wfprev; | ||
|
||
import org.springframework.data.domain.AuditorAware; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
|
||
@Component | ||
public class SpringSecurityAuditorAware implements AuditorAware<String> { | ||
|
||
@Override | ||
public Optional<String> getCurrentAuditor() { | ||
return Optional.ofNullable(SecurityContextHolder.getContext()) | ||
.map(context -> context.getAuthentication()) | ||
.filter(Authentication::isAuthenticated) | ||
.map(authentication -> { | ||
Object principal = authentication.getPrincipal(); | ||
if (principal instanceof DefaultOAuth2AuthenticatedPrincipal) { | ||
// Extract username or preferred identifier | ||
DefaultOAuth2AuthenticatedPrincipal oauthPrincipal = (DefaultOAuth2AuthenticatedPrincipal) principal; | ||
return (String) oauthPrincipal.getAttribute("preferred_username"); // Adjust key to match your provider | ||
} | ||
throw new IllegalStateException("Principal is not of type DefaultOAuth2AuthenticatedPrincipal"); | ||
}); | ||
} | ||
} |
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
21 changes: 14 additions & 7 deletions
21
server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/CheckTokenControllerTest.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 |
---|---|---|
@@ -1,39 +1,46 @@ | ||
package ca.bc.gov.nrs.wfprev; | ||
|
||
import ca.bc.gov.nrs.wfprev.common.controllers.CheckTokenController; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.data.domain.AuditorAware; | ||
import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import org.testcontainers.utility.TestcontainersConfiguration; | ||
|
||
import ca.bc.gov.nrs.wfprev.common.controllers.CheckTokenController; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(CheckTokenController.class) | ||
@Import({SecurityConfig.class, TestcontainersConfiguration.class}) | ||
@MockBean(JpaMetamodelMappingContext.class) | ||
class CheckTokenControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean(name = "springSecurityAuditorAware") // Changed to match the expected bean name | ||
private AuditorAware<String> auditorAware; | ||
|
||
@Test | ||
@WithMockUser | ||
void testToken_MissingAuthorizationHeader() throws Exception { | ||
mockMvc.perform(get("/check/checkToken") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isUnauthorized()); | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
void testToken_NullTokenInHeader() throws Exception { | ||
mockMvc.perform(get("/check/checkToken") | ||
.header("Authorization", "Bearer ") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.header("Authorization", "Bearer ") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isUnauthorized()); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.