forked from j62/ctbrec
1
0
Fork 0

Fix retrieving the CSRF token for CamSoda

This commit is contained in:
0xb00bface 2021-04-09 18:18:02 +02:00
parent d330fabe36
commit 977cee5af0
3 changed files with 25 additions and 15 deletions

View File

@ -1,6 +1,7 @@
4.1.2 4.1.2
======================== ========================
* Fixed bug, which caused recordings to get stuck * Fixed bug, which caused some recordings to get stuck
* Fixed follow/unfollow for CamSoda
* Ignore list is now saved as URLs only. The old format is not compatible * Ignore list is now saved as URLs only. The old format is not compatible
anymore, so make sure, that you export them again, if you created a backup anymore, so make sure, that you export them again, if you created a backup
before. before.

View File

@ -1,15 +1,17 @@
package ctbrec.sites.camsoda; package ctbrec.sites.camsoda;
import static java.util.regex.Pattern.*;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject; import org.json.JSONObject;
import org.jsoup.nodes.Element;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpClient; import ctbrec.io.HttpClient;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import okhttp3.FormBody; import okhttp3.FormBody;
@ -18,7 +20,8 @@ import okhttp3.Response;
public class CamsodaHttpClient extends HttpClient { public class CamsodaHttpClient extends HttpClient {
private static final transient Logger LOG = LoggerFactory.getLogger(CamsodaHttpClient.class); private static final Logger LOG = LoggerFactory.getLogger(CamsodaHttpClient.class);
private static final Pattern CSRF_PATTERN = Pattern.compile("\"csrf\"\s*:\s*\"(.*?)\"", MULTILINE | DOTALL);
private String csrfToken = null; private String csrfToken = null;
public CamsodaHttpClient() { public CamsodaHttpClient() {
@ -69,6 +72,7 @@ public class CamsodaHttpClient extends HttpClient {
/** /**
* check, if the login worked * check, if the login worked
*
* @throws IOException * @throws IOException
*/ */
public boolean checkLoginSuccess() throws IOException { public boolean checkLoginSuccess() throws IOException {
@ -90,8 +94,13 @@ public class CamsodaHttpClient extends HttpClient {
Request request = new Request.Builder().url(url).build(); Request request = new Request.Builder().url(url).build();
try (Response response = execute(request)) { try (Response response = execute(request)) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
Element meta = HtmlParser.getTag(response.body().string(), "meta[name=\"_token\"]"); String pageContent = response.body().string();
csrfToken = meta.attr("content"); Matcher m = CSRF_PATTERN.matcher(pageContent);
if (m.find()) {
csrfToken = m.group(1);
} else {
throw new IOException("Couldn't find csrf token");
}
} else { } else {
throw new HttpException(response.code(), response.message()); throw new HttpException(response.code(), response.message());
} }

View File

@ -303,7 +303,7 @@ public class CamsodaModel extends AbstractModel {
@Override @Override
public boolean unfollow() throws IOException { public boolean unfollow() throws IOException {
String url = Camsoda.BASE_URI + "/api/v1/unfollow/" + getName(); String url = Camsoda.BASE_URI + "/api/v1/unfollow/" + getName();
LOG.debug("Sending follow request {}", url); LOG.debug("Sending unfollow request {}", url);
String csrfToken = ((CamsodaHttpClient)site.getHttpClient()).getCsrfToken(); String csrfToken = ((CamsodaHttpClient)site.getHttpClient()).getCsrfToken();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)