Merge "Fixed typo in SnapshotBackedWriteTransaction class"
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / authentication / AuthProvider.java
index 3d5318073d8c0e7cc203fd80fa63b6a05ea71e3e..2e9a0b9d8bbd256154ff82689e85e556b60c9e90 100644 (file)
@@ -8,74 +8,40 @@
 package org.opendaylight.controller.netconf.ssh.authentication;
 
 import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
-import org.apache.commons.io.IOUtils;
 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
-import org.opendaylight.controller.sal.authorization.UserLevel;
 import org.opendaylight.controller.usermanager.IUserManager;
-import org.opendaylight.controller.usermanager.UserConfig;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import static com.google.common.base.Preconditions.checkNotNull;
 
 public class AuthProvider implements AuthProviderInterface {
 
-    private static IUserManager um;
-    private static final String DEFAULT_USER = "netconf";
-    private static final String DEFAULT_PASSWORD = "netconf";
-    private String PEM;
+    private IUserManager um;
+    private final String pem;
 
-    private static final Logger logger =  LoggerFactory.getLogger(AuthProvider.class);
-
-    public AuthProvider(IUserManager ium,InputStream privateKeyFileInputStream) throws Exception {
-
-        AuthProvider.um = ium;
-        if (AuthProvider.um  == null){
-            throw new Exception("No usermanager service available.");
-        }
-
-        List<String> roles = new ArrayList<String>(1);
-        roles.add(UserLevel.SYSTEMADMIN.toString());
-        AuthProvider.um.addLocalUser(new UserConfig(DEFAULT_USER, DEFAULT_PASSWORD, roles));
-
-        try {
-            PEM = IOUtils.toString(privateKeyFileInputStream);
-        } catch (IOException e) {
-            logger.error("Error reading RSA key from file.");
-            throw new IllegalStateException("Error reading RSA key from file.");
-        }
+    public AuthProvider(IUserManager ium, String pemCertificate) throws IllegalArgumentException, IOException {
+        checkNotNull(pemCertificate, "Parameter 'pemCertificate' is null");
+        checkNotNull(ium, "No user manager service available.");
+        this.um = ium;
+        pem = pemCertificate;
     }
+
     @Override
-    public boolean authenticated(String username, String password)  throws Exception {
-        if (AuthProvider.um  == null){
-            throw new Exception("No usermanager service available.");
-        }
-        AuthResultEnum authResult = AuthProvider.um.authenticate(username,password);
-        if (authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC)){
-            return true;
-        }
-        return false;
+    public boolean authenticated(String username, String password) {
+        AuthResultEnum authResult = this.um.authenticate(username, password);
+        return authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC);
     }
 
     @Override
-    public char[] getPEMAsCharArray() throws Exception {
-        if (null == PEM){
-            logger.error("Missing RSA key string.");
-            throw new Exception("Missing RSA key.");
-        }
-        return PEM.toCharArray();
+    public char[] getPEMAsCharArray() {
+        return pem.toCharArray();
     }
 
     @Override
     public void removeUserManagerService() {
-        AuthProvider.um = null;
+        this.um = null;
     }
 
     @Override
     public void addUserManagerService(IUserManager userManagerService) {
-        AuthProvider.um = userManagerService;
+        this.um = userManagerService;
     }
-
-
 }