fix some problem storing granting tickets in file

This commit is contained in:
Peter Hormanns 2014-06-17 17:31:48 +02:00
parent 1b01d43a61
commit 22b26ccad7
2 changed files with 91 additions and 41 deletions

View File

@ -31,10 +31,10 @@
<copy todir="${build.home}/cls">
<fileset dir="${resource.home}" />
</copy>
<jar destfile="build/hsadmin.jar" basedir="build/cls">
<jar destfile="build/hsscript.jar" basedir="build/cls">
<manifest>
<attribute name="Main-Class" value="de.hsadmin.jscli.Main"/>
<attribute name="Class-Path" value="../lib/commons-cli-1.2.jar ../lib/jline-1.0.jar ../lib/ws-commons-util-1.0.1.jar ../lib/xmlrpc-client-3.1.jar ../lib/xmlrpc-common-3.1.jar"/>
<attribute name="Class-Path" value="lib/commons-cli-1.2.jar lib/jline-1.0.jar lib/ws-commons-util-1.0.1.jar lib/xmlrpc-client-3.1.jar lib/xmlrpc-common-3.1.jar"/>
</manifest>
</jar>
</target>

View File

@ -3,12 +3,15 @@ package de.hsadmin.jscli.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;
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;
@ -36,7 +39,7 @@ public class CASTicketProvider {
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)) {
@ -46,7 +49,7 @@ public class CASTicketProvider {
}
}
public String getTicket() throws JSCliException {
public String getTicket() throws JSCliException, FileNotFoundException {
if (grantingTicket != null && grantingTicket.startsWith("ticket:")) {
return grantingTicket.replaceFirst("ticket", "user");
}
@ -77,6 +80,8 @@ public class CASTicketProvider {
grantingTicket = doHttpPost(loginURL, encodedParams);
} catch (UnsupportedEncodingException e) {
throw new JSCliException(e);
} catch (FileNotFoundException e) {
throw new JSCliException("cas server not available: " + loginURL);
}
return grantingTicket;
}
@ -85,29 +90,17 @@ public class CASTicketProvider {
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);
@ -115,31 +108,88 @@ public class CASTicketProvider {
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