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
========================
* 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
anymore, so make sure, that you export them again, if you created a backup
before.

View File

@ -1,15 +1,17 @@
package ctbrec.sites.camsoda;
import static java.util.regex.Pattern.*;
import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import okhttp3.FormBody;
@ -18,7 +20,8 @@ import okhttp3.Response;
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;
public CamsodaHttpClient() {
@ -27,12 +30,12 @@ public class CamsodaHttpClient extends HttpClient {
@Override
public boolean login() throws IOException {
if(loggedIn) {
if (loggedIn) {
return true;
}
// persisted cookies might let us log in
if(checkLoginSuccess()) {
if (checkLoginSuccess()) {
loggedIn = true;
LOG.debug("Logged in with cookies");
return true;
@ -68,14 +71,15 @@ public class CamsodaHttpClient extends HttpClient {
}
/**
* check, if the login worked
* check, if the login worked
*
* @throws IOException
*/
public boolean checkLoginSuccess() throws IOException {
String url = Camsoda.BASE_URI + "/api/v1/user/current";
Request request = new Request.Builder().url(url).build();
try(Response response = execute(request)) {
if(response.isSuccessful()) {
try (Response response = execute(request)) {
if (response.isSuccessful()) {
JSONObject resp = new JSONObject(response.body().string());
return resp.optBoolean("status");
} else {
@ -85,13 +89,18 @@ public class CamsodaHttpClient extends HttpClient {
}
protected String getCsrfToken() throws IOException {
if(csrfToken == null) {
if (csrfToken == null) {
String url = Camsoda.BASE_URI;
Request request = new Request.Builder().url(url).build();
try(Response response = execute(request)) {
if(response.isSuccessful()) {
Element meta = HtmlParser.getTag(response.body().string(), "meta[name=\"_token\"]");
csrfToken = meta.attr("content");
try (Response response = execute(request)) {
if (response.isSuccessful()) {
String pageContent = response.body().string();
Matcher m = CSRF_PATTERN.matcher(pageContent);
if (m.find()) {
csrfToken = m.group(1);
} else {
throw new IOException("Couldn't find csrf token");
}
} else {
throw new HttpException(response.code(), response.message());
}

View File

@ -303,7 +303,7 @@ public class CamsodaModel extends AbstractModel {
@Override
public boolean unfollow() throws IOException {
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();
Request request = new Request.Builder()
.url(url)