Skip to content

Commit

Permalink
Merge 0a0a7d9 into 74e28d1
Browse files Browse the repository at this point in the history
  • Loading branch information
javidlulu committed Oct 12, 2020
2 parents 74e28d1 + 0a0a7d9 commit 84d6ffe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/main/java/jp/ne/paypay/auth/HmacAuth.java
Expand Up @@ -69,10 +69,20 @@ private byte[] getHmacData(String nonce, long epoch, String hash){
localContentType = this.contentType;
}
String DELIMITER = "\n";
return (this.requestUrl + DELIMITER + this.httpMethod + DELIMITER + nonce + DELIMITER + epoch + DELIMITER
return (formatRequestUrl(this.requestUrl) + DELIMITER + this.httpMethod + DELIMITER + nonce + DELIMITER + epoch + DELIMITER
+ localContentType + DELIMITER + hash)
.getBytes(StandardCharsets.UTF_8);
}
private String formatRequestUrl(String requestUrl){
try{
if(requestUrl.contains("?")){
return requestUrl.substring(0, requestUrl.indexOf("?"));
}
}catch (Exception e){
System.out.println("Exception while formatting request url for HMAC Auth");
}
return requestUrl;
}
@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (apiKey == null || apiSecretKey == null) {
Expand All @@ -84,7 +94,6 @@ public void applyToParams(List<Pair> queryParams, Map<String, String> headerPara
}catch (Exception e){
System.err.println("Error in getting Authorization: "+e);
}

}

public String getApiKey() {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/jp/ne/paypay/example/UserApiExample.java
@@ -0,0 +1,42 @@
package jp.ne.paypay.example;

import jp.ne.paypay.ApiClient;
import jp.ne.paypay.ApiException;
import jp.ne.paypay.Configuration;
import jp.ne.paypay.api.UserApi;
import jp.ne.paypay.model.NotDataResponse;
import jp.ne.paypay.model.UserAuthorizationStatus;

public class UserApiExample {


public static void main(String[] args) throws ApiException {
ApiClient apiClient = new Configuration().getDefaultApiClient();
apiClient.setProductionMode(false);
apiClient.setApiKey("API_KEY");
apiClient.setApiSecretKey("API_SECRET_KEY");
apiClient.setAssumeMerchant("ASSUME_MERCHANT_ID");
String userAuthorizationId = "USER_AUTHORIZATION_ID";

UserApi userApi = new UserApi(apiClient);

getOrUnlinkUser(userAuthorizationId, userApi, false);
//Replace the "userAuthorizationId" with the actual one. Please note that this will Unlink the user from the client
getOrUnlinkUser("userAuthorizationId", userApi, true);
}

private static void getOrUnlinkUser(String userAuthorizationId, UserApi userApi, boolean unlinkUser) {
try{
if(unlinkUser){
NotDataResponse notDataResponse = userApi.unlinkUser(userAuthorizationId);
System.out.println(notDataResponse);
}else{
UserAuthorizationStatus userAuthorizationStatus = userApi.getUserAuthorizationStatus(userAuthorizationId);
System.out.println(userAuthorizationStatus);
}
}catch (ApiException e){
System.out.println(e.getResponseBody());
}
}
}

7 changes: 6 additions & 1 deletion src/test/java/jp/ne/paypay/api/HmacAuthTest.java
Expand Up @@ -35,10 +35,15 @@ public void hmacAuthTest(){
Assert.assertNotNull(hmacAuth.getHttpMethod());
Assert.assertNotNull(hmacAuth.getRequestBody());
Assert.assertNotNull(hmacAuth.getRequestUrl());
hmacAuth.setRequestUrl("/v2/api/test?param=p1");
hmacAuth.applyToParams(queryParams, headerParams);
Assert.assertTrue(headerParams.get("Authorization").startsWith("hmac"));
hmacAuth.setRequestUrl(null);
hmacAuth.applyToParams(queryParams, headerParams);
Assert.assertTrue(headerParams.get("Authorization").startsWith("hmac"));
hmacAuth.setContentType(null);
headerParams = new HashMap<>();
hmacAuth.applyToParams(queryParams, headerParams);
Assert.assertNull(headerParams.get("Authorization"));

}
}

0 comments on commit 84d6ffe

Please sign in to comment.