Skip to content

Commit

Permalink
EduID integration
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Sep 15, 2024
1 parent 9b7923f commit 683d4a7
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
49 changes: 49 additions & 0 deletions server/src/main/java/access/eduid/EduID.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package access.eduid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.Optional;

@Service
public class EduID {

private static final Log LOG = LogFactory.getLog(EduID.class);

private final String uri;
private final RestTemplate restTemplate;
private final HttpHeaders headers;

public EduID(@Value("${myconext.uri}") String uri,
@Value("${myconext.username}") String userName,
@Value("${myconext.password}") String password) {
this.uri = uri;
this.restTemplate = new RestTemplate();
this.restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(userName, password));
this.headers = initHttpHeaders();
}

public Optional<String> provisionEduid(EduIDProvision eduIDProvision) {
HttpEntity<EduIDProvision> requestEntity = new HttpEntity<>(eduIDProvision, headers);
try {
ResponseEntity<EduIDProvision> responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, EduIDProvision.class);
return Optional.of(responseEntity.getBody().getEduIDValue());
} catch (RuntimeException e) {
LOG.error("Error in provisionEduid", e);
return Optional.empty();
}
}

private HttpHeaders initHttpHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
return headers;
}
}
16 changes: 16 additions & 0 deletions server/src/main/java/access/eduid/EduIDProvision.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package access.eduid;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;


@Getter
@AllArgsConstructor
public class EduIDProvision {

private String eduIDValue;
private String institutionGUID;

}
5 changes: 5 additions & 0 deletions server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ cron:
metadata-resolver-fixed-rate-milliseconds: 86_400_000
metadata-resolver-url: "classpath:/metadata/idps-metadata.xml"

myconext:
uri: "https://login.test2.eduid.nl/myconext/api/invite/provision-eduid"
username: invite
password: secret

oidcng:
discovery-url: "https://connect.test2.surfconext.nl/oidc/.well-known/openid-configuration"
introspect-url: "https://connect.test2.surfconext.nl/oidc/introspect"
Expand Down
47 changes: 47 additions & 0 deletions server/src/test/java/access/eduid/EduIDTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package access.eduid;

import access.WireMockExtension;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.Optional;
import java.util.UUID;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class EduIDTest {

private final ObjectMapper objectMapper = new ObjectMapper();
private final EduID eduID = new EduID("http://localhost:8081/myconext/api/invite/provision-eduid", "invite", "secret");

@RegisterExtension
WireMockExtension mockServer = new WireMockExtension(8081);

@SneakyThrows
@Test
void provisionEduid() {
String eduIDValue = UUID.randomUUID().toString();
String institutionGUID = UUID.randomUUID().toString();
EduIDProvision eduIDProvision = new EduIDProvision(eduIDValue, institutionGUID);
stubFor(post(urlPathMatching("/myconext/api/invite/provision-eduid")).willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(eduIDProvision))));
Optional<String> eduid = eduID.provisionEduid(eduIDProvision);
assertEquals(eduIDProvision.getEduIDValue(), eduid.get());
}

@SneakyThrows
@Test
void provisionEduid404() {
EduIDProvision eduIDProvision = new EduIDProvision("eduIDValue", "institutionGUID");
stubFor(post(urlPathMatching("/myconext/api/invite/provision-eduid")).willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(404)));
Optional<String> eduid = eduID.provisionEduid(eduIDProvision);
assertTrue(eduid.isEmpty());
}
}

0 comments on commit 683d4a7

Please sign in to comment.