Replacing Spring Security framework with Apache Tomcat Valve for Web Security (UI...
[controller.git] / opendaylight / security / src / main / java / org / opendaylight / controller / security / ControllerCustomRealm.java
diff --git a/opendaylight/security/src/main/java/org/opendaylight/controller/security/ControllerCustomRealm.java b/opendaylight/security/src/main/java/org/opendaylight/controller/security/ControllerCustomRealm.java
new file mode 100644 (file)
index 0000000..43a41da
--- /dev/null
@@ -0,0 +1,68 @@
+package org.opendaylight.controller.security;
+
+import java.security.Principal;
+import java.util.List;
+
+import org.apache.catalina.realm.GenericPrincipal;
+import org.apache.catalina.realm.RealmBase;
+import org.opendaylight.controller.sal.authorization.AuthResultEnum;
+import org.opendaylight.controller.sal.utils.ServiceHelper;
+import org.opendaylight.controller.usermanager.IUserManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ControllerCustomRealm  extends RealmBase {
+
+    private static final String name = "ControllerCustomRealm";
+
+    private static final Logger logger = LoggerFactory
+            .getLogger(ControllerCustomRealm.class);
+
+    @Override
+    protected String getName() {
+        return name;
+    }
+
+    @Override
+    protected String getPassword(String username) {
+        IUserManager userManager = (IUserManager) ServiceHelper
+                .getGlobalInstance(IUserManager.class, this);
+        if (userManager != null) {
+            return userManager.getPassword(username);
+        } else
+            throw new RuntimeException("User Manager reference is null");
+    }
+
+    @Override
+    protected Principal getPrincipal(String username) {
+        IUserManager userManager = (IUserManager) ServiceHelper
+                .getGlobalInstance(IUserManager.class, this);
+        if (userManager != null) {
+            final List<String> roles = userManager.getUserRoles(username);
+            return new GenericPrincipal(username, getPassword(username), roles);
+        } else
+            throw new RuntimeException("User Manager reference is null");
+
+    }
+
+    @Override
+    public Principal authenticate(String username, String credentials) {
+
+        IUserManager userManager = (IUserManager) ServiceHelper
+                .getGlobalInstance(IUserManager.class, this);
+        if (userManager != null) {
+            AuthResultEnum result = userManager.authenticate(username,
+                    credentials);
+            if (result.equals(AuthResultEnum.AUTHOR_PASS)
+                    || result.equals(AuthResultEnum.AUTH_ACCEPT_LOC)
+                    || result.equals(AuthResultEnum.AUTH_ACCEPT)) {
+                return this.getPrincipal(username);
+            } else {
+                logger.error("Authentication failed for user " + username);
+                return null;
+            }
+        } else
+            throw new RuntimeException("User Manager reference is null");
+    }
+
+}