1 package org.opendaylight.controller.security;
3 import java.security.Principal;
4 import java.util.ArrayList;
7 import org.apache.catalina.realm.GenericPrincipal;
8 import org.apache.catalina.realm.RealmBase;
9 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
10 import org.opendaylight.controller.sal.authorization.UserLevel;
11 import org.opendaylight.controller.sal.utils.ServiceHelper;
12 import org.opendaylight.controller.usermanager.IUserManager;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 public class ControllerCustomRealm extends RealmBase {
18 private static final String name = "ControllerCustomRealm";
20 private static final Logger logger = LoggerFactory
21 .getLogger(ControllerCustomRealm.class);
24 protected String getName() {
29 protected String getPassword(String username) {
30 IUserManager userManager = (IUserManager) ServiceHelper
31 .getGlobalInstance(IUserManager.class, this);
32 if (userManager != null) {
33 return userManager.getPassword(username);
35 throw new RuntimeException("User Manager reference is null");
39 protected Principal getPrincipal(String username) {
40 IUserManager userManager = (IUserManager) ServiceHelper
41 .getGlobalInstance(IUserManager.class, this);
42 if (userManager != null) {
43 final List<String> levels = new ArrayList<String>();
44 UserLevel level = userManager.getUserLevel(username);
45 if (level == null) level = UserLevel.NOUSER;
46 levels.add(level.toString());
47 return new GenericPrincipal(username, "", levels);
49 throw new RuntimeException("User Manager reference is null");
54 public Principal authenticate(String username, String credentials) {
56 IUserManager userManager = (IUserManager) ServiceHelper
57 .getGlobalInstance(IUserManager.class, this);
58 if (userManager != null) {
59 AuthResultEnum result = userManager.authenticate(username,
61 if (result.equals(AuthResultEnum.AUTHOR_PASS)
62 || result.equals(AuthResultEnum.AUTH_ACCEPT_LOC)
63 || result.equals(AuthResultEnum.AUTH_ACCEPT)) {
64 return this.getPrincipal(username);
66 logger.error("Authentication failed for user " + username);
70 throw new RuntimeException("User Manager reference is null");