Commit 7800b175 authored by Daniel Gerhardt's avatar Daniel Gerhardt

Implement HTTP basic authentication for StudipRestConnectorDaoImpl

parent 567336bc
......@@ -17,6 +17,7 @@ repositories {
dependencies {
compile project (':connector-model')
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'commons-codec', name: 'commons-codec', version: '1.7'
compile group: 'org.springframework', name: 'spring-context', version: springVersion
compile group: 'org.springframework', name: 'spring-webmvc', version: springVersion
compile group: 'org.springframework', name: 'spring-jdbc', version: springVersion
......
package de.thm.arsnova.connector.dao;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
......@@ -15,11 +20,17 @@ public class StudipRestConnectorDaoImpl implements ConnectorDao {
private static final String TYPE = "studip";
private static final String USER_MEMBERSHIP_URI = "/user/{userid}/courses";
private static final String USER_SERACH_URI = "/users?needle={username}";
private static final String USER_MEMBERSHIP_URI = "{host}/user/{userid}/courses";
private static final String USER_SERACH_URI = "{host}/users?needle={username}";
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private final RestTemplate restTemplate = new RestTemplate();
@Value("lms.http.serverUrl") private String uriHostPart;
@Value("lms.http.username") private String httpUsername;
@Value("lms.http.password") private String httpPassword;
@Override
public List<String> getCourseUsers(final String courseid) {
throw new UnsupportedOperationException();
......@@ -30,8 +41,10 @@ public class StudipRestConnectorDaoImpl implements ConnectorDao {
Membership membership = new Membership();
ResponseEntity<StudipCoursesContainer> courseContainer = restTemplate.exchange(
USER_MEMBERSHIP_URI,
HttpMethod.GET, null,
HttpMethod.GET,
new HttpEntity<Void>(getAuthorizationHeader()),
StudipCoursesContainer.class,
uriHostPart,
getUserByName(username).getUserId()
);
......@@ -61,8 +74,10 @@ public class StudipRestConnectorDaoImpl implements ConnectorDao {
List<Course> courses = new ArrayList<>();
ResponseEntity<StudipCoursesContainer> courseContainer = restTemplate.exchange(
USER_MEMBERSHIP_URI,
HttpMethod.GET, null,
HttpMethod.GET,
new HttpEntity<Void>(getAuthorizationHeader()),
StudipCoursesContainer.class,
uriHostPart,
getUserByName(username).getUserId()
);
......@@ -79,14 +94,27 @@ public class StudipRestConnectorDaoImpl implements ConnectorDao {
private StudipUser getUserByName(String username) {
ResponseEntity<StudipUserContainer> userContainer = restTemplate.exchange(
USER_SERACH_URI,
HttpMethod.GET, null,
HttpMethod.GET,
new HttpEntity<Void>(getAuthorizationHeader()),
StudipUserContainer.class,
uriHostPart,
username
);
return userContainer.getBody().getUser();
}
private HttpHeaders getAuthorizationHeader() {
HttpHeaders httpHeaders = new HttpHeaders();
String authorization = httpUsername + ":" + httpPassword;
httpHeaders.add(
"Authorization",
"Basic " + Base64.encodeBase64String(authorization.getBytes(UTF8_CHARSET))
);
return httpHeaders;
}
private Course buildCourse(StudipCourse sipCourse, UserRole role) {
Membership membership = new Membership();
membership.setMember(true);
......
......@@ -3,6 +3,10 @@ jdbc.url=jdbc:mysql://127.0.0.1:3306/moodle?zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=secret
lms.http.serverUrl=https://localhost/lms-api
lms.http.username=connector-user
lms.http.password=secret
# Select the data access object implementation your target platform
# Uncomment the following line to access Moodle
dao.implementation=de.thm.arsnova.connector.dao.MoodleConnectorDaoImpl
......
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