Skip to content

Commit

Permalink
Merge pull request #69 from ConsumerDataStandardsAustralia/hotfix/66-…
Browse files Browse the repository at this point in the history
…data-holder-parse-OffsetDateTime-param

Fixed a couple of bugs in data-holder
  • Loading branch information
fyang1024 authored Nov 29, 2019
2 parents 70e3abd + 45775b8 commit ed1385f
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import au.org.consumerdatastandards.client.ApiClient;
import au.org.consumerdatastandards.client.ApiException;
import ch.qos.logback.classic.Logger;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.CreationException;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.tls.HandshakeCertificates;
import okhttp3.tls.HeldCertificate;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.util.Base64;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
import org.slf4j.LoggerFactory;
Expand All @@ -31,6 +34,7 @@
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
Expand All @@ -43,7 +47,9 @@
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApiUtil {

Expand Down Expand Up @@ -73,6 +79,7 @@ public static ApiClient createApiClient(ApiClientOptions clientOptions) throws A
String accessToken = clientOptions.getAccessToken();
if (StringUtils.isNotBlank(accessToken)) {
apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
apiClient.addDefaultHeader("x-cds-subject", getSub(accessToken));
}
if (clientOptions.isMtlsEnabled()) {
validateClientCertSettings(clientOptions);
Expand Down Expand Up @@ -112,6 +119,21 @@ private static X509Certificate loadCertificate(String certFilePath) throws Certi
return (X509Certificate) certificateFactory.generateCertificate(new FileInputStream(certFilePath));
}

private static String getSub(String accessToken) throws ApiException {
String body = accessToken.split("\\.")[1];
String json = new String(Base64.decodeBase64(body), StandardCharsets.UTF_8);
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
Map map;
try {
map = objectMapper.readValue(json, typeRef);
} catch (IOException e) {
throw new ApiException(e);
}
return map.get("sub").toString();
}


private static PrivateKey loadPrivateKey(String keyFilePath)
throws IOException, ApiException, NoSuchAlgorithmException, InvalidKeySpecException {
FileReader reader = new FileReader(keyFilePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,7 @@ private void init() {
addDefaultHeader("Content-Type", "application/json");
addDefaultHeader("x-v", "1");
addDefaultHeader("x-min-v", "1");
addDefaultHeader("x-fapi-customer-ip-address", getIpAddress());
addDefaultHeader("x-fapi-interaction-id", UUID.randomUUID().toString());
addDefaultHeader("x-fapi-auth-date", OffsetDateTime.now().toString());
}

private String getIpAddress() {
String ipAddress = "unknown";
try {
final DatagramSocket socket = new DatagramSocket();
socket.connect(InetAddress.getByName("203.98.87.18"), 53); // ns1.internet.net.au.
ipAddress = socket.getLocalAddress().getHostAddress();
socket.disconnect();
} catch (SocketException | UnknownHostException e) {
// ignored
}
return ipAddress;
}

/**
Expand Down Expand Up @@ -257,6 +242,10 @@ public ApiClient setUserAgent(String userAgent) {
return this;
}

public String getUserAgent() {
return defaultHeaderMap.get("User-Agent");
}

/**
* Add a default header.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,10 @@
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;

public class BankingAccountsAPI {
public class BankingAccountsAPI extends ProtectedAPI {

private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(BankingAccountsAPI.class);

private ApiClient apiClient;

public BankingAccountsAPI() {
this(new ApiClient());
}

public BankingAccountsAPI(ApiClient apiClient) {
this.apiClient = apiClient;
}

public ApiClient getApiClient() {
return apiClient;
}

public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}

/**
* Build call for getAccountDetail
* @param accountId A tokenised identifier for the account which is unique but not shareable (required)
Expand Down Expand Up @@ -68,6 +50,7 @@ public okhttp3.Call getAccountDetailCall(String accountId, final ApiCallback _ca
List<Pair> queryParams = new ArrayList<>();
List<Pair> collectionQueryParams = new ArrayList<>();
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down Expand Up @@ -176,6 +159,7 @@ public okhttp3.Call getTransactionDetailCall(String accountId, String transactio
List<Pair> queryParams = new ArrayList<>();
List<Pair> collectionQueryParams = new ArrayList<>();
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down Expand Up @@ -312,6 +296,7 @@ public okhttp3.Call getTransactionsCall(String accountId, String maxAmount, Stri
addQueryParam(queryParams, "page-size", pageSize);
addQueryParam(queryParams, "text", text);
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down Expand Up @@ -464,6 +449,7 @@ public okhttp3.Call listAccountsCall(Boolean isOwned, ParamAccountOpenStatus ope
addQueryParam(queryParams, "page-size", pageSize);
addQueryParam(queryParams, "product-category", productCategory);
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down Expand Up @@ -584,6 +570,7 @@ public okhttp3.Call listBalanceCall(String accountId, final ApiCallback _callbac
List<Pair> queryParams = new ArrayList<>();
List<Pair> collectionQueryParams = new ArrayList<>();
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down Expand Up @@ -695,6 +682,7 @@ public okhttp3.Call listBalancesSpecificAccountsCall(RequestAccountIds accountId
addQueryParam(queryParams, "page", page);
addQueryParam(queryParams, "page-size", pageSize);
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "POST", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,10 @@
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;

public class BankingDirectDebitsAPI {
public class BankingDirectDebitsAPI extends ProtectedAPI {

private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(BankingDirectDebitsAPI.class);

private ApiClient apiClient;

public BankingDirectDebitsAPI() {
this(new ApiClient());
}

public BankingDirectDebitsAPI(ApiClient apiClient) {
this.apiClient = apiClient;
}

public ApiClient getApiClient() {
return apiClient;
}

public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}

/**
* Build call for listDirectDebits
* @param accountId ID of the account to get direct debit authorisations for. Must have previously been returned by one of the account list end points. (required)
Expand Down Expand Up @@ -73,6 +55,7 @@ public okhttp3.Call listDirectDebitsCall(String accountId, Integer page, Integer
addQueryParam(queryParams, "page", page);
addQueryParam(queryParams, "page-size", pageSize);
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down Expand Up @@ -194,6 +177,7 @@ public okhttp3.Call listDirectDebitsSpecificAccountsCall(RequestAccountIds accou
addQueryParam(queryParams, "page", page);
addQueryParam(queryParams, "page-size", pageSize);
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "POST", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;

public class BankingPayeesAPI {
public class BankingPayeesAPI extends ProtectedAPI {

public enum ParamType {
ALL,
Expand All @@ -29,24 +29,6 @@ public enum ParamType {

private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(BankingPayeesAPI.class);

private ApiClient apiClient;

public BankingPayeesAPI() {
this(new ApiClient());
}

public BankingPayeesAPI(ApiClient apiClient) {
this.apiClient = apiClient;
}

public ApiClient getApiClient() {
return apiClient;
}

public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}

/**
* Build call for getPayeeDetail
* @param payeeId The ID used to locate the details of a particular payee (required)
Expand Down Expand Up @@ -74,6 +56,7 @@ public okhttp3.Call getPayeeDetailCall(String payeeId, final ApiCallback _callba
List<Pair> queryParams = new ArrayList<>();
List<Pair> collectionQueryParams = new ArrayList<>();
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down Expand Up @@ -179,12 +162,13 @@ public okhttp3.Call listPayeesCall(Integer page, Integer pageSize, ParamType typ
pageSize,
type);

List<Pair> queryParams = new ArrayList<>();
List<Pair> collectionQueryParams = new ArrayList<>();
List<Pair> queryParams = new ArrayList<>();
addQueryParam(queryParams, "page", page);
addQueryParam(queryParams, "page-size", pageSize);
addQueryParam(queryParams, "type", type);
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,10 @@
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;

public class BankingScheduledPaymentsAPI {
public class BankingScheduledPaymentsAPI extends ProtectedAPI {

private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(BankingScheduledPaymentsAPI.class);

private ApiClient apiClient;

public BankingScheduledPaymentsAPI() {
this(new ApiClient());
}

public BankingScheduledPaymentsAPI(ApiClient apiClient) {
this.apiClient = apiClient;
}

public ApiClient getApiClient() {
return apiClient;
}

public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}

/**
* Build call for listScheduledPayments
* @param accountId ID of the account to get scheduled payments for. Must have previously been returned by one of the account list end points. The account specified is the source account for the payment (required)
Expand Down Expand Up @@ -73,6 +55,7 @@ public okhttp3.Call listScheduledPaymentsCall(String accountId, Integer page, In
addQueryParam(queryParams, "page", page);
addQueryParam(queryParams, "page-size", pageSize);
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down Expand Up @@ -189,11 +172,12 @@ public okhttp3.Call listScheduledPaymentsSpecificAccountsCall(RequestAccountIds
page,
pageSize);

List<Pair> queryParams = new ArrayList<>();
List<Pair> collectionQueryParams = new ArrayList<>();
List<Pair> queryParams = new ArrayList<>();
addQueryParam(queryParams, "page", page);
addQueryParam(queryParams, "page-size", pageSize);
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "POST", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;

public class CommonCustomerAPI {

private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(CommonCustomerAPI.class);

private ApiClient apiClient;

public CommonCustomerAPI() {
this(new ApiClient());
}

public CommonCustomerAPI(ApiClient apiClient) {
this.apiClient = apiClient;
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public ApiClient getApiClient() {
return apiClient;
}
public class CommonCustomerAPI extends ProtectedAPI {

public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}
private static final Logger LOGGER = LoggerFactory.getLogger(CommonCustomerAPI.class);

/**
* Build call for getCustomer
Expand All @@ -64,6 +47,7 @@ public okhttp3.Call getCustomerCall(final ApiCallback _callback) throws ApiExcep
List<Pair> queryParams = new ArrayList<>();
List<Pair> collectionQueryParams = new ArrayList<>();
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down Expand Up @@ -156,6 +140,7 @@ public okhttp3.Call getCustomerDetailCall(final ApiCallback _callback) throws Ap
List<Pair> queryParams = new ArrayList<>();
List<Pair> collectionQueryParams = new ArrayList<>();
Map<String, String> headerParams = new HashMap<>();
addCdsProtectedApiHeaders(headerParams);
String[] authNames = new String[] { };
return apiClient.buildCall(path, "GET", queryParams, collectionQueryParams, postBody, headerParams, authNames, _callback);
}
Expand Down
Loading

0 comments on commit ed1385f

Please sign in to comment.