Fixed potential class pool override in integration tests.
[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 java.io.IOException;
11 import java.io.InputStream;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.apache.commons.io.IOUtils;
15 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
16 import org.opendaylight.controller.sal.authorization.UserLevel;
17 import org.opendaylight.controller.usermanager.IUserManager;
18 import org.opendaylight.controller.usermanager.UserConfig;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class AuthProvider implements AuthProviderInterface {
23
24     private static IUserManager um;
25     private static final String DEFAULT_USER = "netconf";
26     private static final String DEFAULT_PASSWORD = "netconf";
27     private String PEM;
28
29     private static final Logger logger =  LoggerFactory.getLogger(AuthProvider.class);
30
31     public AuthProvider(IUserManager ium,InputStream privateKeyFileInputStream) throws Exception {
32
33         AuthProvider.um = ium;
34         if (AuthProvider.um  == null){
35             throw new Exception("No usermanager service available.");
36         }
37
38         List<String> roles = new ArrayList<String>(1);
39         roles.add(UserLevel.SYSTEMADMIN.toString());
40         AuthProvider.um.addLocalUser(new UserConfig(DEFAULT_USER, DEFAULT_PASSWORD, roles));
41
42         try {
43             PEM = IOUtils.toString(privateKeyFileInputStream);
44         } catch (IOException e) {
45             logger.error("Error reading RSA key from file.");
46             throw new IllegalStateException("Error reading RSA key from file.");
47         }
48     }
49     @Override
50     public boolean authenticated(String username, String password)  throws Exception {
51         if (AuthProvider.um  == null){
52             throw new Exception("No usermanager service available.");
53         }
54         AuthResultEnum authResult = AuthProvider.um.authenticate(username,password);
55         if (authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC)){
56             return true;
57         }
58         return false;
59     }
60
61     @Override
62     public char[] getPEMAsCharArray() throws Exception {
63         if (null == PEM){
64             logger.error("Missing RSA key string.");
65             throw new Exception("Missing RSA key.");
66         }
67         return PEM.toCharArray();
68     }
69
70     @Override
71     public void removeUserManagerService() {
72         AuthProvider.um = null;
73     }
74
75     @Override
76     public void addUserManagerService(IUserManager userManagerService) {
77         AuthProvider.um = userManagerService;
78     }
79
80
81 }