Merge "BUG 652 leafref CCE & BUG 720 colons problem"
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / authentication / AuthProvider.java
1 /*
2  * Copyright (c) 2013 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.netconf.ssh.authentication;
9
10 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
11 import org.opendaylight.controller.sal.authorization.UserLevel;
12 import org.opendaylight.controller.usermanager.IUserManager;
13 import org.opendaylight.controller.usermanager.UserConfig;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import static com.google.common.base.Preconditions.checkNotNull;
19
20 public class AuthProvider implements AuthProviderInterface {
21
22     private static IUserManager um; //FIXME static mutable state, no locks
23     private static final String DEFAULT_USER = "netconf";
24     private static final String DEFAULT_PASSWORD = "netconf";
25     private final String pem;
26
27     public AuthProvider(IUserManager ium, String pemCertificate) throws Exception {
28         checkNotNull(pemCertificate, "Parameter 'pemCertificate' is null");
29         AuthProvider.um = ium;
30         if (AuthProvider.um == null) {
31             throw new Exception("No usermanager service available.");
32         }
33
34         List<String> roles = new ArrayList<String>(1);
35         roles.add(UserLevel.SYSTEMADMIN.toString());
36         AuthProvider.um.addLocalUser(new UserConfig(DEFAULT_USER, DEFAULT_PASSWORD, roles)); //FIXME hardcoded auth
37         pem = pemCertificate;
38     }
39
40     @Override
41     public boolean authenticated(String username, String password) {
42         if (AuthProvider.um == null) {
43             throw new IllegalStateException("No usermanager service available.");
44         }
45         AuthResultEnum authResult = AuthProvider.um.authenticate(username, password);
46         return authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC);
47     }
48
49     @Override
50     public char[] getPEMAsCharArray() {
51         return pem.toCharArray();
52     }
53
54     @Override
55     public void removeUserManagerService() {
56         AuthProvider.um = null;
57     }
58
59     @Override
60     public void addUserManagerService(IUserManager userManagerService) {
61         AuthProvider.um = userManagerService;
62     }
63 }