fix some problem storing granting tickets in file
This commit is contained in:
parent
1b01d43a61
commit
22b26ccad7
@ -31,10 +31,10 @@
|
|||||||
<copy todir="${build.home}/cls">
|
<copy todir="${build.home}/cls">
|
||||||
<fileset dir="${resource.home}" />
|
<fileset dir="${resource.home}" />
|
||||||
</copy>
|
</copy>
|
||||||
<jar destfile="build/hsadmin.jar" basedir="build/cls">
|
<jar destfile="build/hsscript.jar" basedir="build/cls">
|
||||||
<manifest>
|
<manifest>
|
||||||
<attribute name="Main-Class" value="de.hsadmin.jscli.Main"/>
|
<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>
|
</manifest>
|
||||||
</jar>
|
</jar>
|
||||||
</target>
|
</target>
|
||||||
|
@ -3,12 +3,15 @@ package de.hsadmin.jscli.cas;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.ProtocolException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
@ -36,7 +39,7 @@ public class CASTicketProvider {
|
|||||||
this.passwordReader = console;
|
this.passwordReader = console;
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.runAs = runAs;
|
this.runAs = runAs;
|
||||||
Config config = Config.getInstance();
|
final Config config = Config.getInstance();
|
||||||
backendURL = config.getProperty("backendURL", BACKEND_URL);
|
backendURL = config.getProperty("backendURL", BACKEND_URL);
|
||||||
loginURL = config.getProperty("loginURL", LOGIN_URL);
|
loginURL = config.getProperty("loginURL", LOGIN_URL);
|
||||||
if ("TestUmgebung".equals(loginURL)) {
|
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:")) {
|
if (grantingTicket != null && grantingTicket.startsWith("ticket:")) {
|
||||||
return grantingTicket.replaceFirst("ticket", "user");
|
return grantingTicket.replaceFirst("ticket", "user");
|
||||||
}
|
}
|
||||||
@ -77,6 +80,8 @@ public class CASTicketProvider {
|
|||||||
grantingTicket = doHttpPost(loginURL, encodedParams);
|
grantingTicket = doHttpPost(loginURL, encodedParams);
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw new JSCliException(e);
|
throw new JSCliException(e);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new JSCliException("cas server not available: " + loginURL);
|
||||||
}
|
}
|
||||||
return grantingTicket;
|
return grantingTicket;
|
||||||
}
|
}
|
||||||
@ -85,29 +90,17 @@ public class CASTicketProvider {
|
|||||||
return passwordReader.readPassword();
|
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;
|
String result = null;
|
||||||
try {
|
try {
|
||||||
URL url = new URL(urlString);
|
result = extractTicket(urlString, encodedParams);
|
||||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
} catch (FileNotFoundException e) {
|
||||||
connection.setRequestMethod("POST");
|
grantingTicket = getGrantingTicket();
|
||||||
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
|
saveProperties(grantingTicket, getTicketFile());
|
||||||
connection.setDoInput(true);
|
try {
|
||||||
connection.setDoOutput(true);
|
result = extractTicket(grantingTicket, encodedParams);
|
||||||
connection.setUseCaches(false);
|
} catch (IOException e1) {
|
||||||
connection.setAllowUserInteraction(false);
|
throw new JSCliException(e1);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new JSCliException(e);
|
throw new JSCliException(e);
|
||||||
@ -115,31 +108,88 @@ public class CASTicketProvider {
|
|||||||
return result;
|
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 {
|
private String readFiledGrantingTicket() throws JSCliException {
|
||||||
String filedTicket = null;
|
String filedTicket = null;
|
||||||
String userHome = System.getProperty("user.home");
|
final File file = getTicketFile();
|
||||||
String ticketFileName = userHome + "/.hsadmin.tgt";
|
final Properties properties = loadProperties(file);
|
||||||
File file = new File(ticketFileName);
|
|
||||||
Properties properties = new Properties();
|
|
||||||
if (file.isFile() && file.canRead()) {
|
|
||||||
try {
|
|
||||||
properties.load(new FileReader(file));
|
|
||||||
filedTicket = properties.getProperty(user);
|
filedTicket = properties.getProperty(user);
|
||||||
} catch (IOException e) {
|
|
||||||
throw new JSCliException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (filedTicket == null) {
|
if (filedTicket == null) {
|
||||||
filedTicket = getGrantingTicket();
|
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) {
|
if (filedTicket != null) {
|
||||||
properties.setProperty(user, filedTicket);
|
properties.setProperty(user, filedTicket);
|
||||||
try {
|
try {
|
||||||
properties.store(new FileOutputStream(file), "");
|
properties.store(new FileOutputStream(file), "");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
throw new JSCliException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return filedTicket;
|
|
||||||
|
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 properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user