aboutsummaryrefslogtreecommitdiff
path: root/src/org/slcl/core/Login.java
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-05-18 00:16:22 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-05-28 08:30:24 +0200
commitee53ad2ccc88b91a1857e75714ae56068bd40952 (patch)
tree9f3be3aa809d2cfb54ab6d1269f57f6538a4de97 /src/org/slcl/core/Login.java
parent40f6d425a4429b16936cc8bb4900a23c3362a123 (diff)
Diffstat (limited to 'src/org/slcl/core/Login.java')
-rw-r--r--src/org/slcl/core/Login.java50
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) {