diff options
Diffstat (limited to 'src/org/slcl/core/Login.java')
| -rw-r--r-- | src/org/slcl/core/Login.java | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/src/org/slcl/core/Login.java b/src/org/slcl/core/Login.java index d5b9267..e52a01a 100644 --- a/src/org/slcl/core/Login.java +++ b/src/org/slcl/core/Login.java @@ -18,45 +18,26 @@ package org.slcl.core; +import org.slcl.core.Connection; import java.io.OutputStream; import java.io.IOException; -import java.net.URL; -import java.net.URLConnection; import java.net.HttpURLConnection; import java.util.List; import java.util.Map; -import javax.net.ssl.HttpsURLConnection; public final class Login { - public String login(final boolean https, String url, final String username, - final String password) throws IOException { - - if (!url.startsWith("https://") && https) { - url = "https://" + url; - } - else if (!url.startsWith("http://")) { - url = "http://" + url; - } - - url += "/login"; - - final String data = "username=" + username + "&password=" + password; - - URL u = new URL(url); - HttpURLConnection c; - - if (https) { - c = (HttpsURLConnection)u.openConnection(); - } - else { - c = (HttpURLConnection)u.openConnection(); - } + public String login(final boolean https, final String url, + final String username, final String password) throws IOException + { + final Connection conn = new Connection(https, url + "/login"); + final HttpURLConnection c = conn.getConnection(); c.setRequestMethod("POST"); c.setInstanceFollowRedirects(false); c.setReadTimeout(5000); - OutputStream os = c.getOutputStream(); + final OutputStream os = c.getOutputStream(); + final String data = "username=" + username + "&password=" + password; os.write(data.getBytes()); @@ -74,6 +55,21 @@ public final class Login { { final Map<String,List<String>> headers = c.getHeaderFields(); final List<String> cookies = headers.get("Set-Cookie"); + final List<String> locations = headers.get("Location"); + + if (locations == null) { + throw new IOException("Expected Location header"); + } else if (locations.size() != 1) { + throw new IOException("Expected only 1 Location header"); + } + + final String location = locations.toArray(new String[0])[0], + exp_location = "/user/"; + + if (!location.equals(exp_location)) { + throw new IOException("Expected redirection to " + + exp_location + ", got \"" + location + "\""); + } if (cookies != null) { if (cookies.size() != 1) { |
