| | |
| | | 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; |
| | | import java.io.InputStreamReader; |
| | | import java.io.OutputStreamWriter; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.MalformedURLException; |
| | | import java.net.ProtocolException; |
| | | import java.net.URL; |
| | | import java.net.URLEncoder; |
| | | import java.util.Properties; |
| | |
| | | this.passwordReader = console; |
| | | this.user = user; |
| | | this.runAs = runAs; |
| | | Config config = Config.getInstance(); |
| | | final Config config = Config.getInstance(); |
| | | backendURL = config.getProperty("backendURL", BACKEND_URL); |
| | | loginURL = config.getProperty("loginURL", LOGIN_URL); |
| | | if ("TestUmgebung".equals(loginURL)) { |
| | |
| | | } |
| | | } |
| | | |
| | | public String getTicket() throws JSCliException { |
| | | public String getTicket() throws JSCliException, FileNotFoundException { |
| | | if (grantingTicket != null && grantingTicket.startsWith("ticket:")) { |
| | | return grantingTicket.replaceFirst("ticket", "user"); |
| | | } |
| | |
| | | grantingTicket = doHttpPost(loginURL, encodedParams); |
| | | } catch (UnsupportedEncodingException e) { |
| | | throw new JSCliException(e); |
| | | } catch (FileNotFoundException e) { |
| | | throw new JSCliException("cas server not available: " + loginURL); |
| | | } |
| | | return grantingTicket; |
| | | } |
| | |
| | | return passwordReader.readPassword(); |
| | | } |
| | | |
| | | private String doHttpPost(String urlString, String encodedParams) throws JSCliException { |
| | | private String doHttpPost(final String urlString, final String encodedParams) throws JSCliException, FileNotFoundException { |
| | | String result = null; |
| | | try { |
| | | URL url = new URL(urlString); |
| | | 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); |
| | | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); |
| | | writer.write(encodedParams); |
| | | writer.close(); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| | | String ticket = reader.readLine(); |
| | | String readLine = null; |
| | | do { |
| | | readLine = reader.readLine(); |
| | | } while (readLine != null); |
| | | result = connection.getHeaderField("Location"); |
| | | if (ticket != null && ticket.startsWith("ST-")) { |
| | | result = ticket; |
| | | result = extractTicket(urlString, encodedParams); |
| | | } catch (FileNotFoundException e) { |
| | | grantingTicket = getGrantingTicket(); |
| | | saveProperties(grantingTicket, getTicketFile()); |
| | | try { |
| | | result = extractTicket(grantingTicket, encodedParams); |
| | | } catch (IOException e1) { |
| | | throw new JSCliException(e1); |
| | | } |
| | | } catch (IOException e) { |
| | | throw new JSCliException(e); |
| | |
| | | return result; |
| | | } |
| | | |
| | | private String extractTicket(final String urlString, |
| | | final String encodedParams) throws MalformedURLException, |
| | | IOException, ProtocolException { |
| | | String result; |
| | | final HttpsURLConnection connection = doConnect(urlString, encodedParams); |
| | | final String ticket = readTicket(connection); |
| | | if (ticket != null && ticket.startsWith("ST-")) { |
| | | result = ticket; |
| | | } else { |
| | | result = connection.getHeaderField("Location"); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private String readTicket(final HttpsURLConnection connection) |
| | | throws IOException { |
| | | final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| | | final String ticket = reader.readLine(); |
| | | String readLine = null; |
| | | do { |
| | | readLine = reader.readLine(); |
| | | } while (readLine != null); |
| | | return ticket; |
| | | } |
| | | |
| | | private HttpsURLConnection doConnect(final String urlString, |
| | | final String encodedParams) throws MalformedURLException, |
| | | IOException, ProtocolException { |
| | | final URL url = new URL(urlString); |
| | | 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(encodedParams); |
| | | writer.close(); |
| | | return connection; |
| | | } |
| | | |
| | | private String readFiledGrantingTicket() throws JSCliException { |
| | | String filedTicket = null; |
| | | String userHome = System.getProperty("user.home"); |
| | | String ticketFileName = userHome + "/.hsadmin.tgt"; |
| | | File file = new File(ticketFileName); |
| | | Properties properties = new Properties(); |
| | | if (file.isFile() && file.canRead()) { |
| | | final File file = getTicketFile(); |
| | | final Properties properties = loadProperties(file); |
| | | filedTicket = properties.getProperty(user); |
| | | if (filedTicket == null) { |
| | | filedTicket = getGrantingTicket(); |
| | | saveProperties(filedTicket, file); |
| | | } |
| | | return filedTicket; |
| | | } |
| | | |
| | | private File getTicketFile() { |
| | | final String userHome = System.getProperty("user.home"); |
| | | final String ticketFileName = userHome + "/.hsadmin.tgt"; |
| | | return new File(ticketFileName); |
| | | } |
| | | |
| | | private void saveProperties(final String filedTicket, final File file) throws JSCliException { |
| | | final Properties properties = loadProperties(file); |
| | | if (filedTicket != null) { |
| | | properties.setProperty(user, filedTicket); |
| | | try { |
| | | properties.load(new FileReader(file)); |
| | | filedTicket = properties.getProperty(user); |
| | | properties.store(new FileOutputStream(file), ""); |
| | | } catch (IOException e) { |
| | | throw new JSCliException(e); |
| | | } |
| | | } |
| | | if (filedTicket == null) { |
| | | filedTicket = getGrantingTicket(); |
| | | if (filedTicket != null) { |
| | | properties.setProperty(user, filedTicket); |
| | | try { |
| | | properties.store(new FileOutputStream(file), ""); |
| | | } catch (IOException e) { |
| | | } |
| | | } |
| | | |
| | | private Properties loadProperties(final File file) throws JSCliException { |
| | | final Properties properties = new Properties(); |
| | | if (file.isFile() && file.canRead()) { |
| | | try { |
| | | properties.load(new FileReader(file)); |
| | | } catch (IOException e) { |
| | | throw new JSCliException(e); |
| | | } |
| | | } |
| | | return filedTicket; |
| | | return properties; |
| | | } |
| | | |
| | | @Override |