From 519ad5a9f8fb425d8410a1877430135c7bc33b10 Mon Sep 17 00:00:00 2001 From: Carol Alexandru Date: Wed, 13 Oct 2021 11:29:02 +0200 Subject: [PATCH 1/2] authenticate with OLAT using POST instead of GET --- .../lms/impl/olat/OlatLmsRestTemplate.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java index 6c326124..4e759384 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java @@ -12,6 +12,7 @@ import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRequest; import org.springframework.http.HttpStatus; @@ -66,12 +67,16 @@ public class OlatLmsRestTemplate extends RestTemplate { private void authenticate() { // Authenticate with OLAT and store the received X-OLAT-TOKEN this.token = "authenticating"; - final String authUrl = String.format("%s%s?password=%s", - this.details.getAccessTokenUri(), + final String authUrl = this.details.getAccessTokenUri(); + final String credentials = String.format( + "{\"username\": \"%s\", \"password\": \"%s\"}", this.details.getClientId(), this.details.getClientSecret()); + final HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.set("content-type", "application/json"); + final HttpEntity requestEntity = new HttpEntity<>(credentials, httpHeaders); try { - final ResponseEntity response = this.getForEntity(authUrl, String.class); + final ResponseEntity response = this.postForEntity(authUrl, requestEntity, String.class); final HttpHeaders responseHeaders = response.getHeaders(); log.debug("OLAT [authenticate] {} Headers: {}", response.getStatusCode(), responseHeaders); this.token = responseHeaders.getFirst("X-OLAT-TOKEN"); From 1449de217cbadf485eb6b97aad8527d2095262ac Mon Sep 17 00:00:00 2001 From: Carol Alexandru Date: Wed, 13 Oct 2021 12:01:12 +0200 Subject: [PATCH 2/2] don't construct json manually --- .../lms/impl/olat/OlatLmsRestTemplate.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java index 4e759384..bc026a1f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java @@ -9,6 +9,8 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.olat; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,13 +70,12 @@ public class OlatLmsRestTemplate extends RestTemplate { // Authenticate with OLAT and store the received X-OLAT-TOKEN this.token = "authenticating"; final String authUrl = this.details.getAccessTokenUri(); - final String credentials = String.format( - "{\"username\": \"%s\", \"password\": \"%s\"}", - this.details.getClientId(), - this.details.getClientSecret()); + final Map credentials = new HashMap<>(); + credentials.put("username", this.details.getClientId()); + credentials.put("password", this.details.getClientSecret()); final HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("content-type", "application/json"); - final HttpEntity requestEntity = new HttpEntity<>(credentials, httpHeaders); + final HttpEntity> requestEntity = new HttpEntity<>(credentials, httpHeaders); try { final ResponseEntity response = this.postForEntity(authUrl, requestEntity, String.class); final HttpHeaders responseHeaders = response.getHeaders();