2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.ssh.authentication;
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;
22 public class AuthProvider implements AuthProviderInterface {
24 private static IUserManager um;
25 private static final String DEFAULT_USER = "netconf";
26 private static final String DEFAULT_PASSWORD = "netconf";
29 private static final Logger logger = LoggerFactory.getLogger(AuthProvider.class);
31 public AuthProvider(IUserManager ium,InputStream privateKeyFileInputStream) throws Exception {
35 throw new Exception("No usermanager service available.");
38 List<String> roles = new ArrayList<String>(1);
39 roles.add(UserLevel.SYSTEMADMIN.toString());
40 this.um.addLocalUser(new UserConfig(DEFAULT_USER, DEFAULT_PASSWORD, roles));
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.");
50 public boolean authenticated(String username, String password) throws Exception {
52 throw new Exception("No usermanager service available.");
54 AuthResultEnum authResult = this.um.authenticate(username,password);
55 if (authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC)){
62 public char[] getPEMAsCharArray() throws Exception {
64 logger.error("Missing RSA key string.");
65 throw new Exception("Missing RSA key.");
67 return PEM.toCharArray();
71 public void removeUserManagerService() {
76 public void addUserManagerService(IUserManager userManagerService) {
77 this.um = userManagerService;