Improve Security
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / core / DefaultSecurityService.java
index ef64337ebed1d99762cf642a0c496a8892447980..a4dd7a2029c6b0410dff75164186426b607c6a09 100644 (file)
@@ -6,10 +6,19 @@ import org.argeo.security.ArgeoSecurityDao;
 import org.argeo.security.ArgeoSecurityService;
 import org.argeo.security.ArgeoUser;
 import org.argeo.security.SimpleArgeoUser;
+import org.springframework.core.task.SimpleAsyncTaskExecutor;
+import org.springframework.core.task.TaskExecutor;
+import org.springframework.security.Authentication;
+import org.springframework.security.AuthenticationManager;
+import org.springframework.security.context.SecurityContext;
+import org.springframework.security.context.SecurityContextHolder;
 
 public class DefaultSecurityService implements ArgeoSecurityService {
        private ArgeoSecurity argeoSecurity = new DefaultArgeoSecurity();
        private ArgeoSecurityDao securityDao;
+       private AuthenticationManager authenticationManager;
+
+       private String systemAuthenticationKey;
 
        public ArgeoSecurityDao getSecurityDao() {
                return securityDao;
@@ -48,6 +57,39 @@ public class DefaultSecurityService implements ArgeoSecurityService {
                securityDao.update(simpleArgeoUser);
        }
 
+       public TaskExecutor createSystemAuthenticatedTaskExecutor() {
+               return new SimpleAsyncTaskExecutor() {
+                       private static final long serialVersionUID = -8126773862193265020L;
+
+                       @Override
+                       public Thread createThread(Runnable runnable) {
+                               return super
+                                               .createThread(wrapWithSystemAuthentication(runnable));
+                       }
+
+               };
+       }
+
+       /**
+        * Wraps another runnable, adding security context <br/>
+        * TODO: secure the call to this method with Java Security
+        */
+       public Runnable wrapWithSystemAuthentication(final Runnable runnable) {
+               return new Runnable() {
+
+                       public void run() {
+                               SecurityContext securityContext = SecurityContextHolder
+                                               .getContext();
+                               Authentication auth = authenticationManager
+                                               .authenticate(new InternalAuthentication(
+                                                               systemAuthenticationKey));
+                               securityContext.setAuthentication(auth);
+
+                               runnable.run();
+                       }
+               };
+       }
+
        public void setArgeoSecurity(ArgeoSecurity argeoSecurity) {
                this.argeoSecurity = argeoSecurity;
        }
@@ -56,4 +98,13 @@ public class DefaultSecurityService implements ArgeoSecurityService {
                this.securityDao = dao;
        }
 
+       public void setAuthenticationManager(
+                       AuthenticationManager authenticationManager) {
+               this.authenticationManager = authenticationManager;
+       }
+
+       public void setSystemAuthenticationKey(String systemAuthenticationKey) {
+               this.systemAuthenticationKey = systemAuthenticationKey;
+       }
+
 }