Skip to content

Commit

Permalink
fix(tenpayv3): 修复保险行业委托代扣预签约接口无法加密请求中敏感数据字段的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fudiwei committed May 8, 2024
1 parent 61d2902 commit 11afc1c
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// <summary>
/// <para>表示 [POST] /papay/insurance-sign/contracts/pre-entrust-sign/h5 接口的请求。</para>
/// </summary>
[WechatTenpaySensitive]
public class PresignPAPayInsuranceSignContractEntrustH5Request : WechatTenpayRequest
{
public static class Types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// <summary>
/// <para>表示 [POST] /papay/insurance-sign/contracts/pre-entrust-sign/mini-program 接口的请求。</para>
/// </summary>
[WechatTenpaySensitive]
public class PresignPAPayInsuranceSignContractEntrustMiniProgramRequest : WechatTenpayRequest
{
public static class Types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,183 @@ static void AssertMockRequestModel(Models.CreateNewTaxControlFapiaoApplicationCa
}
}

[Fact(DisplayName = "测试用例:加密请求中的敏感数据([POST] /papay/insurance-sign/contracts/pre-entrust-sign/h5)")]
public async Task TestEncryptRequestSensitiveProperty_PresignPAPayInsuranceSignContractEntrustH5Request()
{
static Models.PresignPAPayInsuranceSignContractEntrustH5Request GenerateMockRequestModel()
{
return new Models.PresignPAPayInsuranceSignContractEntrustH5Request()
{
RealIdentity = new Models.PresignPAPayInsuranceSignContractEntrustH5Request.Types.RealIdentity()
{
RealName = MOCK_PLAIN_STR,
IdCardNumber = MOCK_PLAIN_STR
}
};
}

static void AssertMockRequestModel(Models.PresignPAPayInsuranceSignContractEntrustH5Request request, Func<string, string> decryptor)
{
Assert.NotEqual(MOCK_PLAIN_STR, request.RealIdentity!.RealName);
Assert.NotEqual(MOCK_PLAIN_STR, request.RealIdentity!.IdCardNumber);
Assert.Equal(MOCK_PLAIN_STR, decryptor.Invoke(request.RealIdentity!.RealName));
Assert.Equal(MOCK_PLAIN_STR, decryptor.Invoke(request.RealIdentity!.IdCardNumber));
Assert.Equal(MOCK_CERT_SN, request.WechatpayCertificateSerialNumber!, ignoreCase: true);
}

if (!string.IsNullOrEmpty(TestConfigs.WechatMerchantRSACertificatePrivateKey))
{
using (var client = CreateMockClientUseRSA(autoEncrypt: false))
{
var request = GenerateMockRequestModel();
client.EncryptRequestSensitiveProperty(request);
AssertMockRequestModel(request, (cipher) => Utilities.RSAUtility.DecryptWithECB(RSA_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}

using (var client = CreateMockClientUseRSA(autoEncrypt: true))
{
var request = GenerateMockRequestModel();
await client.ExecutePresignPAPayInsuranceSignContractEntrustH5Async(request);
AssertMockRequestModel(request, (cipher) => Utilities.RSAUtility.DecryptWithECB(RSA_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}
}

if (!string.IsNullOrEmpty(TestConfigs.WechatMerchantSM2CertificatePrivateKey))
{
using (var client = CreateMockClientUseSM2(autoEncrypt: false))
{
var request = GenerateMockRequestModel();
client.EncryptRequestSensitiveProperty(request);
AssertMockRequestModel(request, (cipher) => Utilities.SM2Utility.Decrypt(SM2_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}

using (var client = CreateMockClientUseSM2(autoEncrypt: true))
{
var request = GenerateMockRequestModel();
await client.ExecutePresignPAPayInsuranceSignContractEntrustH5Async(request);
AssertMockRequestModel(request, (cipher) => Utilities.SM2Utility.Decrypt(SM2_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}
}
}

[Fact(DisplayName = "测试用例:加密请求中的敏感数据([POST] /papay/insurance-sign/contracts/pre-entrust-sign/jsapi)")]
public async Task TestEncryptRequestSensitiveProperty_PresignPAPayInsuranceSignContractEntrustJsapiRequest()
{
static Models.PresignPAPayInsuranceSignContractEntrustJsapiRequest GenerateMockRequestModel()
{
return new Models.PresignPAPayInsuranceSignContractEntrustJsapiRequest()
{
RealIdentity = new Models.PresignPAPayInsuranceSignContractEntrustJsapiRequest.Types.RealIdentity()
{
RealName = MOCK_PLAIN_STR,
IdCardNumber = MOCK_PLAIN_STR
}
};
}

static void AssertMockRequestModel(Models.PresignPAPayInsuranceSignContractEntrustJsapiRequest request, Func<string, string> decryptor)
{
Assert.NotEqual(MOCK_PLAIN_STR, request.RealIdentity!.RealName);
Assert.NotEqual(MOCK_PLAIN_STR, request.RealIdentity!.IdCardNumber);
Assert.Equal(MOCK_PLAIN_STR, decryptor.Invoke(request.RealIdentity!.RealName));
Assert.Equal(MOCK_PLAIN_STR, decryptor.Invoke(request.RealIdentity!.IdCardNumber));
Assert.Equal(MOCK_CERT_SN, request.WechatpayCertificateSerialNumber!, ignoreCase: true);
}

if (!string.IsNullOrEmpty(TestConfigs.WechatMerchantRSACertificatePrivateKey))
{
using (var client = CreateMockClientUseRSA(autoEncrypt: false))
{
var request = GenerateMockRequestModel();
client.EncryptRequestSensitiveProperty(request);
AssertMockRequestModel(request, (cipher) => Utilities.RSAUtility.DecryptWithECB(RSA_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}

using (var client = CreateMockClientUseRSA(autoEncrypt: true))
{
var request = GenerateMockRequestModel();
await client.ExecutePresignPAPayInsuranceSignContractEntrustJsapiAsync(request);
AssertMockRequestModel(request, (cipher) => Utilities.RSAUtility.DecryptWithECB(RSA_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}
}

if (!string.IsNullOrEmpty(TestConfigs.WechatMerchantSM2CertificatePrivateKey))
{
using (var client = CreateMockClientUseSM2(autoEncrypt: false))
{
var request = GenerateMockRequestModel();
client.EncryptRequestSensitiveProperty(request);
AssertMockRequestModel(request, (cipher) => Utilities.SM2Utility.Decrypt(SM2_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}

using (var client = CreateMockClientUseSM2(autoEncrypt: true))
{
var request = GenerateMockRequestModel();
await client.ExecutePresignPAPayInsuranceSignContractEntrustJsapiAsync(request);
AssertMockRequestModel(request, (cipher) => Utilities.SM2Utility.Decrypt(SM2_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}
}
}

[Fact(DisplayName = "测试用例:加密请求中的敏感数据([POST] /papay/insurance-sign/contracts/pre-entrust-sign/mini-program)")]
public async Task TestEncryptRequestSensitiveProperty_PresignPAPayInsuranceSignContractEntrustMiniProgramRequest()
{
static Models.PresignPAPayInsuranceSignContractEntrustMiniProgramRequest GenerateMockRequestModel()
{
return new Models.PresignPAPayInsuranceSignContractEntrustMiniProgramRequest()
{
RealIdentity = new Models.PresignPAPayInsuranceSignContractEntrustMiniProgramRequest.Types.RealIdentity()
{
RealName = MOCK_PLAIN_STR,
IdCardNumber = MOCK_PLAIN_STR
}
};
}

static void AssertMockRequestModel(Models.PresignPAPayInsuranceSignContractEntrustMiniProgramRequest request, Func<string, string> decryptor)
{
Assert.NotEqual(MOCK_PLAIN_STR, request.RealIdentity!.RealName);
Assert.NotEqual(MOCK_PLAIN_STR, request.RealIdentity!.IdCardNumber);
Assert.Equal(MOCK_PLAIN_STR, decryptor.Invoke(request.RealIdentity!.RealName));
Assert.Equal(MOCK_PLAIN_STR, decryptor.Invoke(request.RealIdentity!.IdCardNumber));
Assert.Equal(MOCK_CERT_SN, request.WechatpayCertificateSerialNumber!, ignoreCase: true);
}

if (!string.IsNullOrEmpty(TestConfigs.WechatMerchantRSACertificatePrivateKey))
{
using (var client = CreateMockClientUseRSA(autoEncrypt: false))
{
var request = GenerateMockRequestModel();
client.EncryptRequestSensitiveProperty(request);
AssertMockRequestModel(request, (cipher) => Utilities.RSAUtility.DecryptWithECB(RSA_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}

using (var client = CreateMockClientUseRSA(autoEncrypt: true))
{
var request = GenerateMockRequestModel();
await client.ExecutePresignPAPayInsuranceSignContractEntrustMiniProgramAsync(request);
AssertMockRequestModel(request, (cipher) => Utilities.RSAUtility.DecryptWithECB(RSA_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}
}

if (!string.IsNullOrEmpty(TestConfigs.WechatMerchantSM2CertificatePrivateKey))
{
using (var client = CreateMockClientUseSM2(autoEncrypt: false))
{
var request = GenerateMockRequestModel();
client.EncryptRequestSensitiveProperty(request);
AssertMockRequestModel(request, (cipher) => Utilities.SM2Utility.Decrypt(SM2_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}

using (var client = CreateMockClientUseSM2(autoEncrypt: true))
{
var request = GenerateMockRequestModel();
await client.ExecutePresignPAPayInsuranceSignContractEntrustMiniProgramAsync(request);
AssertMockRequestModel(request, (cipher) => Utilities.SM2Utility.Decrypt(SM2_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}
}
}

[Fact(DisplayName = "测试用例:加密请求中的敏感数据([POST] /payroll-card/authentications/pre-order-with-auth)")]
public async Task TestEncryptRequestSensitiveProperty_PreorderPayrollCardAuthenticationRequest()
{
Expand Down

0 comments on commit 11afc1c

Please sign in to comment.