Merge "Refactor Subnet.isSubnetOf - reduce number of 'if' statements. Added unitests."
[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 ch.ethz.ssh2.signature.RSAPrivateKey;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.apache.commons.io.IOUtils;
16 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
17 import org.opendaylight.controller.sal.authorization.UserLevel;
18 import org.opendaylight.controller.usermanager.IUserManager;
19 import org.opendaylight.controller.usermanager.UserConfig;
20
21 public class AuthProvider implements AuthProviderInterface {
22
23     private static RSAPrivateKey hostkey = null;
24     private static IUserManager um;
25     private static final String DEAFULT_USER = "netconf";
26     private static final String DEAFULT_PASSWORD = "netconf";
27
28
29     public AuthProvider(IUserManager ium) throws Exception {
30
31         this.um = ium;
32
33         if (this.um  == null){
34             throw new Exception("No usermanager service available.");
35         }
36
37         List<String> roles = new ArrayList<String>(1);
38         roles.add(UserLevel.SYSTEMADMIN.toString());
39         this.um.addLocalUser(new UserConfig(DEAFULT_USER, DEAFULT_PASSWORD, roles));
40     }
41     @Override
42     public boolean authenticated(String username, String password)  throws Exception {
43         if (this.um  == null){
44             throw new Exception("No usermanager service available.");
45         }
46         AuthResultEnum authResult = this.um.authenticate(username,password);
47         if (authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC)){
48             return true;
49         }
50         return false;
51     }
52
53     @Override
54     public char[] getPEMAsCharArray() {
55
56         InputStream is = getClass().getResourceAsStream("/RSA.pk");
57         try {
58             return IOUtils.toCharArray(is);
59         } catch (IOException e) {
60             e.printStackTrace();
61         }
62         return null;
63     }
64
65     @Override
66     public void removeUserManagerService() {
67         this.um = null;
68     }
69
70     @Override
71     public void addUserManagerService(IUserManager userManagerService) {
72         this.um = userManagerService;
73     }
74
75
76 }