Skip to content

Commit

Permalink
Disable user management API when OIDC security is used.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Oct 20, 2023
1 parent c0f2664 commit 55c4a48
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
target
node_modules
build
logs
**/generated-sources
**/npm-debug.log
.DS_store
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cz/cvut/kbss/study/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import cz.cvut.kbss.study.security.SecurityConstants;
import cz.cvut.kbss.study.service.ConfigReader;
import cz.cvut.kbss.study.util.ConfigParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -35,6 +37,8 @@
@EnableMethodSecurity
public class SecurityConfig {

private static final Logger LOG = LoggerFactory.getLogger(SecurityConfig.class);

private static final String[] COOKIES_TO_DESTROY = {
SecurityConstants.SESSION_COOKIE_NAME,
SecurityConstants.REMEMBER_ME_COOKIE_NAME,
Expand All @@ -61,6 +65,7 @@ public SecurityConfig(AuthenticationFailureHandler authenticationFailureHandler,

@Bean
public SecurityFilterChain filterChain(HttpSecurity http, ConfigReader config) throws Exception {
LOG.debug("Using internal security mechanisms.");
final AuthenticationManager authManager = buildAuthenticationManager(http);
http.authorizeHttpRequests((auth) -> auth.anyRequest().permitAll())
.cors((auth) -> auth.configurationSource(corsConfigurationSource(config)))
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/cz/cvut/kbss/study/rest/OidcUserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cz.cvut.kbss.study.rest;

import cz.cvut.kbss.study.model.User;
import cz.cvut.kbss.study.security.SecurityConstants;
import cz.cvut.kbss.study.service.UserService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* API for getting basic user info.
* <p>
* Enabled when OIDC security is used.
*/
@ConditionalOnProperty(prefix = "security", name = "provider", havingValue = "oidc")
@RestController
@RequestMapping("/users")
public class OidcUserController extends BaseController {

private final UserService userService;

public OidcUserController(UserService userService) {
this.userService = userService;
}

@PreAuthorize("hasRole('" + SecurityConstants.ROLE_USER + "')")
@GetMapping(value = "/current", produces = MediaType.APPLICATION_JSON_VALUE)
public User getCurrent() {
return userService.getCurrentUser();
}
}
16 changes: 10 additions & 6 deletions src/main/java/cz/cvut/kbss/study/rest/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import cz.cvut.kbss.study.service.InstitutionService;
import cz.cvut.kbss.study.service.UserService;
import cz.cvut.kbss.study.service.security.SecurityUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -27,10 +28,15 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
* User management API.
*
* Enabled when internal security is used.
*/
@ConditionalOnProperty(prefix = "security", name = "provider", havingValue = "internal", matchIfMissing = true)
@RestController
@RequestMapping("/users")
public class UserController extends BaseController {
Expand All @@ -57,9 +63,8 @@ public User getByUsername(@PathVariable("username") String username) {

@PreAuthorize("hasRole('" + SecurityConstants.ROLE_USER + "')")
@GetMapping(value = "/current", produces = MediaType.APPLICATION_JSON_VALUE)
public User getCurrent(Principal principal) {
final String username = principal.getName();
return getByUsername(username);
public User getCurrent() {
return userService.getCurrentUser();
}

@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "')")
Expand All @@ -79,8 +84,7 @@ public ResponseEntity<Void> create(@RequestBody User user) {
"or hasRole('" + SecurityConstants.ROLE_USER + "') and @securityUtils.isMemberOfInstitution(#institutionKey)")
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<User> getUsers(@RequestParam(value = "institution", required = false) String institutionKey) {
final List<User> users = institutionKey != null ? getByInstitution(institutionKey) : userService.findAll();
return users;
return institutionKey != null ? getByInstitution(institutionKey) : userService.findAll();
}

private List<User> getByInstitution(String institutionKey) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cz/cvut/kbss/study/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface UserService extends BaseService<User> {

User findByUsername(String username);

User getCurrentUser();

User findByEmail(String email);

User findByToken(String token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public User findByUsername(String username) {
return userDao.findByUsername(username);
}

@Override
public User getCurrentUser() {
return securityUtils.getCurrentUser();
}

@Transactional(readOnly = true)
@Override
public List<User> findByInstitution(Institution institution) {
Expand Down

0 comments on commit 55c4a48

Please sign in to comment.