Skip to content

Instantly share code, notes, and snippets.

@itsbalamurali
Forked from kczechbec/EnrollCert.java
Created November 8, 2022 11:04
Show Gist options
  • Save itsbalamurali/af1a70f3b472ecf8f433041ea991584f to your computer and use it in GitHub Desktop.
Save itsbalamurali/af1a70f3b472ecf8f433041ea991584f to your computer and use it in GitHub Desktop.
Enrollment example using TPP client library
package com.example;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EnrollCert {
private String cert;
}
package com.example;
import dk.bec.tpsi.openbanking.tppclient.model.TppClientCertParams;
import dk.bec.tpsi.openbanking.tppclient.model.TppClientRequest;
import dk.bec.tpsi.openbanking.tppclient.model.TppClientResponse;
import dk.bec.tpsi.openbanking.tppclient.service.TppClientException;
import dk.bec.tpsi.openbanking.tppclient.service.TppClientService;
import dk.bec.tpsi.openbanking.tppclient.service.TppClientServiceImpl;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.HttpStatus;
import java.io.IOException;
import java.util.*;
public class EnrollmentService {
public final String REQUEST_URL = "https://some.gateway.url/eidas/1.0/v1/";
public void enrollment() throws IOException, TppClientException {
String reqId = UUID.randomUUID().toString();
TppClientService tppClientService = new TppClientServiceImpl(REQUEST_URL, getTppClientCertParams());
String jsonBody = getJsonBody(getEnrollRequestBody());
TppClientRequest request = getTppClientRequest(getHeaders(reqId), jsonBody);
TppClientResponse response = tppClientService.callGateway(request);
if (HttpStatus.CREATED.value() != response.getStatus()) {
// handle HTTP error
} else {
//handle response.getBody();
}
}
private TppClientCertParams getTppClientCertParams() {
return TppClientCertParams.builder().keystorePath("keystore_path")
.sealCertName("seal_certificate.p12")
//the alias is optional, if not specified the first key is used
.sealKeyAlias("seal_certificate_alias")
.sealCertPass("seal_cert_pass")
.wacCertName("wac_certificate.p12")
.wacCertPass("wac_certificate_pass")
//the alias is optional, if not specified the first certificate is used
.wacKeyAlias("wac_certificate_alias")
.build();
}
private EnrollRequestBody getEnrollRequestBody() {
return EnrollRequestBody.builder()
.tppid("DK-FSA001-63882")
.commercialname("Amazing FinTech")
.roles(new String[] {"PSP_AI"})
.qsealcert(getEnrollQSealCert())
.qwaccert(getEnrollQWacCert())
.build();
}
private EnrollQSealCert getEnrollQSealCert() {
return EnrollQSealCert.builder()
.certificate("Base64 encoded QSEAL certificate string")
.cacert("Base64 encoded CA certificate string")
.chaincerts(new EnrollCert[] { new EnrollCert("Base64 encoded CHAIN certificate string") })
.build();
}
private EnrollQWacCert getEnrollQWacCert() {
return EnrollQWacCert.builder()
.certificate("Base64 encoded QWAC certificate string without newlines and -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----. This applies for all certificates.")
.cacert("Base64 encoded CA certificate string")
.chaincerts(new EnrollCert[] { new EnrollCert("Base64 encoded CHAIN certificate string") })
.build();
}
private TppClientRequest getTppClientRequest(Map<String, List<String>> headers, String jsonBody) {
return TppClientRequest
.builder()
.httpMethod("POST")
.path("enrollment")
.headers(headers)
.requestBody(jsonBody)
.build();
}
private Map<String, List<String>> getHeaders(String reqId) {
Map<String, List<String>> headers = new HashMap<>();
headers.put("x-request-id", Arrays.asList(reqId));
return headers;
}
private String getJsonBody(EnrollRequestBody body) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(body);
}
}
package com.example;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EnrollQSealCert {
private String certificate;
private String cacert;
private EnrollCert[] chaincerts;
}
package com.example;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EnrollQWacCert {
private String certificate;
private String cacert;
private EnrollCert[] chaincerts;
}
package com.example;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EnrollRequestBody {
private String tppid;
private String commercialname;
private String[] roles;
private EnrollQWacCert qwaccert;
private EnrollQSealCert qsealcert;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment