summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2013-06-17 22:46:37 +0200
committerAndrew Branson <andrew.branson@cern.ch>2013-06-17 22:46:37 +0200
commita3079c033e7923a4076a1cc148c74b5bb86c79a1 (patch)
tree3057eae4ddd8b58c900d2be10b316e5b8e68927b
parent8d242edc79e015d5f54841a771b6222459e86994 (diff)
Authentication plugin for Console
-rw-r--r--src/main/java/com/c2kernel/process/ClientShell.java30
-rw-r--r--src/main/java/com/c2kernel/process/auth/Authenticator.java12
-rw-r--r--src/main/java/com/c2kernel/process/auth/ConsoleAuth.java45
3 files changed, 65 insertions, 22 deletions
diff --git a/src/main/java/com/c2kernel/process/ClientShell.java b/src/main/java/com/c2kernel/process/ClientShell.java
index c20c1d4..f0c3a0d 100644
--- a/src/main/java/com/c2kernel/process/ClientShell.java
+++ b/src/main/java/com/c2kernel/process/ClientShell.java
@@ -3,6 +3,7 @@ package com.c2kernel.process;
import java.util.Scanner;
import com.c2kernel.entity.proxy.AgentProxy;
+import com.c2kernel.process.auth.Authenticator;
import com.c2kernel.scripting.Script;
public class ClientShell extends StandardClient {
@@ -10,8 +11,8 @@ public class ClientShell extends StandardClient {
AgentProxy user;
Script console;
- public ClientShell(String username, String pass) throws Exception {
- user = Gateway.connect(username, pass);
+ public ClientShell(AgentProxy user) throws Exception {
+ this.user = user;
console = new Script("javascript", user, System.out);
}
@@ -38,27 +39,12 @@ public class ClientShell extends StandardClient {
public static void main(String[] args) throws Exception {
Gateway.init(readC2KArgs(args));
- @SuppressWarnings("resource") // closing the scanner closes system.in
- Scanner scan = new Scanner(System.in);
- int loginAttempts = 0;
- ClientShell shell = null;
- while (shell == null && loginAttempts++ < 3) {
- System.out.print("User:");
- String username = scan.nextLine();
- System.out.print("Password:");
- String pass = scan.nextLine();
- try {
- shell = new ClientShell(username, pass);
- } catch (Exception ex) {
- System.err.println(ex.getMessage());
- }
- }
- if (shell == null) {
- System.err.println("Bye");
- System.exit(0);
- }
+ String authClassName = Gateway.getProperty("cli.auth");
+ Class<?> authClass = Class.forName(authClassName);
+ Authenticator auth = (Authenticator)authClass.newInstance();
+ AgentProxy user = auth.authenticate(Gateway.getProperty("Name"));
+ ClientShell shell = new ClientShell(user);
shell.run();
-
}
}
diff --git a/src/main/java/com/c2kernel/process/auth/Authenticator.java b/src/main/java/com/c2kernel/process/auth/Authenticator.java
new file mode 100644
index 0000000..ae18474
--- /dev/null
+++ b/src/main/java/com/c2kernel/process/auth/Authenticator.java
@@ -0,0 +1,12 @@
+package com.c2kernel.process.auth;
+
+import java.util.Properties;
+
+import com.c2kernel.entity.proxy.AgentProxy;
+
+public interface Authenticator {
+
+ public void initialize(Properties props) throws Exception;
+ public AgentProxy authenticate(String resource) throws Exception;
+
+}
diff --git a/src/main/java/com/c2kernel/process/auth/ConsoleAuth.java b/src/main/java/com/c2kernel/process/auth/ConsoleAuth.java
new file mode 100644
index 0000000..a3ea521
--- /dev/null
+++ b/src/main/java/com/c2kernel/process/auth/ConsoleAuth.java
@@ -0,0 +1,45 @@
+package com.c2kernel.process.auth;
+
+import java.util.Properties;
+import java.util.Scanner;
+
+import com.c2kernel.entity.proxy.AgentProxy;
+import com.c2kernel.process.Gateway;
+
+public class ConsoleAuth implements Authenticator {
+
+ public ConsoleAuth() {
+ }
+
+ @Override
+ public void initialize(Properties props) throws Exception {
+
+ }
+ @Override
+ public AgentProxy authenticate(String resource) throws Exception {
+ AgentProxy user = null;
+ if (resource!=null) System.out.println("Please log in"+(resource.length()>0?"to "+resource:""));
+ @SuppressWarnings("resource") // closing the scanner closes system.in
+ Scanner scan = new Scanner(System.in);
+ int loginAttempts = 0;
+ while (user == null && loginAttempts++ < 3) {
+ System.out.print("User:");
+ String username = scan.nextLine();
+ System.out.print("Password:");
+ String pass = scan.nextLine();
+ try {
+ user = Gateway.connect(username, pass);
+ } catch (Exception ex) {
+ System.err.println(ex.getMessage());
+ }
+ }
+
+ if (user == null) {
+ System.err.println("Bye");
+ System.exit(0);
+ }
+ return user;
+
+ }
+
+}