Merge "Resolve Bug:853 - remove groovy from config code generator."
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / authentication / AuthProvider.java
index 22dda95064c092c286a1046edc90595943485a0d..6ddc8ebb55752dca439e63ee3e70dfa260e79783 100644 (file)
@@ -7,67 +7,57 @@
  */
 package org.opendaylight.controller.netconf.ssh.authentication;
 
-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 java.util.ArrayList;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
 
 public class AuthProvider implements AuthProviderInterface {
 
-    private static IUserManager um;
+    private static IUserManager um; //FIXME static mutable state, no locks
     private static final String DEFAULT_USER = "netconf";
     private static final String DEFAULT_PASSWORD = "netconf";
-    private static InputStream privateKeyFileInputStream;
-
-    private static final Logger logger =  LoggerFactory.getLogger(AuthProvider.class);
+    private final String pem;
 
-    public AuthProvider(IUserManager ium,InputStream privateKeyFileInputStream) throws Exception {
-
-        this.um = ium;
-        if (this.um  == null){
+    public AuthProvider(IUserManager ium, String pemCertificate) throws Exception {
+        checkNotNull(pemCertificate, "Parameter 'pemCertificate' is null");
+        AuthProvider.um = ium;
+        if (AuthProvider.um == null) {
             throw new Exception("No usermanager service available.");
         }
 
-        this.privateKeyFileInputStream = privateKeyFileInputStream;
-
         List<String> roles = new ArrayList<String>(1);
         roles.add(UserLevel.SYSTEMADMIN.toString());
-        this.um.addLocalUser(new UserConfig(DEFAULT_USER, DEFAULT_PASSWORD, roles));
+        AuthProvider.um.addLocalUser(new UserConfig(DEFAULT_USER, DEFAULT_PASSWORD, roles)); //FIXME hardcoded auth
+        pem = pemCertificate;
     }
+
     @Override
-    public boolean authenticated(String username, String password)  throws Exception {
-        if (this.um  == null){
-            throw new Exception("No usermanager service available.");
+    public boolean authenticated(String username, String password) {
+        if (AuthProvider.um == null) {
+            throw new IllegalStateException("No usermanager service available.");
         }
-        AuthResultEnum authResult = this.um.authenticate(username,password);
-        if (authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC)){
-            return true;
-        }
-        return false;
+        AuthResultEnum authResult = AuthProvider.um.authenticate(username, password);
+        return authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC);
     }
 
     @Override
-    public char[] getPEMAsCharArray() throws Exception {
-        char [] PEM  = IOUtils.toCharArray(privateKeyFileInputStream);
-        privateKeyFileInputStream.close();
-        return PEM;
+    public char[] getPEMAsCharArray() {
+        return pem.toCharArray();
     }
 
     @Override
     public void removeUserManagerService() {
-        this.um = null;
+        AuthProvider.um = null;
     }
 
     @Override
     public void addUserManagerService(IUserManager userManagerService) {
-        this.um = userManagerService;
+        AuthProvider.um = userManagerService;
     }
-
-
 }