You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by da...@apache.org on 2024/06/14 17:15:25 UTC

(causeway) branch maintenance-branch updated (50df124aa3 -> 0ae133c40e)

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a change to branch maintenance-branch
in repository https://gitbox.apache.org/repos/asf/causeway.git


    from 50df124aa3 CAUSEWAY-3774: docs
     new 29d4f86220 CAUSEWAY-3778: reworks ApplicationUserAutoCreationService
     new bb52fd20c8 CAUSEWAY-3778: check if principal is OidcUser
     new 0ae133c40e CAUSEWAY-3778: now honours autoCreatePolicy

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../core/config/CausewayConfiguration.java         |  1 +
 .../dom/ApplicationUserAutoCreationService.java    | 55 ++++++++++++++++------
 2 files changed, 42 insertions(+), 14 deletions(-)


(causeway) 03/03: CAUSEWAY-3778: now honours autoCreatePolicy

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch maintenance-branch
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit 0ae133c40e0e6e88997f1d6bc99ad8fd22988b3e
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Fri Jun 14 16:50:47 2024 +0100

    CAUSEWAY-3778: now honours autoCreatePolicy
    
    along with new option, DO_NOT_AUTO_CREATE
---
 .../core/config/CausewayConfiguration.java         |  1 +
 .../dom/ApplicationUserAutoCreationService.java    | 32 +++++++++++++++++-----
 2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java b/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
index c498d13902..4857a2d72c 100644
--- a/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
+++ b/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
@@ -4352,6 +4352,7 @@ public class CausewayConfiguration {
                 public enum AutoCreatePolicy {
                     AUTO_CREATE_AS_LOCKED,
                     AUTO_CREATE_AS_UNLOCKED,
+                    DO_NOT_AUTO_CREATE,
                 }
 
                 /**
diff --git a/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java b/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java
index c1a182f131..9d505983dc 100644
--- a/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java
+++ b/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java
@@ -22,19 +22,16 @@ package org.apache.causeway.extensions.secman.delegated.springoauth2.dom;
 
 import java.util.Collection;
 import java.util.List;
-import java.util.Optional;
 
 import javax.annotation.Priority;
 import javax.inject.Inject;
 
 import org.apache.causeway.applib.annotation.PriorityPrecedence;
 
-import org.springframework.context.ApplicationListener;
 import org.springframework.context.event.EventListener;
 import org.springframework.core.annotation.Order;
 import org.springframework.lang.Nullable;
 import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
-import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
 import org.springframework.security.oauth2.core.oidc.user.OidcUser;
 import org.springframework.stereotype.Service;
 
@@ -51,6 +48,15 @@ import org.apache.causeway.extensions.secman.applib.user.dom.mixins.ApplicationU
 import lombok.RequiredArgsConstructor;
 import lombok.val;
 
+/**
+ * This service automatically creates an {@link ApplicationUser} if an end-user successfully logged in via Oauth.
+ *
+ * <p>
+ *     The initial set of rules are as per {@link CausewayConfiguration.Extensions.Secman.DelegatedUsers.AutoCreatePolicy}
+ * </p>
+ *
+ * @since 2.0 {@index}
+ */
 @Service
 @RequiredArgsConstructor(onConstructor_ = {@Inject})
 @Priority(PriorityPrecedence.MIDPOINT)
@@ -76,14 +82,26 @@ public class ApplicationUserAutoCreationService {
         val username = oidcUser.getPreferredUsername();
         val email = oidcUser.getEmail();
 
+        val secmanConfig = causewayConfiguration.getExtensions().getSecman().getDelegatedUsers();
+        switch (secmanConfig.getAutoCreatePolicy()) {
+            case DO_NOT_AUTO_CREATE:
+                break;
+            case AUTO_CREATE_AS_LOCKED:
+                create(username, email, secmanConfig.getInitialRoleNames(), ApplicationUserStatus.LOCKED);
+                break;
+            case AUTO_CREATE_AS_UNLOCKED:
+                create(username, email, secmanConfig.getInitialRoleNames(), ApplicationUserStatus.UNLOCKED);
+                break;
+        }
+    }
+
+    private void create(String username, String email, List<String> initialRoleNames, ApplicationUserStatus userStatus) {
         interactionService.runAnonymous(() -> {
-            Optional<ApplicationUser> userIfAny = applicationUserRepository.findByUsername(username);
+            val userIfAny = applicationUserRepository.findByUsername(username);
             if (userIfAny.isEmpty()) {
-                val status = ApplicationUserStatus.UNLOCKED;  // locking not supported for spring delegated accounts
-                val applicationUser = applicationUserRepository.newDelegateUser(username, status);
+                val applicationUser = applicationUserRepository.newDelegateUser(username, userStatus);
                 factoryService.mixin(ApplicationUser_updateEmailAddress.class, applicationUser).act(email);
 
-                val initialRoleNames = causewayConfiguration.getExtensions().getSecman().getDelegatedUsers().getInitialRoleNames();
                 if (notEmpty(initialRoleNames)) {
                     for (String initialRoleName : initialRoleNames) {
                         addRoleIfExists(applicationUser, initialRoleName);


(causeway) 01/03: CAUSEWAY-3778: reworks ApplicationUserAutoCreationService

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch maintenance-branch
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit 29d4f862205f56d744d5057c409178f10d910c5b
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Fri Jun 14 16:39:34 2024 +0100

    CAUSEWAY-3778: reworks ApplicationUserAutoCreationService
    
    to use Spring boot annotations, and specify an explicit order
---
 .../dom/ApplicationUserAutoCreationService.java             | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java b/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java
index e3836fd780..5ce4e02d6c 100644
--- a/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java
+++ b/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java
@@ -24,9 +24,14 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
 
+import javax.annotation.Priority;
 import javax.inject.Inject;
 
+import org.apache.causeway.applib.annotation.PriorityPrecedence;
+
 import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.EventListener;
+import org.springframework.core.annotation.Order;
 import org.springframework.lang.Nullable;
 import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
 import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
@@ -47,8 +52,8 @@ import lombok.val;
 
 @Service
 @RequiredArgsConstructor(onConstructor_ = {@Inject})
-public class ApplicationUserAutoCreationService
-        implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
+@Priority(PriorityPrecedence.MIDPOINT)
+public class ApplicationUserAutoCreationService {
 
     private final ApplicationUserRepository applicationUserRepository;
     private final ApplicationRoleRepository applicationRoleRepository;
@@ -56,8 +61,10 @@ public class ApplicationUserAutoCreationService
     private final CausewayConfiguration causewayConfiguration;
     private final FactoryService factoryService;
 
-    @Override
+    @Order(PriorityPrecedence.MIDPOINT)
+    @EventListener(InteractiveAuthenticationSuccessEvent.class)
     public void onApplicationEvent(final InteractiveAuthenticationSuccessEvent event) {
+
         val authentication = event.getAuthentication();
         val principal = authentication.getPrincipal();
         if (!(principal instanceof DefaultOidcUser)) {


(causeway) 02/03: CAUSEWAY-3778: check if principal is OidcUser

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch maintenance-branch
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit bb52fd20c88885cc46dc5eb8eab7f122d0ad8e58
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Fri Jun 14 16:41:04 2024 +0100

    CAUSEWAY-3778: check if principal is OidcUser
    
    rather than DefaultOidcUser
---
 .../springoauth2/dom/ApplicationUserAutoCreationService.java   | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java b/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java
index 5ce4e02d6c..c1a182f131 100644
--- a/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java
+++ b/extensions/security/secman/delegated-springoauth2/src/main/java/org/apache/causeway/extensions/secman/delegated/springoauth2/dom/ApplicationUserAutoCreationService.java
@@ -35,6 +35,7 @@ import org.springframework.core.annotation.Order;
 import org.springframework.lang.Nullable;
 import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
 import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
+import org.springframework.security.oauth2.core.oidc.user.OidcUser;
 import org.springframework.stereotype.Service;
 
 import org.apache.causeway.applib.services.factory.FactoryService;
@@ -67,13 +68,14 @@ public class ApplicationUserAutoCreationService {
 
         val authentication = event.getAuthentication();
         val principal = authentication.getPrincipal();
-        if (!(principal instanceof DefaultOidcUser)) {
+        if (!(principal instanceof OidcUser)) {
             return;
         }
 
-        val oidcUser = (DefaultOidcUser) principal;
-        val username = oidcUser.getIdToken().getPreferredUsername();
-        val email = oidcUser.getIdToken().getEmail();
+        val oidcUser = (OidcUser) principal;
+        val username = oidcUser.getPreferredUsername();
+        val email = oidcUser.getEmail();
+
         interactionService.runAnonymous(() -> {
             Optional<ApplicationUser> userIfAny = applicationUserRepository.findByUsername(username);
             if (userIfAny.isEmpty()) {