-
Notifications
You must be signed in to change notification settings - Fork 4
Add authentication #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add authentication #131
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
54a43be
Add Spring security
loichenninger be164f8
Add authentication for all endpoints except health-checks
loichenninger 37d1738
Add keycloak properties
loichenninger 7e04a6d
Add SSO to swagger
loichenninger 475bd95
Show url of the server in swagger
loichenninger 6f73abd
Resolve http/https issues in swagger
loichenninger cf51126
Add test for SpringDocConfiguration
loichenninger 8c8a712
Remove mocks
loichenninger bfb7f67
Remove SpringDocConfiguration tests
loichenninger 823ed2f
Remove unused config in pom
loichenninger e76ca7d
Bump BPM version
loichenninger 22e36ef
Resolve pom conflict
loichenninger 35f082f
Merge branch 'main' into devAuth
loichenninger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
69 changes: 57 additions & 12 deletions
69
src/main/java/fr/insee/genesis/configuration/SpringDocConfiguration.java
This file contains hidden or 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,28 +1,73 @@ | ||
| package fr.insee.genesis.configuration; | ||
|
|
||
| import io.swagger.v3.oas.models.Components; | ||
| import io.swagger.v3.oas.models.OpenAPI; | ||
| import io.swagger.v3.oas.models.info.Info; | ||
| import io.swagger.v3.oas.models.security.*; | ||
| import io.swagger.v3.oas.models.servers.Server; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class SpringDocConfiguration { | ||
|
|
||
| @Value("${fr.insee.genesis.version}") | ||
| private String projectVersion; | ||
| @Value("${fr.insee.genesis.version}") | ||
| private String projectVersion; | ||
| public static final String BEARERSCHEME = "bearerAuth"; | ||
| public static final String OAUTH2SCHEME = "oauth2"; | ||
|
|
||
| @Bean | ||
| public OpenAPI customOpenAPI() { | ||
| return new OpenAPI() | ||
| .addServersItem(new Server().url("/")) | ||
| .info(new Info() | ||
| .title("Genesis API") | ||
| .description("Rest Endpoints and services to communicate with Genesis database") | ||
| .version(projectVersion) | ||
| ); | ||
| } | ||
| @Bean | ||
| @ConditionalOnProperty(name = "fr.insee.genesis.authentication", havingValue = "NONE") | ||
| public OpenAPI noAuthOpenAPI() { | ||
| return generateOpenAPI(); | ||
| } | ||
|
|
||
| @Bean | ||
| @ConditionalOnProperty(name = "fr.insee.genesis.authentication", havingValue = "OIDC") | ||
| public OpenAPI oidcOpenAPI(Config config) { | ||
| String authUrl = config.getAuthServerUrl() + "/realms/" + config.getRealm() + "/protocol/openid-connect"; | ||
| return generateOpenAPI() | ||
| .addSecurityItem(new SecurityRequirement().addList(OAUTH2SCHEME)) | ||
| .addSecurityItem(new SecurityRequirement().addList(BEARERSCHEME)) | ||
| .components( | ||
| new Components() | ||
| .addSecuritySchemes(OAUTH2SCHEME, | ||
| new SecurityScheme() | ||
| .name(OAUTH2SCHEME) | ||
| .type(SecurityScheme.Type.OAUTH2) | ||
| .flows(getFlows(authUrl)) | ||
| ) | ||
| .addSecuritySchemes(BEARERSCHEME, | ||
| new SecurityScheme() | ||
| .name(BEARERSCHEME) | ||
| .type(SecurityScheme.Type.HTTP) | ||
| .scheme("bearer") | ||
| .bearerFormat("JWT") | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| private OpenAPI generateOpenAPI() { | ||
| return new OpenAPI() | ||
| .addServersItem(new Server().url("/")) | ||
| .info(new Info() | ||
| .title("Genesis API") | ||
| .description("Rest Endpoints and services to communicate with Genesis database") | ||
| .version(projectVersion) | ||
| ); | ||
| } | ||
|
|
||
| private OAuthFlows getFlows(String authUrl) { | ||
| OAuthFlows flows = new OAuthFlows(); | ||
| OAuthFlow flow = new OAuthFlow(); | ||
| Scopes scopes = new Scopes(); | ||
| flow.setAuthorizationUrl(authUrl + "/auth"); | ||
| flow.setTokenUrl(authUrl + "/token"); | ||
| flow.setRefreshUrl(authUrl + "/token"); | ||
| flow.setScopes(scopes); | ||
| return flows.authorizationCode(flow); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
34 changes: 30 additions & 4 deletions
34
src/main/java/fr/insee/genesis/configuration/auth/security/OIDCSecurityConfig.java
This file contains hidden or 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,22 +1,48 @@ | ||
| package fr.insee.genesis.configuration.auth.security; | ||
|
|
||
| import fr.insee.genesis.configuration.Config; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| 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.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
| import org.springframework.security.config.http.SessionCreationPolicy; | ||
| import org.springframework.security.web.SecurityFilterChain; | ||
| import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | ||
|
|
||
| @Configuration | ||
| @EnableWebSecurity | ||
| @Slf4j | ||
| @ConditionalOnProperty(name = "fr.insee.genesis.authentication", havingValue = "OIDC") | ||
| public class OIDCSecurityConfig { | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
| http.csrf(AbstractHttpConfigurer::disable).authorizeHttpRequests(auth -> auth.anyRequest().permitAll()); | ||
| return http.build(); | ||
| Config config; | ||
| @Autowired | ||
| public OIDCSecurityConfig(Config config) { | ||
| this.config = config; | ||
| } | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
| http | ||
| .csrf(AbstractHttpConfigurer::disable) | ||
| .sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS)); | ||
| for (var pattern : config.getWhiteList()) { | ||
| http.authorizeHttpRequests(authorize -> | ||
| authorize | ||
| .requestMatchers(AntPathRequestMatcher.antMatcher(pattern)).permitAll() | ||
| ); | ||
| } | ||
| http | ||
| .authorizeHttpRequests(configurer -> configurer | ||
| .anyRequest().authenticated() | ||
| ) | ||
| .oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults())); | ||
| return http.build(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,14 +6,23 @@ spring.profiles.active=local | |
| #-------------------------------------------------------------------------- | ||
| # Global configuration | ||
| #-------------------------------------------------------------------------- | ||
| fr.insee.genesis.authentication = NONE | ||
| fr.insee.genesis.authentication = OIDC | ||
|
|
||
| #-------------------------------------------------------------------------- | ||
| # Configuration for springdoc / swagger | ||
| #-------------------------------------------------------------------------- | ||
| [email protected]@ | ||
| #To make swagger-ui display the actuator endpoints | ||
| springdoc.show-actuator=true | ||
| springdoc.swagger-ui.oauth2RedirectUrl=${fr.insee.genesis.application.host.url}/swagger-ui/oauth2-redirect.html | ||
|
|
||
| #-------------------------------------------------------------------------- | ||
| # Security | ||
| #-------------------------------------------------------------------------- | ||
| fr.insee.genesis.security.token.oidc-claim-role=realm_access.roles | ||
| fr.insee.genesis.security.token.oidc-claim-username=name | ||
| spring.security.oauth2.resourceserver.jwt.issuer-uri=${fr.insee.genesis.oidc.auth-server-url}/realms/${fr.insee.genesis.oidc.realm} | ||
| fr.insee.genesis.security.whitelist-matchers=/v3/api-docs/**,/swagger-ui/**,/swagger-ui.html,/actuator/**,/error,/,/health-check/** | ||
|
|
||
| #-------------------------------------------------------------------------- | ||
| # Actuator | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.