-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from qbicsoftware/feature/dm-1122-enable-pat-to…
…ken-auth Authenticate using personal access tokens
- Loading branch information
Showing
24 changed files
with
888 additions
and
30 deletions.
There are no files selected for viewing
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
2 changes: 0 additions & 2 deletions
2
...nector/src/test/java/life/qbic/data_download/openbis/DatasetFileStreamReaderImplTest.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
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
53 changes: 53 additions & 0 deletions
53
rest-api/src/main/java/life/qbic/data_download/rest/config/AsyncDownloadConfig.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,53 @@ | ||
package life.qbic.data_download.rest.config; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.Executor; | ||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.task.AsyncTaskExecutor; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
|
||
@Configuration | ||
@EnableAsync | ||
public class AsyncDownloadConfig implements AsyncConfigurer { | ||
|
||
@Override | ||
@Bean("taskExecutor") | ||
public Executor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); | ||
threadPoolTaskExecutor.setCorePoolSize(2); | ||
threadPoolTaskExecutor.setMaxPoolSize(2); | ||
threadPoolTaskExecutor.setQueueCapacity(200); | ||
threadPoolTaskExecutor.setThreadNamePrefix("download - "); | ||
threadPoolTaskExecutor.initialize(); | ||
return threadPoolTaskExecutor; | ||
} | ||
|
||
@Override | ||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | ||
return new SimpleAsyncUncaughtExceptionHandler(); | ||
} | ||
|
||
@Bean | ||
public WebMvcConfigurer webMvcConfigurer( | ||
@Qualifier("taskExecutor") final AsyncTaskExecutor taskExecutor, | ||
@Value("${spring.mvc.async.request-timeout}") Duration timeout) { | ||
return new WebMvcConfigurer() { | ||
@Override | ||
public void configureAsyncSupport(AsyncSupportConfigurer configurer) { | ||
configurer.setDefaultTimeout(timeout.toMillis()).setTaskExecutor(taskExecutor); | ||
|
||
} | ||
}; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
rest-api/src/main/java/life/qbic/data_download/rest/config/OpenApiConfig.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,40 @@ | ||
package life.qbic.data_download.rest.config; | ||
|
||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn; | ||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; | ||
import io.swagger.v3.oas.annotations.info.Info; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.security.SecurityScheme; | ||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.security.SecurityScheme.In; | ||
import io.swagger.v3.oas.models.security.SecurityScheme.Type; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@OpenAPIDefinition(info = @Info(title = "My API", version = "v1"), | ||
security = @SecurityRequirement(name = "personal_access_token")) //globally set this | ||
public class OpenApiConfig { | ||
|
||
@Bean | ||
public OpenAPI customOpenAPI( | ||
@Value("${server.download.token-name}") String tokenName | ||
) { | ||
io.swagger.v3.oas.models.security.SecurityScheme securityScheme = new io.swagger.v3.oas.models.security.SecurityScheme() | ||
.type(Type.APIKEY) | ||
.in(In.HEADER) | ||
.description( | ||
"A personal access token (PAT) obtained through by the data manager. Please prefix your token with '" | ||
+ tokenName + " " + "' e.g. '" + tokenName + " abcdefg1234'.") | ||
.name("Authorization"); | ||
var securityComponent = new Components() | ||
.addSecuritySchemes("personal_access_token", securityScheme); | ||
|
||
return new OpenAPI() | ||
.components(securityComponent); | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
rest-api/src/main/java/life/qbic/data_download/rest/config/SecurityConfig.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,97 @@ | ||
package life.qbic.data_download.rest.config; | ||
|
||
import life.qbic.data_download.rest.security.QBiCTokenAuthenticationFilter; | ||
import life.qbic.data_download.rest.security.QBiCTokenAuthenticationProvider; | ||
import life.qbic.data_download.rest.security.QBiCTokenMatcher; | ||
import life.qbic.data_download.rest.security.TokenMatcher; | ||
import life.qbic.data_download.rest.security.jpa.token.EncodedAccessTokenRepository; | ||
import life.qbic.data_download.rest.security.jpa.user.UserDetailsRepository; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.ProviderManager; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; | ||
|
||
/** | ||
* The security config setting up endpoint security for the controllers. | ||
*/ | ||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
|
||
private final EncodedAccessTokenRepository encodedAccessTokenRepository; | ||
private final UserDetailsRepository userDetailsRepository; | ||
private final String[] ignoredEndpoints = { | ||
"/v3/api-docs/**", | ||
"/swagger-ui/**", | ||
"/swagger-ui.html" | ||
}; | ||
|
||
public SecurityConfig(EncodedAccessTokenRepository encodedAccessTokenRepository, | ||
UserDetailsRepository userDetailsRepository) { | ||
this.encodedAccessTokenRepository = encodedAccessTokenRepository; | ||
this.userDetailsRepository = userDetailsRepository; | ||
} | ||
|
||
|
||
@Bean("accessTokenEncoder") | ||
public TokenMatcher tokenEncoder() { | ||
return new QBiCTokenMatcher(); | ||
} | ||
|
||
@Bean("tokenAuthenticationProvider") | ||
public QBiCTokenAuthenticationProvider authenticationProvider( | ||
@Qualifier("accessTokenEncoder") TokenMatcher tokenMatcher) { | ||
return new QBiCTokenAuthenticationProvider(tokenMatcher, encodedAccessTokenRepository, userDetailsRepository); | ||
} | ||
|
||
@Bean("tokenAuthenticationManager") | ||
public AuthenticationManager authenticationManager( | ||
@Qualifier("tokenAuthenticationProvider") QBiCTokenAuthenticationProvider authenticationProvider) { | ||
return new ProviderManager(authenticationProvider); | ||
} | ||
|
||
@Bean("tokenAuthenticationFilter") | ||
public QBiCTokenAuthenticationFilter authenticationFilter( | ||
@Qualifier("tokenAuthenticationManager") AuthenticationManager authenticationManager, | ||
@Value("${server.download.token-name}") String tokenName) { | ||
return new QBiCTokenAuthenticationFilter(authenticationManager, tokenName); | ||
} | ||
|
||
|
||
|
||
@Bean | ||
public SecurityFilterChain apiFilterChain(HttpSecurity http, | ||
@Qualifier("tokenAuthenticationProvider") QBiCTokenAuthenticationProvider authenticationProvider, | ||
@Qualifier("tokenAuthenticationFilter") QBiCTokenAuthenticationFilter tokenAuthenticationFilter) throws Exception { | ||
http | ||
.authorizeHttpRequests(authorizedRequest -> | ||
authorizedRequest | ||
.requestMatchers(ignoredEndpoints) | ||
.permitAll()) | ||
// //require https | ||
// .requiresChannel(channel -> channel.anyRequest().requiresSecure()) | ||
.authenticationProvider(authenticationProvider) | ||
.addFilterAt(tokenAuthenticationFilter, BasicAuthenticationFilter.class) | ||
.authorizeHttpRequests(authorizedRequest -> | ||
authorizedRequest | ||
.requestMatchers("/download/measurements/**") | ||
.authenticated() | ||
); | ||
// .access(new WebExpressionAuthorizationManager("hasPermission(//TODO)"))); | ||
|
||
return http.build(); | ||
} | ||
|
||
|
||
@Bean | ||
public WebSecurityCustomizer webSecurityCustomizer() { | ||
return web -> web.ignoring().requestMatchers(ignoredEndpoints); | ||
} | ||
} |
Oops, something went wrong.