Commit 84d23655 authored by Marcel's avatar Marcel
Browse files

Adapt to API changes introduced with CT 3.56

parent 3b341c71
Pipeline #126363668 passed with stage
in 3 minutes and 35 seconds
......@@ -5,7 +5,7 @@
<groupId>de.apis-stuttgart</groupId>
<artifactId>churchtools-to-songbeamer</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
<packaging>jar</packaging>
<name>churchtools-to-songbeamer</name>
......@@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<version>2.2.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
......
......@@ -18,16 +18,26 @@ public class ChurchtoolsConfig {
protected String userId;
protected String userToken;
public String getUrlBase() {
private String getInstanceUrl() {
if (instance == null || instance.isEmpty()) {
log.error("Please set the churchtools instance");
return "";
return null;
}
if (instance.contains(".")) {
return "https://" + instance + "/index.php";
return "https://" + instance + "/";
}
return "https://" + instance + ".church.tools/index.php";
return "https://" + instance + ".church.tools/";
}
public String getUrlBase() {
String instanceUrl = getInstanceUrl();
return instanceUrl != null ? instanceUrl + "index.php" : "";
}
public String getApiBase() {
String instanceUrl = getInstanceUrl();
return instanceUrl != null ? instanceUrl + "api/" : "";
}
}
package de.apisstuttgart.churchtoolstosongbeamer.domain;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class CtCsrfToken {
@JsonProperty("data")
protected String token;
}
package de.apisstuttgart.churchtoolstosongbeamer.service;
import de.apisstuttgart.churchtoolstosongbeamer.config.ChurchtoolsConfig;
import de.apisstuttgart.churchtoolstosongbeamer.domain.CtAgendaItem;
import de.apisstuttgart.churchtoolstosongbeamer.domain.CtCsrfToken;
import de.apisstuttgart.churchtoolstosongbeamer.domain.CtResponse;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
@Service
......@@ -55,6 +57,24 @@ public class CtLoginService {
}
/**
* Request CSRF token for further requests
*/
protected void requestCsrfToken() {
HttpEntity requestEntity = new HttpEntity(requestHeader);
ResponseEntity<CtCsrfToken> response = restTemplate.exchange(churchtoolsConfig.getApiBase() + "csrftoken", HttpMethod.GET, requestEntity, CtCsrfToken.class);
if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null && response.getBody().getToken() != null) {
log.info("Got CSRF token from CT");
requestHeader.set("CSRF-Token", response.getBody().getToken());
} else {
log.error("Failed to load CSRF token from CT.");
}
}
/**
* Get request header which handles the authentication
*
......@@ -63,6 +83,7 @@ public class CtLoginService {
public HttpHeaders getRequestHeader() {
if (requestHeader.isEmpty()) {
this.performLogin();
this.requestCsrfToken();
}
return requestHeader;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment