forked from operaton/operaton
-
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.
feat(run): add spring security oauth2 integration (#4481)
related to camunda/camunda-bpm-platform#4452 Backported commit b53ad55475 from the camunda-bpm-platform repository. Original author: Daniel Kelemen <[email protected]>"
- Loading branch information
1 parent
d7b6604
commit 6107308
Showing
13 changed files
with
213 additions
and
113 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
1 change: 0 additions & 1 deletion
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
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
92 changes: 92 additions & 0 deletions
92
...pm/spring/boot/starter/security/oauth2/OperatonSpringSecurityOAuth2AutoConfiguration.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,92 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. Camunda licenses this file to you under the Apache License, | ||
* Version 2.0; you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.operaton.bpm.spring.boot.starter.security.oauth2; | ||
|
||
import jakarta.servlet.DispatcherType; | ||
import jakarta.servlet.Filter; | ||
import org.operaton.bpm.engine.rest.security.auth.ProcessEngineAuthenticationFilter; | ||
import org.operaton.bpm.engine.spring.SpringProcessEngineServicesConfiguration; | ||
import org.operaton.bpm.spring.boot.starter.OperatonBpmAutoConfiguration; | ||
import org.operaton.bpm.spring.boot.starter.property.OperatonBpmProperties; | ||
import org.operaton.bpm.spring.boot.starter.property.WebappProperty; | ||
import org.operaton.bpm.spring.boot.starter.security.oauth2.impl.OAuth2AuthenticationProvider; | ||
import org.operaton.bpm.webapp.impl.security.auth.ContainerBasedAuthenticationFilter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.AutoConfigureOrder; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.security.SecurityProperties; | ||
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.security.config.Customizer; | ||
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 java.util.Map; | ||
|
||
@AutoConfigureOrder(OperatonSpringSecurityOAuth2AutoConfiguration.CAMUNDA_OAUTH2_ORDER) | ||
@AutoConfigureAfter({ OperatonBpmAutoConfiguration.class, SpringProcessEngineServicesConfiguration.class }) | ||
@ConditionalOnBean(OperatonBpmProperties.class) | ||
@Conditional(ClientsConfiguredCondition.class) | ||
public class OperatonSpringSecurityOAuth2AutoConfiguration { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(OperatonSpringSecurityOAuth2AutoConfiguration.class); | ||
public static final int CAMUNDA_OAUTH2_ORDER = Ordered.HIGHEST_PRECEDENCE + 100; | ||
private final String webappPath; | ||
|
||
public OperatonSpringSecurityOAuth2AutoConfiguration(OperatonBpmProperties properties) { | ||
WebappProperty webapp = properties.getWebapp(); | ||
this.webappPath = webapp.getApplicationPath(); | ||
} | ||
|
||
@Bean | ||
public FilterRegistrationBean<?> webappAuthenticationFilter() { | ||
FilterRegistrationBean<Filter> filterRegistration = new FilterRegistrationBean<>(); | ||
filterRegistration.setName("Container Based Authentication Filter"); | ||
filterRegistration.setFilter(new ContainerBasedAuthenticationFilter()); | ||
filterRegistration.setInitParameters(Map.of( | ||
ProcessEngineAuthenticationFilter.AUTHENTICATION_PROVIDER_PARAM, OAuth2AuthenticationProvider.class.getName())); | ||
// make sure the filter is registered after the Spring Security Filter Chain | ||
filterRegistration.setOrder(SecurityProperties.DEFAULT_FILTER_ORDER + 1); | ||
filterRegistration.addUrlPatterns(webappPath + "/app/*", webappPath + "/api/*"); | ||
filterRegistration.setDispatcherTypes(DispatcherType.REQUEST); | ||
return filterRegistration; | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
logger.info("Enabling Operaton Spring Security oauth2 integration"); | ||
|
||
http.authorizeHttpRequests(c -> c | ||
.requestMatchers(webappPath + "/app/**").authenticated() | ||
.requestMatchers(webappPath + "/api/**").authenticated() | ||
.anyRequest().permitAll() | ||
) | ||
.oauth2Login(Customizer.withDefaults()) | ||
.oidcLogout(Customizer.withDefaults()) | ||
.oauth2Client(Customizer.withDefaults()) | ||
.csrf(AbstractHttpConfigurer::disable); | ||
|
||
return http.build(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
.../operaton/bpm/spring/boot/starter/security/oauth2/impl/ClientsNotConfiguredCondition.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,34 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. Camunda licenses this file to you under the Apache License, | ||
* Version 2.0; you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.operaton.bpm.spring.boot.starter.security.oauth2.impl; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; | ||
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
|
||
/** | ||
* Condition that matches if no {@code spring.security.oauth2.client.registration} properties are defined | ||
* by inverting the outcome of {@link ClientsConfiguredCondition}. | ||
*/ | ||
public class ClientsNotConfiguredCondition extends ClientsConfiguredCondition { | ||
@Override | ||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
var matchOutcome = super.getMatchOutcome(context, metadata); | ||
return new ConditionOutcome(!matchOutcome.isMatch(), matchOutcome.getConditionMessage()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...g/operaton/bpm/spring/boot/starter/security/oauth2/impl/OAuth2AuthenticationProvider.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,56 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. Camunda licenses this file to you under the Apache License, | ||
* Version 2.0; you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.operaton.bpm.spring.boot.starter.security.oauth2.impl; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.operaton.bpm.engine.ProcessEngine; | ||
import org.operaton.bpm.engine.rest.security.auth.AuthenticationResult; | ||
import org.operaton.bpm.engine.rest.security.auth.impl.ContainerBasedAuthenticationProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
|
||
public class OAuth2AuthenticationProvider extends ContainerBasedAuthenticationProvider { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(OAuth2AuthenticationProvider.class); | ||
|
||
@Override | ||
public AuthenticationResult extractAuthenticatedUser(HttpServletRequest request, ProcessEngine engine) { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
if (authentication == null) { | ||
logger.debug("Authentication is null"); | ||
return AuthenticationResult.unsuccessful(); | ||
} | ||
|
||
if (!(authentication instanceof OAuth2AuthenticationToken)) { | ||
logger.debug("Authentication is not OAuth2, it is {}", authentication.getClass()); | ||
return AuthenticationResult.unsuccessful(); | ||
} | ||
var oauth2 = (OAuth2AuthenticationToken) authentication; | ||
String operatonUserId = oauth2.getName(); | ||
if (operatonUserId == null || operatonUserId.isEmpty()) { | ||
logger.debug("UserId is empty"); | ||
return AuthenticationResult.unsuccessful(); | ||
} | ||
|
||
logger.debug("Authenticated user '{}'", operatonUserId); | ||
return AuthenticationResult.successful(operatonUserId); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
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,2 @@ | ||
org.operaton.bpm.spring.boot.starter.security.oauth2.OperatonBpmSpringSecurityDisableAutoConfiguration | ||
org.operaton.bpm.spring.boot.starter.security.oauth2.OperatonSpringSecurityOAuth2AutoConfiguration |
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
43 changes: 0 additions & 43 deletions
43
...-security/src/test/java/my/own/custom/spring/boot/project/SamplePermitAllApplication.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
Oops, something went wrong.