Skip to content

Commit

Permalink
updated HMAC Auth logic for api with request params, added example cl…
Browse files Browse the repository at this point in the history
…ass for user apis
  • Loading branch information
javidlulu committed Oct 12, 2020
1 parent 74e28d1 commit 1053aee
Show file tree
Hide file tree
Showing 3 changed files with 61 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("?")){
requestUrl = 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
47 changes: 47 additions & 0 deletions src/main/java/jp/ne/paypay/example/UserApiExample.java
@@ -0,0 +1,47 @@
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);

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

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

4 changes: 3 additions & 1 deletion src/test/java/jp/ne/paypay/api/HmacAuthTest.java
Expand Up @@ -35,10 +35,12 @@ 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.setContentType(null);
headerParams = new HashMap<>();
hmacAuth.applyToParams(queryParams, headerParams);
Assert.assertNull(headerParams.get("Authorization"));

}
}

0 comments on commit 1053aee

Please sign in to comment.