Merge "Add test for generated code checking list of dependencies."
[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.InputStream;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.apache.commons.io.IOUtils;
14 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
15 import org.opendaylight.controller.sal.authorization.UserLevel;
16 import org.opendaylight.controller.usermanager.IUserManager;
17 import org.opendaylight.controller.usermanager.UserConfig;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class AuthProvider implements AuthProviderInterface {
22
23     private static IUserManager um;
24     private static final String DEFAULT_USER = "netconf";
25     private static final String DEFAULT_PASSWORD = "netconf";
26     private static InputStream privateKeyFileInputStream;
27
28     private static final Logger logger =  LoggerFactory.getLogger(AuthProvider.class);
29
30     public AuthProvider(IUserManager ium,InputStream privateKeyFileInputStream) throws Exception {
31
32         this.um = ium;
33         if (this.um  == null){
34             throw new Exception("No usermanager service available.");
35         }
36
37         this.privateKeyFileInputStream = privateKeyFileInputStream;
38
39         List<String> roles = new ArrayList<String>(1);
40         roles.add(UserLevel.SYSTEMADMIN.toString());
41         this.um.addLocalUser(new UserConfig(DEFAULT_USER, DEFAULT_PASSWORD, roles));
42     }
43     @Override
44     public boolean authenticated(String username, String password)  throws Exception {
45         if (this.um  == null){
46             throw new Exception("No usermanager service available.");
47         }
48         AuthResultEnum authResult = this.um.authenticate(username,password);
49         if (authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC)){
50             return true;
51         }
52         return false;
53     }
54
55     @Override
56     public char[] getPEMAsCharArray() throws Exception {
57         char [] PEM  = IOUtils.toCharArray(privateKeyFileInputStream);
58         privateKeyFileInputStream.close();
59         return PEM;
60     }
61
62     @Override
63     public void removeUserManagerService() {
64         this.um = null;
65     }
66
67     @Override
68     public void addUserManagerService(IUserManager userManagerService) {
69         this.um = userManagerService;
70     }
71
72
73 }