Use OpenOlat endpoint to authenticate

This commit is contained in:
Christian Schweizer 2024-04-30 10:14:38 +02:00
parent d1cb6fc2c9
commit d4d12b4446
2 changed files with 7 additions and 6 deletions

View file

@ -473,7 +473,7 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm
.getOrThrow(); .getOrThrow();
final ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails(); final ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
details.setAccessTokenUri(lmsSetup.lmsApiUrl + "/restapi/auth/"); details.setAccessTokenUri(lmsSetup.lmsApiUrl + "/restapi/auth/{username}?password={password}");
details.setClientId(plainClientId.toString()); details.setClientId(plainClientId.toString());
details.setClientSecret(plainClientSecret.toString()); details.setClientSecret(plainClientSecret.toString());

View file

@ -16,6 +16,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest; import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -116,14 +117,14 @@ public class OlatLmsRestTemplate extends RestTemplate {
// Authenticate with OLAT and store the received X-OLAT-TOKEN // Authenticate with OLAT and store the received X-OLAT-TOKEN
this.token = "authenticating"; this.token = "authenticating";
final String authUrl = this.details.getAccessTokenUri(); final String authUrl = this.details.getAccessTokenUri();
final Map<String, String> credentials = new HashMap<>(); final Map<String, String> parameters = new HashMap<>();
credentials.put("username", this.details.getClientId()); parameters.put("username", this.details.getClientId());
credentials.put("password", this.details.getClientSecret()); parameters.put("password", this.details.getClientSecret());
final HttpHeaders httpHeaders = new HttpHeaders(); final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("content-type", "application/json"); httpHeaders.set("content-type", "application/json");
final HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(credentials, httpHeaders); final HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(httpHeaders);
try { try {
final ResponseEntity<String> response = this.postForEntity(authUrl, requestEntity, String.class); final ResponseEntity<String> response = this.exchange(authUrl, HttpMethod.GET, requestEntity, String.class, parameters);
final HttpHeaders responseHeaders = response.getHeaders(); final HttpHeaders responseHeaders = response.getHeaders();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {