Allow abilitiy to boot controller with non-default admin credentials
[controller.git] / opendaylight / security / src / main / java / org / opendaylight / controller / security / ControllerCustomRealm.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.security;
9
10 import java.security.Principal;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.apache.catalina.realm.GenericPrincipal;
15 import org.apache.catalina.realm.RealmBase;
16 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
17 import org.opendaylight.controller.sal.authorization.UserLevel;
18 import org.opendaylight.controller.sal.utils.ServiceHelper;
19 import org.opendaylight.controller.usermanager.IUserManager;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class ControllerCustomRealm  extends RealmBase {
24
25     private static final String name = "ControllerCustomRealm";
26
27     private static final Logger logger = LoggerFactory
28             .getLogger(ControllerCustomRealm.class);
29
30     @Override
31     protected String getName() {
32         return name;
33     }
34
35     @Override
36     protected String getPassword(String username) {
37         IUserManager userManager = (IUserManager) ServiceHelper
38                 .getGlobalInstance(IUserManager.class, this);
39         if (userManager != null) {
40             return userManager.getPassword(username);
41         } else {
42             throw new RuntimeException("User Manager reference is null");
43         }
44     }
45
46     @Override
47     protected Principal getPrincipal(String username) {
48         IUserManager userManager = (IUserManager) ServiceHelper
49                 .getGlobalInstance(IUserManager.class, this);
50         if (userManager != null) {
51             List<String> controllerRoles = new ArrayList<String>();
52             for (UserLevel level : userManager.getUserLevels(username)) {
53                 controllerRoles.add(level.toString());
54             }
55             return new GenericPrincipal(username, "", controllerRoles);
56         } else {
57             throw new RuntimeException("User Manager reference is null");
58         }
59     }
60
61     @Override
62     public Principal authenticate(String username, String credentials) {
63
64         IUserManager userManager = (IUserManager) ServiceHelper
65                 .getGlobalInstance(IUserManager.class, this);
66         if (userManager != null) {
67             AuthResultEnum result = userManager.authenticate(username,
68                     credentials);
69             if (result.equals(AuthResultEnum.AUTHOR_PASS)
70                     || result.equals(AuthResultEnum.AUTH_ACCEPT_LOC)
71                     || result.equals(AuthResultEnum.AUTH_ACCEPT)) {
72                 return this.getPrincipal(username);
73             } else {
74                 logger.debug("Authentication failed for user " + username);
75                 return null;
76             }
77         } else {
78             throw new RuntimeException("User Manager reference is null");
79         }
80     }
81
82 }