forked from j62/ctbrec
Fix retrieving the CSRF token for CamSoda
This commit is contained in:
parent
d330fabe36
commit
977cee5af0
|
@ -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.
|
||||||
|
|
|
@ -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() {
|
||||||
|
@ -27,12 +30,12 @@ public class CamsodaHttpClient extends HttpClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean login() throws IOException {
|
public boolean login() throws IOException {
|
||||||
if(loggedIn) {
|
if (loggedIn) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// persisted cookies might let us log in
|
// persisted cookies might let us log in
|
||||||
if(checkLoginSuccess()) {
|
if (checkLoginSuccess()) {
|
||||||
loggedIn = true;
|
loggedIn = true;
|
||||||
LOG.debug("Logged in with cookies");
|
LOG.debug("Logged in with cookies");
|
||||||
return true;
|
return true;
|
||||||
|
@ -68,14 +71,15 @@ 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 {
|
||||||
String url = Camsoda.BASE_URI + "/api/v1/user/current";
|
String url = Camsoda.BASE_URI + "/api/v1/user/current";
|
||||||
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()) {
|
||||||
JSONObject resp = new JSONObject(response.body().string());
|
JSONObject resp = new JSONObject(response.body().string());
|
||||||
return resp.optBoolean("status");
|
return resp.optBoolean("status");
|
||||||
} else {
|
} else {
|
||||||
|
@ -85,13 +89,18 @@ public class CamsodaHttpClient extends HttpClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getCsrfToken() throws IOException {
|
protected String getCsrfToken() throws IOException {
|
||||||
if(csrfToken == null) {
|
if (csrfToken == null) {
|
||||||
String url = Camsoda.BASE_URI;
|
String url = Camsoda.BASE_URI;
|
||||||
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());
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue