forked from cBioPortal/cbioportal
-
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.
Working version of OAuth2Token mechanism
- Loading branch information
1 parent
0fd0d4f
commit d700c8a
Showing
6 changed files
with
82 additions
and
286 deletions.
There are no files selected for viewing
87 changes: 62 additions & 25 deletions
87
src/main/java/org/cbioportal/security/config/ApiSecurityConfig.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,61 +1,98 @@ | ||
package org.cbioportal.security.config; | ||
|
||
import org.cbioportal.security.token.RestAuthenticationEntryPoint; | ||
import org.cbioportal.security.token.oauth2.OAuth2AccessTokenRefreshFilter; | ||
import org.cbioportal.security.token.TokenAuthenticationFilter; | ||
import org.cbioportal.security.token.TokenAuthenticationSuccessHandler; | ||
import org.cbioportal.service.DataAccessTokenService; | ||
import org.cbioportal.utils.config.annotation.ConditionalOnProperty; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.security.SecurityProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.context.SecurityContextPersistenceFilter; | ||
|
||
@Configuration | ||
@Order(SecurityProperties.BASIC_AUTH_ORDER - 2) | ||
@ConditionalOnProperty(name = "authenticate", havingValue = {"saml", "oauth2"}) | ||
@ConditionalOnProperty(name = "authenticate", havingValue = {"false", "noauthsessionservice"}, isNot = true) | ||
public class ApiSecurityConfig { | ||
|
||
// Add security filter chains that handle calls to the API endpoints. | ||
// Different chains are added for the '/api' and legacy '/webservice.do' paths. | ||
// Both are able to handle API tokens provided in the request. | ||
// see: "Creating and Customizing Filter Chains" @ https://spring.io/guides/topicals/spring-security-architecture | ||
|
||
// Only available when using OAuth2 authentication. | ||
@Autowired(required = false) | ||
private OAuth2AccessTokenRefreshFilter oAuth2AccessTokenRefreshFilter; | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
|
||
@Autowired(required = false) | ||
private DataAccessTokenService tokenService; | ||
|
||
@Autowired(required = false) | ||
private AuthenticationProvider tokenAuthenticationProvider; | ||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
http | ||
// FIXME - csrf should be enabled | ||
.csrf(AbstractHttpConfigurer::disable) | ||
// This filter chain only grabs requests to the '/api' path. | ||
.securityMatcher("/api/**") | ||
.securityMatcher("/api/**", "/webservice.do") | ||
.authorizeHttpRequests(authorize -> authorize | ||
.requestMatchers( | ||
"/api/swagger-resources/**", | ||
"/api/swagger-ui.html", | ||
"/api/health", | ||
"/api/cache/**").permitAll() | ||
.anyRequest().authenticated() | ||
.requestMatchers( | ||
"/api/swagger-resources/**", | ||
"/api/swagger-ui.html", | ||
"/api/health", | ||
"/api/cache/**").permitAll() | ||
.anyRequest().authenticated() | ||
) | ||
.sessionManagement(sessionManagement -> sessionManagement.sessionFixation().none()) | ||
.exceptionHandling(exceptionHandling -> exceptionHandling | ||
.authenticationEntryPoint(restAuthenticationEntryPoint()) | ||
); | ||
if (oAuth2AccessTokenRefreshFilter != null) { | ||
http | ||
.addFilterAfter(oAuth2AccessTokenRefreshFilter, SecurityContextPersistenceFilter.class); | ||
// When dat.method is not 'none' and a tokenService bean is present, | ||
// the apiTokenAuthenticationFilter is added to the filter chain. | ||
if (tokenService != null) { | ||
http.apply(ApiTokenFilterDsl.tokenFilterDsl(tokenService)); | ||
} | ||
return http.build(); | ||
} | ||
|
||
@Autowired | ||
public AuthenticationManagerBuilder buildAuthenticationManager(AuthenticationManagerBuilder authenticationManagerBuilder) { | ||
if (tokenAuthenticationProvider != null) { | ||
authenticationManagerBuilder.authenticationProvider(tokenAuthenticationProvider); | ||
} | ||
return http.build(); | ||
} | ||
return authenticationManagerBuilder; | ||
} | ||
|
||
@Bean | ||
public RestAuthenticationEntryPoint restAuthenticationEntryPoint() { | ||
return new RestAuthenticationEntryPoint(); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
class ApiTokenFilterDsl extends AbstractHttpConfigurer<ApiTokenFilterDsl, HttpSecurity> { | ||
|
||
private final DataAccessTokenService tokenService; | ||
|
||
public ApiTokenFilterDsl(DataAccessTokenService tokenService) { | ||
this.tokenService = tokenService; | ||
} | ||
|
||
@Override | ||
public void configure(HttpSecurity http) { | ||
AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class); | ||
TokenAuthenticationSuccessHandler tokenAuthenticationSuccessHandler = new TokenAuthenticationSuccessHandler(); | ||
TokenAuthenticationFilter filter = new TokenAuthenticationFilter("/**", authenticationManager, tokenService); | ||
filter.setAuthenticationSuccessHandler(tokenAuthenticationSuccessHandler); | ||
http.addFilterAfter(filter, SecurityContextPersistenceFilter.class); | ||
} | ||
|
||
public static ApiTokenFilterDsl tokenFilterDsl(DataAccessTokenService tokenService) { | ||
return new ApiTokenFilterDsl(tokenService); | ||
} | ||
|
||
} |
58 changes: 0 additions & 58 deletions
58
src/main/java/org/cbioportal/security/config/DatAccessApiSecurityConfig.java
This file was deleted.
Oops, something went wrong.
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.