From 19c43be1d7cbbbb4c4981f6873f75053ab73aad8 Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Mon, 8 May 2017 19:25:13 +0200 Subject: [PATCH] WebUI mit lokalem Server starten --- .../java/de/hsadmin/jscli/ScriptClient.java | 89 +++++++++--------- .../java/de/hsadmin/jscli/TicketProvider.java | 13 --- .../jscli/cas/TicketProviderFactory.java | 31 ------- .../hsadmin/jscli/console/ConsoleWrapper.java | 12 +-- .../hsadmin/jscli/console/PasswordReader.java | 11 --- .../hsadmin/service/customer/RoleRemote.java | 13 +++ .../hsadmin/service/customer/RoleService.java | 54 +++++++++++ .../service/customer/RoleServiceLocal.java | 10 +++ .../de/hsadmin/service/customer/RoleVO.java | 31 +++++++ .../xmlrpc/webserver/XmlRpcServlet.properties | 1 + .../java/de/hsadmin/login/LoginService.java | 18 +--- .../java/de/hsadmin/login/TicketProvider.java | 11 +++ .../hsadmin/login}/cas/CASTicketProvider.java | 39 ++++---- .../login/{ => cas}/CASTicketValidator.java | 11 +-- .../de/hsadmin/login/cas/PasswordReader.java | 11 +++ .../login}/cas/TestTicketProvider.java | 10 +-- .../login/{ => cas}/TestTicketValidator.java | 4 +- .../login/cas/TicketProviderFactory.java | 24 +++++ .../login/cas/TicketValidatorFactory.java | 23 +++++ web/hsadmin.properties | 5 ++ web/pom.xml | 7 ++ .../de/hsadmin/model/CASTicketService.java | 90 +++++++++++++++++++ .../de/hsadmin/model/TestTicketService.java | 21 +++++ .../java/de/hsadmin/model/TicketService.java | 85 +----------------- .../hsadmin/model/TicketServiceFactory.java | 16 ++++ .../de/hsadmin/rpc/ModulesManagerFactory.java | 6 +- .../de/hsadmin/web/EntryPointsFactory.java | 24 +++-- .../main/java/de/hsadmin/web/HSAdminUI.java | 14 +-- 28 files changed, 435 insertions(+), 249 deletions(-) delete mode 100644 cli/src/main/java/de/hsadmin/jscli/TicketProvider.java delete mode 100644 cli/src/main/java/de/hsadmin/jscli/cas/TicketProviderFactory.java delete mode 100644 cli/src/main/java/de/hsadmin/jscli/console/PasswordReader.java create mode 100644 cust-services/src/main/java/de/hsadmin/service/customer/RoleRemote.java create mode 100644 cust-services/src/main/java/de/hsadmin/service/customer/RoleService.java create mode 100644 cust-services/src/main/java/de/hsadmin/service/customer/RoleServiceLocal.java create mode 100644 cust-services/src/main/java/de/hsadmin/service/customer/RoleVO.java create mode 100644 framework/src/main/java/de/hsadmin/login/TicketProvider.java rename {cli/src/main/java/de/hsadmin/jscli => framework/src/main/java/de/hsadmin/login}/cas/CASTicketProvider.java (82%) rename framework/src/main/java/de/hsadmin/login/{ => cas}/CASTicketValidator.java (80%) create mode 100644 framework/src/main/java/de/hsadmin/login/cas/PasswordReader.java rename {cli/src/main/java/de/hsadmin/jscli => framework/src/main/java/de/hsadmin/login}/cas/TestTicketProvider.java (61%) rename framework/src/main/java/de/hsadmin/login/{ => cas}/TestTicketValidator.java (78%) create mode 100644 framework/src/main/java/de/hsadmin/login/cas/TicketProviderFactory.java create mode 100644 framework/src/main/java/de/hsadmin/login/cas/TicketValidatorFactory.java create mode 100644 web/hsadmin.properties create mode 100644 web/src/main/java/de/hsadmin/model/CASTicketService.java create mode 100644 web/src/main/java/de/hsadmin/model/TestTicketService.java create mode 100644 web/src/main/java/de/hsadmin/model/TicketServiceFactory.java diff --git a/cli/src/main/java/de/hsadmin/jscli/ScriptClient.java b/cli/src/main/java/de/hsadmin/jscli/ScriptClient.java index 0324884..5cc1ef3 100644 --- a/cli/src/main/java/de/hsadmin/jscli/ScriptClient.java +++ b/cli/src/main/java/de/hsadmin/jscli/ScriptClient.java @@ -11,9 +11,10 @@ import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; -import de.hsadmin.jscli.cas.TicketProviderFactory; +import de.hsadmin.common.error.TechnicalException; import de.hsadmin.jscli.console.ConsoleWrapper; import de.hsadmin.jscli.exception.JSCliException; +import de.hsadmin.login.cas.TicketProviderFactory; public class ScriptClient { @@ -22,51 +23,55 @@ public class ScriptClient { public ScriptClient(final ConsoleWrapper console, final String user, final String runAs, final String... arguments) throws JSCliException { final RpcClient rpcClient = new RpcClient(); - engine = new ScriptEngineManager().getEngineByName("js"); - engine.put("casgrantingticket", TicketProviderFactory.getInstance(console, user, runAs)); - engine.put("xmlrpcclient", rpcClient); - engine.put("xmlrpcLastResult", null); - completionStrings = new HashSet(); - completionStrings.add("set"); - completionStrings.add("where"); - considerArguments(arguments); try { - final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js"); - engine.eval(new InputStreamReader(inputResource)); - } catch (ScriptException e) { - throw new JSCliException(e); - } - final List methods = rpcClient.listMethods(); - for (final String method : methods) { - final String[] parts = method.split("\\."); - if (parts.length == 2) { - final String module = parts[0]; - final String function = parts[1]; - if ("system".equals(module) || "getModuleLookup".equals(function) || "createValueObject".equals(function)) { - continue; - } - completionStrings.add(module); - final String jsFunctionIdent; - if ("delete".equals(function)) { - jsFunctionIdent = module + "['remove']"; - completionStrings.add(module + ".remove"); - } else { - jsFunctionIdent = module + "['" + function + "']"; - completionStrings.add(module + "." + function); - } - try { - engine.eval( - "if (typeof " + module + " === 'undefined')" + - " { var " + module + " = { }; };\n" + - jsFunctionIdent + - " = function(json) { return hsaModuleCall('" + module + "', '" + function + "', json); }" - ); - } catch (ScriptException e) { - e.printStackTrace(); + engine = new ScriptEngineManager().getEngineByName("js"); + engine.put("casgrantingticket", TicketProviderFactory.getInstance(console, user, runAs)); + engine.put("xmlrpcclient", rpcClient); + engine.put("xmlrpcLastResult", null); + completionStrings = new HashSet(); + completionStrings.add("set"); + completionStrings.add("where"); + considerArguments(arguments); + try { + final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js"); + engine.eval(new InputStreamReader(inputResource)); + } catch (ScriptException e) { + throw new JSCliException(e); + } + final List methods = rpcClient.listMethods(); + for (final String method : methods) { + final String[] parts = method.split("\\."); + if (parts.length == 2) { + final String module = parts[0]; + final String function = parts[1]; + if ("system".equals(module) || "getModuleLookup".equals(function) || "createValueObject".equals(function)) { + continue; + } + completionStrings.add(module); + final String jsFunctionIdent; + if ("delete".equals(function)) { + jsFunctionIdent = module + "['remove']"; + completionStrings.add(module + ".remove"); + } else { + jsFunctionIdent = module + "['" + function + "']"; + completionStrings.add(module + "." + function); + } + try { + engine.eval( + "if (typeof " + module + " === 'undefined')" + + " { var " + module + " = { }; };\n" + + jsFunctionIdent + + " = function(json) { return hsaModuleCall('" + module + "', '" + function + "', json); }" + ); + } catch (ScriptException e) { + e.printStackTrace(); + } } } + console.codeCompletion(getCodeCompletionStrings()); + } catch (TechnicalException e) { + throw new JSCliException(e); } - console.codeCompletion(getCodeCompletionStrings()); } public String[] getCodeCompletionStrings() { diff --git a/cli/src/main/java/de/hsadmin/jscli/TicketProvider.java b/cli/src/main/java/de/hsadmin/jscli/TicketProvider.java deleted file mode 100644 index 6e50c0c..0000000 --- a/cli/src/main/java/de/hsadmin/jscli/TicketProvider.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.hsadmin.jscli; - -import java.io.FileNotFoundException; - -import de.hsadmin.jscli.exception.JSCliException; - -public interface TicketProvider { - - public String getTicket() throws JSCliException, FileNotFoundException; - - public String getRunAs(); - -} \ No newline at end of file diff --git a/cli/src/main/java/de/hsadmin/jscli/cas/TicketProviderFactory.java b/cli/src/main/java/de/hsadmin/jscli/cas/TicketProviderFactory.java deleted file mode 100644 index 329426e..0000000 --- a/cli/src/main/java/de/hsadmin/jscli/cas/TicketProviderFactory.java +++ /dev/null @@ -1,31 +0,0 @@ -package de.hsadmin.jscli.cas; - -import de.hsadmin.common.config.Config; -import de.hsadmin.common.error.TechnicalException; -import de.hsadmin.jscli.TicketProvider; -import de.hsadmin.jscli.console.PasswordReader; -import de.hsadmin.jscli.exception.JSCliException; - -public class TicketProviderFactory { - - private static final String HOSTSHARING_LOGIN_URL = "https://login.hostsharing.net:443/cas/v1/tickets"; - private static final String HOSTSHARING_BACKEND_URL = "https://config.hostsharing.net:443/hsar/backend"; - - public static TicketProvider getInstance(final PasswordReader console, final String user, final String runAs) throws JSCliException - { - Config config; - try { - config = Config.getInstance(); - final String backendURL = config.getProperty("backendURL", HOSTSHARING_BACKEND_URL); - final String loginURL = config.getProperty("loginURL", HOSTSHARING_LOGIN_URL); - if ("TestUmgebung".equalsIgnoreCase(loginURL)) { - return new TestTicketProvider(user, runAs); - } else { - return new CASTicketProvider(console, user, runAs, backendURL, loginURL); - } - } catch (TechnicalException e) { - throw new JSCliException(e); - } - } - -} diff --git a/cli/src/main/java/de/hsadmin/jscli/console/ConsoleWrapper.java b/cli/src/main/java/de/hsadmin/jscli/console/ConsoleWrapper.java index 1722374..478af11 100644 --- a/cli/src/main/java/de/hsadmin/jscli/console/ConsoleWrapper.java +++ b/cli/src/main/java/de/hsadmin/jscli/console/ConsoleWrapper.java @@ -3,7 +3,9 @@ package de.hsadmin.jscli.console; import java.io.File; import java.io.IOException; +import de.hsadmin.common.error.TechnicalException; import de.hsadmin.jscli.exception.JSCliException; +import de.hsadmin.login.cas.PasswordReader; import jline.console.ConsoleReader; import jline.console.completer.StringsCompleter; import jline.console.history.FileHistory; @@ -38,25 +40,25 @@ public class ConsoleWrapper implements PasswordReader { } } - public void println(final String text) throws JSCliException { + public void println(final String text) throws TechnicalException { try { if (cons != null) { cons.println(text); } else { - throw new JSCliException("cannot write console"); + throw new TechnicalException("cannot write console"); } } catch (IOException e) { - throw new JSCliException(e); + throw new TechnicalException(e); } } - public String readPassword() throws JSCliException { + public String readPassword() throws TechnicalException { try { final String pw = cons.readLine("Password: ", new Character('*')); cons.setPrompt(prompt); return pw; } catch (IOException e) { - throw new JSCliException(e); + throw new TechnicalException(e); } } diff --git a/cli/src/main/java/de/hsadmin/jscli/console/PasswordReader.java b/cli/src/main/java/de/hsadmin/jscli/console/PasswordReader.java deleted file mode 100644 index 95f220d..0000000 --- a/cli/src/main/java/de/hsadmin/jscli/console/PasswordReader.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.hsadmin.jscli.console; - -import de.hsadmin.jscli.exception.JSCliException; - -public interface PasswordReader { - - public String readPassword() throws JSCliException; - - public void println(final String text) throws JSCliException; - -} diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/RoleRemote.java b/cust-services/src/main/java/de/hsadmin/service/customer/RoleRemote.java new file mode 100644 index 0000000..83c3ae3 --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/service/customer/RoleRemote.java @@ -0,0 +1,13 @@ +package de.hsadmin.service.customer; + +import de.hsadmin.xmlrpc.AbstractRemote; +import de.hsadmin.xmlrpc.Remote; + +public class RoleRemote extends AbstractRemote implements Remote { + + @Override + protected String getModuleLookup() { + return "RoleServiceLocal"; + } + +} diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/RoleService.java b/cust-services/src/main/java/de/hsadmin/service/customer/RoleService.java new file mode 100644 index 0000000..e201efc --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/service/customer/RoleService.java @@ -0,0 +1,54 @@ +package de.hsadmin.service.customer; + +import java.util.ArrayList; +import java.util.List; + +import javax.ejb.Stateless; + +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.common.error.UserException; +import de.hsadmin.login.RequestContext; +import de.hsadmin.login.RequiredScope; +import de.hsadmin.login.Role; +import de.hsadmin.login.ScopePolicy; +import de.hsadmin.module.impl.AbstractModule; +import de.hsadmin.module.property.StringProperty; + +@Stateless +public class RoleService extends AbstractModule implements RoleServiceLocal { + + @Override + public RoleVO buildVO() throws TechnicalException { + return new RoleVO(); + } + + @Override + @RequiredScope({ @ScopePolicy(Role.NONE) }) + public RoleVO create(RequestContext requestContext, RoleVO prototype) throws UserException, TechnicalException { + return null; + } + + @Override + @RequiredScope({ @ScopePolicy(Role.ANY) }) + public List read(RequestContext requestContext, RoleVO criteria) throws UserException, TechnicalException { + final Role loginRole = requestContext.getLoginRole(); + final ArrayList userRoles = new ArrayList(); + final RoleVO roleVO = buildVO(); + roleVO.setRole(loginRole.name()); + ((StringProperty) roleVO.get("role")).setValue(loginRole.name()); + userRoles.add(roleVO); + return userRoles; + } + + @Override + @RequiredScope({ @ScopePolicy(Role.NONE) }) + public List update(RequestContext requestContext, RoleVO criteria, RoleVO prototype) + throws UserException, TechnicalException { + return new ArrayList(); + } + + @Override + @RequiredScope({ @ScopePolicy(Role.NONE) }) + public void delete(RequestContext requestContext, RoleVO criteria) throws UserException, TechnicalException { + } +} diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/RoleServiceLocal.java b/cust-services/src/main/java/de/hsadmin/service/customer/RoleServiceLocal.java new file mode 100644 index 0000000..da47c92 --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/service/customer/RoleServiceLocal.java @@ -0,0 +1,10 @@ +package de.hsadmin.service.customer; + +import javax.ejb.Local; + +import de.hsadmin.module.Module; + +@Local +public interface RoleServiceLocal extends Module { + +} diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/RoleVO.java b/cust-services/src/main/java/de/hsadmin/service/customer/RoleVO.java new file mode 100644 index 0000000..d84c7e1 --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/service/customer/RoleVO.java @@ -0,0 +1,31 @@ +package de.hsadmin.service.customer; + +import javax.validation.constraints.Size; + +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.module.ValueObject; +import de.hsadmin.module.impl.AbstractVO; +import de.hsadmin.module.property.ReadWrite; +import de.hsadmin.module.property.ReadWritePolicy; +import de.hsadmin.module.property.Required; + +public class RoleVO extends AbstractVO implements ValueObject { + + @ReadWrite(ReadWritePolicy.READ) + @Required(true) + @Size(min=1,max=24) + private String role; + + public RoleVO() throws TechnicalException { + super(); + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + +} diff --git a/cust-services/src/main/resources/org/apache/xmlrpc/webserver/XmlRpcServlet.properties b/cust-services/src/main/resources/org/apache/xmlrpc/webserver/XmlRpcServlet.properties index f5864b8..8bcb46d 100644 --- a/cust-services/src/main/resources/org/apache/xmlrpc/webserver/XmlRpcServlet.properties +++ b/cust-services/src/main/resources/org/apache/xmlrpc/webserver/XmlRpcServlet.properties @@ -1,3 +1,4 @@ +role=de.hsadmin.service.customer.RoleRemote customer=de.hsadmin.service.customer.CustomerRemote contact=de.hsadmin.service.customer.ContactRemote mandat=de.hsadmin.service.customer.SEPADirectDebitRemote diff --git a/framework/src/main/java/de/hsadmin/login/LoginService.java b/framework/src/main/java/de/hsadmin/login/LoginService.java index 785855d..d113597 100644 --- a/framework/src/main/java/de/hsadmin/login/LoginService.java +++ b/framework/src/main/java/de/hsadmin/login/LoginService.java @@ -8,6 +8,7 @@ import de.hsadmin.common.config.Config; import de.hsadmin.common.error.TechnicalException; import de.hsadmin.common.error.UserError; import de.hsadmin.common.error.UserException; +import de.hsadmin.login.cas.TicketValidatorFactory; @Stateless public class LoginService implements LoginServiceLocal { @@ -15,8 +16,6 @@ public class LoginService implements LoginServiceLocal { @PersistenceContext(name = "hsar") private EntityManager entityManager; - private TicketValidator ticketValidator; - @Override public RequestContext createContext(final String ticket, final String runAsUser) throws UserException, TechnicalException { if (ticket != null && !ticket.isEmpty()) { @@ -74,21 +73,8 @@ public class LoginService implements LoginServiceLocal { } private String validateTicket(final String ticket) throws TechnicalException, UserException { - final TicketValidator ticketValidator = getTicketValidator(); + final TicketValidator ticketValidator = TicketValidatorFactory.getTicketValidator(); return ticketValidator.validate(ticket); } - private TicketValidator getTicketValidator() throws TechnicalException { - if (ticketValidator == null) { - try { - final String property = Config.getInstance().getProperty(Config.TICKETVALIDATOR_CLASS); - final Class validatorClass = Class.forName(property); - ticketValidator = (TicketValidator) validatorClass.newInstance(); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { - throw new TechnicalException(e); - } - } - return ticketValidator; - } - } diff --git a/framework/src/main/java/de/hsadmin/login/TicketProvider.java b/framework/src/main/java/de/hsadmin/login/TicketProvider.java new file mode 100644 index 0000000..32e7561 --- /dev/null +++ b/framework/src/main/java/de/hsadmin/login/TicketProvider.java @@ -0,0 +1,11 @@ +package de.hsadmin.login; + +import de.hsadmin.common.error.TechnicalException; + +public interface TicketProvider { + + public String getTicket() throws TechnicalException; + + public String getRunAs(); + +} \ No newline at end of file diff --git a/cli/src/main/java/de/hsadmin/jscli/cas/CASTicketProvider.java b/framework/src/main/java/de/hsadmin/login/cas/CASTicketProvider.java similarity index 82% rename from cli/src/main/java/de/hsadmin/jscli/cas/CASTicketProvider.java rename to framework/src/main/java/de/hsadmin/login/cas/CASTicketProvider.java index 66f763a..5432331 100644 --- a/cli/src/main/java/de/hsadmin/jscli/cas/CASTicketProvider.java +++ b/framework/src/main/java/de/hsadmin/login/cas/CASTicketProvider.java @@ -1,9 +1,8 @@ -package de.hsadmin.jscli.cas; +package de.hsadmin.login.cas; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; @@ -20,9 +19,7 @@ import javax.net.ssl.HttpsURLConnection; import de.hsadmin.common.config.Config; import de.hsadmin.common.error.TechnicalException; -import de.hsadmin.jscli.TicketProvider; -import de.hsadmin.jscli.console.PasswordReader; -import de.hsadmin.jscli.exception.JSCliException; +import de.hsadmin.login.TicketProvider; public class CASTicketProvider implements TicketProvider { @@ -34,7 +31,7 @@ public class CASTicketProvider implements TicketProvider { private String grantingTicket; - public CASTicketProvider(final PasswordReader console, final String user, final String runAs, final String backendURL, final String loginURL) throws JSCliException { + public CASTicketProvider(final PasswordReader console, final String user, final String runAs, final String backendURL, final String loginURL) throws TechnicalException { this.passwordReader = console; this.user = user; this.runAs = runAs; @@ -44,7 +41,7 @@ public class CASTicketProvider implements TicketProvider { } @Override - public String getTicket() throws JSCliException { + public String getTicket() throws TechnicalException { try { final String encodedParams = URLEncoder.encode("service", "UTF-8") + "=" + URLEncoder.encode(backendURL, "UTF-8"); @@ -64,14 +61,14 @@ public class CASTicketProvider implements TicketProvider { } trails++; if (trails > 3) { - throw new JSCliException("exceeded number of login attempts"); + throw new TechnicalException("exceeded number of login attempts"); } urlString = getGrantingTicket(); } } return result; } catch (UnsupportedEncodingException e) { - throw new JSCliException(e); + throw new TechnicalException(e); } } @@ -80,7 +77,7 @@ public class CASTicketProvider implements TicketProvider { return runAs; } - private String getGrantingTicket() throws JSCliException { + private String getGrantingTicket() throws TechnicalException { grantingTicket = null; try { String password = Config.getInstance().getProperty(user + ".passWord"); @@ -93,25 +90,21 @@ public class CASTicketProvider implements TicketProvider { + "=" + URLEncoder.encode(password, "UTF-8"); grantingTicket = requestForGrantingTicket(loginURL, encodedParams); } catch (UnsupportedEncodingException e) { - throw new JSCliException(e); - } catch (FileNotFoundException e) { - throw new JSCliException("cas server not available: " + loginURL); - } catch (TechnicalException e) { - throw new JSCliException(e); + throw new TechnicalException(e); } return grantingTicket; } - private String readPasswordFromConsole() throws JSCliException { + private String readPasswordFromConsole() throws TechnicalException { return passwordReader.readPassword(); } - private String requestForGrantingTicket(final String urlString, final String encodedParams) throws JSCliException, FileNotFoundException { + private String requestForGrantingTicket(final String urlString, final String encodedParams) throws TechnicalException { try { final HttpsURLConnection connection = doConnect(urlString, encodedParams); return connection.getHeaderField("Location"); } catch (IOException e) { - throw new JSCliException(e); + throw new TechnicalException(e); } } @@ -148,7 +141,7 @@ public class CASTicketProvider implements TicketProvider { return connection; } - private void readFiledGrantingTicket() throws JSCliException { + private void readFiledGrantingTicket() throws TechnicalException { final File file = getTicketFile(); final Properties properties = loadProperties(file); final String filedTicket = properties.getProperty(user); @@ -162,25 +155,25 @@ public class CASTicketProvider implements TicketProvider { return new File(ticketFileName); } - private void saveProperties(final String filedTicket, final File file) throws JSCliException { + private void saveProperties(final String filedTicket, final File file) throws TechnicalException { final Properties properties = loadProperties(file); if (filedTicket != null) { properties.setProperty(user, filedTicket); try { properties.store(new FileOutputStream(file), "stored cas tickets"); } catch (IOException e) { - throw new JSCliException(e); + throw new TechnicalException(e); } } } - private Properties loadProperties(final File file) throws JSCliException { + private Properties loadProperties(final File file) throws TechnicalException { final Properties properties = new Properties(); if (file.isFile() && file.canRead()) { try { properties.load(new FileReader(file)); } catch (IOException e) { - throw new JSCliException(e); + throw new TechnicalException(e); } } return properties; diff --git a/framework/src/main/java/de/hsadmin/login/CASTicketValidator.java b/framework/src/main/java/de/hsadmin/login/cas/CASTicketValidator.java similarity index 80% rename from framework/src/main/java/de/hsadmin/login/CASTicketValidator.java rename to framework/src/main/java/de/hsadmin/login/cas/CASTicketValidator.java index 0fb3464..96e6039 100644 --- a/framework/src/main/java/de/hsadmin/login/CASTicketValidator.java +++ b/framework/src/main/java/de/hsadmin/login/cas/CASTicketValidator.java @@ -1,4 +1,4 @@ -package de.hsadmin.login; +package de.hsadmin.login.cas; import java.io.BufferedReader; import java.io.IOException; @@ -11,6 +11,7 @@ import de.hsadmin.common.config.Config; import de.hsadmin.common.error.TechnicalException; import de.hsadmin.common.error.UserError; import de.hsadmin.common.error.UserException; +import de.hsadmin.login.TicketValidator; public class CASTicketValidator implements TicketValidator { @@ -29,11 +30,11 @@ public class CASTicketValidator implements TicketValidator { throw new TechnicalException("TicketValidator is not initialized."); } try { - URL url = new URL(proxyValidateURL + "?service=" + proxyServiceURL + "&ticket=" + ticket); - URLConnection httpConnection = url.openConnection(); + final URL url = new URL(proxyValidateURL + "?service=" + proxyServiceURL + "&ticket=" + ticket); + final URLConnection httpConnection = url.openConnection(); httpConnection.connect(); - InputStream inputStream = httpConnection.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + final InputStream inputStream = httpConnection.getInputStream(); + final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String nextLine = reader.readLine(); while (nextLine != null) { if (nextLine.contains("")) { diff --git a/framework/src/main/java/de/hsadmin/login/cas/PasswordReader.java b/framework/src/main/java/de/hsadmin/login/cas/PasswordReader.java new file mode 100644 index 0000000..c5726af --- /dev/null +++ b/framework/src/main/java/de/hsadmin/login/cas/PasswordReader.java @@ -0,0 +1,11 @@ +package de.hsadmin.login.cas; + +import de.hsadmin.common.error.TechnicalException; + +public interface PasswordReader { + + public String readPassword() throws TechnicalException; + + public void println(final String text) throws TechnicalException; + +} diff --git a/cli/src/main/java/de/hsadmin/jscli/cas/TestTicketProvider.java b/framework/src/main/java/de/hsadmin/login/cas/TestTicketProvider.java similarity index 61% rename from cli/src/main/java/de/hsadmin/jscli/cas/TestTicketProvider.java rename to framework/src/main/java/de/hsadmin/login/cas/TestTicketProvider.java index 3bb3f67..4e42af2 100644 --- a/cli/src/main/java/de/hsadmin/jscli/cas/TestTicketProvider.java +++ b/framework/src/main/java/de/hsadmin/login/cas/TestTicketProvider.java @@ -1,9 +1,7 @@ -package de.hsadmin.jscli.cas; +package de.hsadmin.login.cas; -import java.io.FileNotFoundException; - -import de.hsadmin.jscli.TicketProvider; -import de.hsadmin.jscli.exception.JSCliException; +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.login.TicketProvider; public class TestTicketProvider implements TicketProvider { @@ -16,7 +14,7 @@ public class TestTicketProvider implements TicketProvider { } @Override - public String getTicket() throws JSCliException, FileNotFoundException { + public String getTicket() throws TechnicalException { return grantingTicket; } diff --git a/framework/src/main/java/de/hsadmin/login/TestTicketValidator.java b/framework/src/main/java/de/hsadmin/login/cas/TestTicketValidator.java similarity index 78% rename from framework/src/main/java/de/hsadmin/login/TestTicketValidator.java rename to framework/src/main/java/de/hsadmin/login/cas/TestTicketValidator.java index 37a6bf4..d03d209 100644 --- a/framework/src/main/java/de/hsadmin/login/TestTicketValidator.java +++ b/framework/src/main/java/de/hsadmin/login/cas/TestTicketValidator.java @@ -1,4 +1,6 @@ -package de.hsadmin.login; +package de.hsadmin.login.cas; + +import de.hsadmin.login.TicketValidator; public class TestTicketValidator implements TicketValidator { diff --git a/framework/src/main/java/de/hsadmin/login/cas/TicketProviderFactory.java b/framework/src/main/java/de/hsadmin/login/cas/TicketProviderFactory.java new file mode 100644 index 0000000..f994e60 --- /dev/null +++ b/framework/src/main/java/de/hsadmin/login/cas/TicketProviderFactory.java @@ -0,0 +1,24 @@ +package de.hsadmin.login.cas; + +import de.hsadmin.common.config.Config; +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.login.TicketProvider; + +public class TicketProviderFactory { + + private static final String HOSTSHARING_LOGIN_URL = "https://login.hostsharing.net:443/cas/v1/tickets"; + private static final String HOSTSHARING_BACKEND_URL = "https://config.hostsharing.net:443/hsar/backend"; + + public static TicketProvider getInstance(final PasswordReader console, final String user, final String runAs) throws TechnicalException + { + final Config config = Config.getInstance(); + final String backendURL = config.getProperty("backendURL", HOSTSHARING_BACKEND_URL); + final String loginURL = config.getProperty("loginURL", HOSTSHARING_LOGIN_URL); + if ("TestUmgebung".equalsIgnoreCase(loginURL)) { + return new TestTicketProvider(user, runAs); + } else { + return new CASTicketProvider(console, user, runAs, backendURL, loginURL); + } + } + +} diff --git a/framework/src/main/java/de/hsadmin/login/cas/TicketValidatorFactory.java b/framework/src/main/java/de/hsadmin/login/cas/TicketValidatorFactory.java new file mode 100644 index 0000000..ebfe311 --- /dev/null +++ b/framework/src/main/java/de/hsadmin/login/cas/TicketValidatorFactory.java @@ -0,0 +1,23 @@ +package de.hsadmin.login.cas; + +import de.hsadmin.common.config.Config; +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.login.TicketValidator; + +public class TicketValidatorFactory { + + public static TicketValidator getTicketValidator() throws TechnicalException { + TicketValidator ticketValidator = null; + if (ticketValidator == null) { + try { + final String property = Config.getInstance().getProperty(Config.TICKETVALIDATOR_CLASS); + final Class validatorClass = Class.forName(property); + ticketValidator = (TicketValidator) validatorClass.newInstance(); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { + throw new TechnicalException(e); + } + } + return ticketValidator; + } + +} diff --git a/web/hsadmin.properties b/web/hsadmin.properties new file mode 100644 index 0000000..785c811 --- /dev/null +++ b/web/hsadmin.properties @@ -0,0 +1,5 @@ +backendURL=https://config.hostsharing.net:443/hsar/backend +;xmlrpcURL=https://config.hostsharing.net:443/hsar/xmlrpc/hsadmin +;loginURL=https://login.hostsharing.net:443/cas/v1/tickets +xmlrpcURL=http://localhost:8080/cust-webapp/xmlrpc/hsadmin +loginURL=TestUmgebung diff --git a/web/pom.xml b/web/pom.xml index 059ba5c..58ca7ea 100644 --- a/web/pom.xml +++ b/web/pom.xml @@ -25,6 +25,12 @@ de.hsadmin framework 1.0-SNAPSHOT + + + org.apache.xmlrpc + xmlrpc-server + + @@ -96,6 +102,7 @@ 2.2 / + 8081 diff --git a/web/src/main/java/de/hsadmin/model/CASTicketService.java b/web/src/main/java/de/hsadmin/model/CASTicketService.java new file mode 100644 index 0000000..4813e5f --- /dev/null +++ b/web/src/main/java/de/hsadmin/model/CASTicketService.java @@ -0,0 +1,90 @@ +package de.hsadmin.model; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Serializable; +import java.net.URL; +import java.net.URLEncoder; + +import javax.net.ssl.HttpsURLConnection; + +import de.hsadmin.common.config.Config; +import de.hsadmin.rpc.RpcException; + +/** + * Helper for service tickets. + * Hostsharing uses the CAS authentication service to authenticate + * users of hostsharing services. This class is used to create a + * "ticket granting ticket" for a session and service ticket for + * individual service calls. + */ +class CASTicketService implements Serializable, TicketService { + + private static final long serialVersionUID = 1L; + + @Override + public String getGrantingTicket(final String user, final String password) throws RpcException { + String ticket = null; + try { + final Config conf = Config.getInstance(); + final String userParam = "username=" + URLEncoder.encode(user, "UTF-8"); + final String passwordParam = "password=" + URLEncoder.encode(password, "UTF-8"); + final String encodedData = userParam + "&" + passwordParam; + final String casUrl = conf.getProperty("loginURL"); + final URL url = new URL(casUrl); + + final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.setUseCaches(false); + connection.setAllowUserInteraction(false); + final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); + writer.write(encodedData); + writer.close(); + connection.connect(); + ticket = connection.getHeaderField("Location"); + } catch (Exception e) { + throw new RpcException(e); + } + + return ticket; + } + + @Override + public String getServiceTicket(String grantingTicket) throws RpcException { + String ticket = null; + try { + final Config conf = Config.getInstance(); + final String backendURL = conf.getProperty("backendURL"); + final String serviceParam = "service=" + URLEncoder.encode(backendURL, "UTF-8"); + final URL url = new URL(grantingTicket); + + final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.setUseCaches(false); + connection.setAllowUserInteraction(false); + final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); + writer.write(serviceParam); + writer.close(); + connection.connect(); + final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + ticket = reader.readLine(); + String readLine = reader.readLine(); + do { + readLine = reader.readLine(); + } while (readLine != null); + } catch (Exception e) { + throw new RpcException(e); + } + + return ticket; + } + +} diff --git a/web/src/main/java/de/hsadmin/model/TestTicketService.java b/web/src/main/java/de/hsadmin/model/TestTicketService.java new file mode 100644 index 0000000..e257ddd --- /dev/null +++ b/web/src/main/java/de/hsadmin/model/TestTicketService.java @@ -0,0 +1,21 @@ +package de.hsadmin.model; + +import java.io.Serializable; + +import de.hsadmin.rpc.RpcException; + +public class TestTicketService implements TicketService, Serializable { + + private static final long serialVersionUID = 1L; + + @Override + public String getGrantingTicket(final String user, final String password) throws RpcException { + return "granting:" + user; + } + + @Override + public String getServiceTicket(final String grantingTicket) throws RpcException { + return "user" + grantingTicket.substring(grantingTicket.indexOf(':')); + } + +} diff --git a/web/src/main/java/de/hsadmin/model/TicketService.java b/web/src/main/java/de/hsadmin/model/TicketService.java index e98b0ff..2579227 100644 --- a/web/src/main/java/de/hsadmin/model/TicketService.java +++ b/web/src/main/java/de/hsadmin/model/TicketService.java @@ -1,88 +1,11 @@ package de.hsadmin.model; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Serializable; -import java.net.URL; -import java.net.URLEncoder; - -import javax.net.ssl.HttpsURLConnection; - -import de.hsadmin.common.config.Config; import de.hsadmin.rpc.RpcException; -/** - * Helper for service tickets. - * Hostsharing uses the CAS authentication service to authenticate - * users of hostsharing services. This class is used to create a - * "ticket granting ticket" for a session and service ticket for - * individual service calls. - */ -public class TicketService implements Serializable { +public interface TicketService { - private static final long serialVersionUID = 1L; + String getGrantingTicket(String user, String password) throws RpcException; - public String getGrantingTicket(final String user, final String password) throws RpcException { - String ticket = null; - try { - final Config conf = Config.getInstance(); - final String userParam = "username=" + URLEncoder.encode(user, "UTF-8"); - final String passwordParam = "password=" + URLEncoder.encode(password, "UTF-8"); - final String encodedData = userParam + "&" + passwordParam; - final String casUrl = conf.getProperty("loginURL"); - final URL url = new URL(casUrl); + String getServiceTicket(String grantingTicket) throws RpcException; - final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); - connection.setDoInput(true); - connection.setDoOutput(true); - connection.setUseCaches(false); - connection.setAllowUserInteraction(false); - final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); - writer.write(encodedData); - writer.close(); - connection.connect(); - ticket = connection.getHeaderField("Location"); - } catch (Exception e) { - throw new RpcException(e); - } - - return ticket; - } - - public String getServiceTicket(String grantingTicket) throws RpcException { - String ticket = null; - try { - final Config conf = Config.getInstance(); - final String backendURL = conf.getProperty("backendURL"); - final String serviceParam = "service=" + URLEncoder.encode(backendURL, "UTF-8"); - final URL url = new URL(grantingTicket); - - final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); - connection.setDoInput(true); - connection.setDoOutput(true); - connection.setUseCaches(false); - connection.setAllowUserInteraction(false); - final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); - writer.write(serviceParam); - writer.close(); - connection.connect(); - final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - ticket = reader.readLine(); - String readLine = reader.readLine(); - do { - readLine = reader.readLine(); - } while (readLine != null); - } catch (Exception e) { - throw new RpcException(e); - } - - return ticket; - } - -} +} \ No newline at end of file diff --git a/web/src/main/java/de/hsadmin/model/TicketServiceFactory.java b/web/src/main/java/de/hsadmin/model/TicketServiceFactory.java new file mode 100644 index 0000000..022f320 --- /dev/null +++ b/web/src/main/java/de/hsadmin/model/TicketServiceFactory.java @@ -0,0 +1,16 @@ +package de.hsadmin.model; + +import de.hsadmin.common.config.Config; +import de.hsadmin.common.error.TechnicalException; + +public class TicketServiceFactory { + + public static TicketService getTicketService() throws TechnicalException { + final String loginURL = Config.getInstance().getProperty("loginURL"); + if ("TestUmgebung".equals(loginURL)) { + return new TestTicketService(); + } + return new CASTicketService(); + } + +} diff --git a/web/src/main/java/de/hsadmin/rpc/ModulesManagerFactory.java b/web/src/main/java/de/hsadmin/rpc/ModulesManagerFactory.java index ed79a8b..b1d853c 100644 --- a/web/src/main/java/de/hsadmin/rpc/ModulesManagerFactory.java +++ b/web/src/main/java/de/hsadmin/rpc/ModulesManagerFactory.java @@ -12,7 +12,9 @@ import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; +import de.hsadmin.common.error.TechnicalException; import de.hsadmin.model.TicketService; +import de.hsadmin.model.TicketServiceFactory; public class ModulesManagerFactory { @@ -26,8 +28,8 @@ public class ModulesManagerFactory { public ModulesManager newModulesManager(final String... serverURLs) throws RpcException { final ModulesManager moduleManager = new ModulesManager(); - final TicketService ticketService = new TicketService(); try { + final TicketService ticketService = TicketServiceFactory.getTicketService();; for (final String servername : serverURLs) { final XmlRpcClient rpcClient = new XmlRpcClient(); @@ -63,7 +65,7 @@ public class ModulesManagerFactory { moduleManager.module(moduleName).add(propInfo); } } - } catch (MalformedURLException | XmlRpcException e) { + } catch (MalformedURLException | XmlRpcException | TechnicalException e) { throw new RpcException(e); } return moduleManager; diff --git a/web/src/main/java/de/hsadmin/web/EntryPointsFactory.java b/web/src/main/java/de/hsadmin/web/EntryPointsFactory.java index 61efa20..638fef4 100644 --- a/web/src/main/java/de/hsadmin/web/EntryPointsFactory.java +++ b/web/src/main/java/de/hsadmin/web/EntryPointsFactory.java @@ -1,20 +1,28 @@ package de.hsadmin.web; +import de.hsadmin.login.Role; + public class EntryPointsFactory extends AbstractEntryPointsFactory { +// FIXME: Domain-Modul zum Testen auskommentiert (ph) + @Override public String[] getEntryPointNames(final String role) { - if ("HOSTMASTER".equals(role)) { - return new String[] { "customer", "pac", "domain" }; + + if (Role.SYSTEM.name().equals(role)) { + return new String[] { "customer", "pac" }; +// return new String[] { "customer", "pac", "domain" }; } - if ("CUSTOMER".equals(role)) { - return new String[] { "customer", "pac", "domain" }; + if (Role.CUSTOMER.name().equals(role)) { + return new String[] { "customer", "pac" }; +// return new String[] { "customer", "pac", "domain" }; } - if ("PAC_ADMIN_DW".equals(role)) { - return new String[] { "pac", "domain" }; + if (Role.PACKET.name().equals(role)) { + return new String[] { "pac" }; +// return new String[] { "pac", "domain" }; } - if ("DOM_ADMIN".equals(role)) { - return new String[] { "domain" }; + if (Role.DOMAIN.name().equals(role)) { +// return new String[] { "domain" }; } return new String[] { }; } diff --git a/web/src/main/java/de/hsadmin/web/HSAdminUI.java b/web/src/main/java/de/hsadmin/web/HSAdminUI.java index eba84b1..8659fed 100644 --- a/web/src/main/java/de/hsadmin/web/HSAdminUI.java +++ b/web/src/main/java/de/hsadmin/web/HSAdminUI.java @@ -11,7 +11,9 @@ import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.themes.ValoTheme; +import de.hsadmin.common.error.TechnicalException; import de.hsadmin.model.TicketService; +import de.hsadmin.model.TicketServiceFactory; @Title("HSAdmin Web") @Theme(ValoTheme.THEME_NAME) @@ -23,7 +25,7 @@ public class HSAdminUI extends UI { @VaadinServletConfiguration(productionMode = false, ui = HSAdminUI.class) public static class Servlet extends VaadinServlet { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; } @@ -35,10 +37,12 @@ public class HSAdminUI extends UI { setSizeFull(); layout = new VerticalLayout(); layout.setSizeFull(); - - ticketService = new TicketService(); - UI.getCurrent().addWindow(new LoginWindow(this, ticketService)); - + try { + ticketService = TicketServiceFactory.getTicketService(); + UI.getCurrent().addWindow(new LoginWindow(this, ticketService)); + } catch (TechnicalException e) { + // dont care + } setContent(layout); }