feat: GA4GH ClaimSource by API call

pull/1580/head
BaranekD 2022-07-28 16:45:40 +02:00
parent c1be3a18c3
commit 6cac023c7a
5 changed files with 85 additions and 27 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@ target
*~ *~
bin bin
*.idea *.idea
*node_modules
*.iml *.iml
*.eml *.eml
.project .project

View File

@ -51,15 +51,7 @@
</c:when> </c:when>
<c:when test="${claim.value.getClass().name eq 'java.util.ArrayList'}"> <c:when test="${claim.value.getClass().name eq 'java.util.ArrayList'}">
<c:forEach var="subValue" items="${claim.value}"> <c:forEach var="subValue" items="${claim.value}">
<c:choose> <li>${subValue}</li>
<c:when test="${claim.key=='ga4gh_passport_v1'}">
<li><%= Ga4ghPassportAndVisaClaimSource.parseAndVerifyVisa(
(String) jspContext.findAttribute("subValue")).getPrettyString() %></li>
</c:when>
<c:otherwise>
<li>${subValue}</li>
</c:otherwise>
</c:choose>
</c:forEach> </c:forEach>
</c:when> </c:when>
<c:otherwise> <c:otherwise>

View File

@ -70,15 +70,7 @@
<c:if test="${claim.value.getClass().name eq 'java.util.ArrayList'}"> <c:if test="${claim.value.getClass().name eq 'java.util.ArrayList'}">
<c:forEach var="subValue" items="${claim.value}"> <c:forEach var="subValue" items="${claim.value}">
<div> <div>
<c:choose> <code>${subValue}</code>
<c:when test="${claim.key=='ga4gh_passport_v1'}">
<code><%= Ga4ghPassportAndVisaClaimSource.parseAndVerifyVisa(
(String) pageContext.findAttribute("subValue")).getPrettyString() %></code>
</c:when>
<c:otherwise>
<code>${subValue}</code>
</c:otherwise>
</c:choose>
</div> </div>
</c:forEach> </c:forEach>
</c:if> </c:if>

View File

@ -71,15 +71,7 @@
<c:if test="${claim.value.getClass().name eq 'java.util.ArrayList'}"> <c:if test="${claim.value.getClass().name eq 'java.util.ArrayList'}">
<c:forEach var="subValue" items="${claim.value}"> <c:forEach var="subValue" items="${claim.value}">
<div> <div>
<c:choose> <code>${subValue}</code>
<c:when test="${claim.key=='ga4gh_passport_v1'}">
<code><%= Ga4ghPassportAndVisaClaimSource.parseAndVerifyVisa(
(String) pageContext.findAttribute("subValue")).getPrettyString() %></code>
</c:when>
<c:otherwise>
<code>${subValue}</code>
</c:otherwise>
</c:choose>
</div> </div>
</c:forEach> </c:forEach>
</c:if> </c:if>

View File

@ -0,0 +1,81 @@
package cz.muni.ics.oidc.server.claims.sources;
import com.fasterxml.jackson.databind.JsonNode;
import cz.muni.ics.oidc.server.claims.ClaimSource;
import cz.muni.ics.oidc.server.claims.ClaimSourceInitContext;
import cz.muni.ics.oidc.server.claims.ClaimSourceProduceContext;
import cz.muni.ics.oidc.server.claims.ClaimUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
import java.util.Set;
@Slf4j
public class Ga4ghApiClaimSource extends ClaimSource {
private static final String ENDPOINT = "endpoint";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String PARAM_NAME = "param_name";
private static final String EPPN = "{eppn}";
private final String endpoint;
private final String username;
private final String password;
private final String paramName;
public Ga4ghApiClaimSource(ClaimSourceInitContext ctx) {
super(ctx);
this.endpoint = ClaimUtils.fillStringMandatoryProperty(ENDPOINT, ctx, getClaimName());
this.username = ClaimUtils.fillStringMandatoryProperty(USERNAME, ctx, getClaimName());
this.password = ClaimUtils.fillStringMandatoryProperty(PASSWORD, ctx, getClaimName());
this.paramName = ClaimUtils.fillStringPropertyOrDefaultVal(PARAM_NAME, ctx, EPPN);
log.debug("{} - initialized", getClaimName());
}
@Override
public Set<String> getAttrIdentifiers() {
return Collections.emptySet();
}
@Override
public JsonNode produceValue(ClaimSourceProduceContext pctx) {
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
JsonNode result = restTemplate.getForObject(endpoint, JsonNode.class, Collections.singletonMap(paramName, pctx.getSub()));
log.debug("{} - user: {}, GA4GH passports: {}", getClaimName(), pctx.getSub(), result);
return result;
}
private HttpComponentsClientHttpRequestFactory getClientHttpRequestFactory()
{
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setHttpClient(httpClient());
return clientHttpRequestFactory;
}
private HttpClient httpClient()
{
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
return HttpClientBuilder
.create()
.setDefaultCredentialsProvider(credentialsProvider)
.build();
}
}