-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
1 changed file
with
94 additions
and
94 deletions.
There are no files selected for viewing
188 changes: 94 additions & 94 deletions
188
src/main/java/com/wechat/pay/contrib/apache/httpclient/auth/WechatPay2Credentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,94 @@ | ||
package com.wechat.pay.contrib.apache.httpclient.auth; | ||
|
||
import com.wechat.pay.contrib.apache.httpclient.Credentials; | ||
import com.wechat.pay.contrib.apache.httpclient.WechatPayUploadHttpPost; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.SecureRandom; | ||
|
||
import org.apache.http.HttpEntityEnclosingRequest; | ||
import org.apache.http.client.methods.HttpRequestWrapper; | ||
import org.apache.http.util.EntityUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class WechatPay2Credentials implements Credentials { | ||
private static final Logger log = LoggerFactory.getLogger(WechatPay2Credentials.class); | ||
|
||
private static final String SYMBOLS = | ||
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
private static final SecureRandom RANDOM = new SecureRandom(); | ||
protected String merchantId; | ||
protected Signer signer; | ||
|
||
public WechatPay2Credentials(String merchantId, Signer signer) { | ||
this.merchantId = merchantId; | ||
this.signer = signer; | ||
} | ||
|
||
public String getMerchantId() { | ||
return merchantId; | ||
} | ||
|
||
protected long generateTimestamp() { | ||
return System.currentTimeMillis() / 1000; | ||
} | ||
|
||
protected String generateNonceStr() { | ||
char[] nonceChars = new char[32]; | ||
for (int index = 0; index < nonceChars.length; ++index) { | ||
nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length())); | ||
} | ||
return new String(nonceChars); | ||
} | ||
|
||
@Override | ||
public final String getSchema() { | ||
return "WECHATPAY2-SHA256-RSA2048"; | ||
} | ||
|
||
@Override | ||
public final String getToken(HttpRequestWrapper request) throws IOException { | ||
String nonceStr = generateNonceStr(); | ||
long timestamp = generateTimestamp(); | ||
|
||
String message = buildMessage(nonceStr, timestamp, request); | ||
log.debug("authorization message=[{}]", message); | ||
|
||
Signer.SignatureResult signature = signer.sign(message.getBytes(StandardCharsets.UTF_8)); | ||
|
||
String token = "mchid=\"" + getMerchantId() + "\"," | ||
+ "nonce_str=\"" + nonceStr + "\"," | ||
+ "timestamp=\"" + timestamp + "\"," | ||
+ "serial_no=\"" + signature.certificateSerialNumber + "\"," | ||
+ "signature=\"" + signature.sign + "\""; | ||
log.debug("authorization token=[{}]", token); | ||
|
||
return token; | ||
} | ||
|
||
protected final String buildMessage(String nonce, long timestamp, HttpRequestWrapper request) | ||
throws IOException { | ||
URI uri = request.getURI(); | ||
String canonicalUrl = uri.getRawPath(); | ||
if (uri.getQuery() != null) { | ||
canonicalUrl += "?" + uri.getRawQuery(); | ||
} | ||
|
||
String body = ""; | ||
// PATCH,POST,PUT | ||
if (request.getOriginal() instanceof WechatPayUploadHttpPost) { | ||
body = ((WechatPayUploadHttpPost) request.getOriginal()).getMeta(); | ||
} else if (request instanceof HttpEntityEnclosingRequest) { | ||
body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity()); | ||
} | ||
|
||
return request.getRequestLine().getMethod() + "\n" | ||
+ canonicalUrl + "\n" | ||
+ timestamp + "\n" | ||
+ nonce + "\n" | ||
+ body + "\n"; | ||
} | ||
|
||
} | ||
package com.wechat.pay.contrib.apache.httpclient.auth; | ||
|
||
import com.wechat.pay.contrib.apache.httpclient.Credentials; | ||
import com.wechat.pay.contrib.apache.httpclient.WechatPayUploadHttpPost; | ||
import org.apache.http.HttpEntityEnclosingRequest; | ||
import org.apache.http.client.methods.HttpRequestWrapper; | ||
import org.apache.http.util.EntityUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.SecureRandom; | ||
|
||
public class WechatPay2Credentials implements Credentials { | ||
private static final Logger log = LoggerFactory.getLogger(WechatPay2Credentials.class); | ||
|
||
private static final String SYMBOLS = | ||
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
private static final SecureRandom RANDOM = new SecureRandom(); | ||
protected String merchantId; | ||
protected Signer signer; | ||
|
||
public WechatPay2Credentials(String merchantId, Signer signer) { | ||
this.merchantId = merchantId; | ||
this.signer = signer; | ||
} | ||
|
||
public String getMerchantId() { | ||
return merchantId; | ||
} | ||
|
||
protected long generateTimestamp() { | ||
return System.currentTimeMillis() / 1000; | ||
} | ||
|
||
protected String generateNonceStr() { | ||
char[] nonceChars = new char[32]; | ||
for (int index = 0; index < nonceChars.length; ++index) { | ||
nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length())); | ||
} | ||
return new String(nonceChars); | ||
} | ||
|
||
@Override | ||
public final String getSchema() { | ||
return "WECHATPAY2-SHA256-RSA2048"; | ||
} | ||
|
||
@Override | ||
public final String getToken(HttpRequestWrapper request) throws IOException { | ||
String nonceStr = generateNonceStr(); | ||
long timestamp = generateTimestamp(); | ||
|
||
String message = buildMessage(nonceStr, timestamp, request); | ||
log.debug("authorization message=[{}]", message); | ||
|
||
Signer.SignatureResult signature = signer.sign(message.getBytes(StandardCharsets.UTF_8)); | ||
|
||
String token = "mchid=\"" + getMerchantId() + "\"," | ||
+ "nonce_str=\"" + nonceStr + "\"," | ||
+ "timestamp=\"" + timestamp + "\"," | ||
+ "serial_no=\"" + signature.certificateSerialNumber + "\"," | ||
+ "signature=\"" + signature.sign + "\""; | ||
log.debug("authorization token=[{}]", token); | ||
|
||
return token; | ||
} | ||
|
||
protected final String buildMessage(String nonce, long timestamp, HttpRequestWrapper request) | ||
throws IOException { | ||
URI uri = request.getURI(); | ||
String canonicalUrl = uri.getRawPath(); | ||
if (uri.getQuery() != null) { | ||
canonicalUrl += "?" + uri.getRawQuery(); | ||
} | ||
|
||
String body = ""; | ||
// PATCH,POST,PUT | ||
if (request.getOriginal() instanceof WechatPayUploadHttpPost) { | ||
body = ((WechatPayUploadHttpPost) request.getOriginal()).getMeta(); | ||
} else if (request instanceof HttpEntityEnclosingRequest) { | ||
body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity(), StandardCharsets.UTF_8); | ||
} | ||
|
||
return request.getRequestLine().getMethod() + "\n" | ||
+ canonicalUrl + "\n" | ||
+ timestamp + "\n" | ||
+ nonce + "\n" | ||
+ body + "\n"; | ||
} | ||
|
||
} |