拆分大文件
This commit is contained in:
@@ -504,10 +504,7 @@ impl SmsAuthProvider {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn verify_code(
|
||||
&self,
|
||||
request: SmsVerifyCodeRequest,
|
||||
) -> Result<(), SmsProviderError> {
|
||||
pub async fn verify_code(&self, request: SmsVerifyCodeRequest) -> Result<(), SmsProviderError> {
|
||||
match self {
|
||||
Self::Mock(provider) => provider.verify_code(request).await,
|
||||
Self::Aliyun(provider) => provider.verify_code(request).await,
|
||||
@@ -520,7 +517,8 @@ impl MockSmsAuthProvider {
|
||||
&self,
|
||||
request: SmsSendCodeRequest,
|
||||
) -> Result<SmsSendCodeResult, SmsProviderError> {
|
||||
let provider_out_id = build_sms_provider_out_id(&request.scene, &request.national_phone_number);
|
||||
let provider_out_id =
|
||||
build_sms_provider_out_id(&request.scene, &request.national_phone_number);
|
||||
|
||||
Ok(SmsSendCodeResult {
|
||||
cooldown_seconds: self.config.interval_seconds,
|
||||
@@ -530,10 +528,7 @@ impl MockSmsAuthProvider {
|
||||
})
|
||||
}
|
||||
|
||||
async fn verify_code(
|
||||
&self,
|
||||
request: SmsVerifyCodeRequest,
|
||||
) -> Result<(), SmsProviderError> {
|
||||
async fn verify_code(&self, request: SmsVerifyCodeRequest) -> Result<(), SmsProviderError> {
|
||||
if request.verify_code.trim() != self.config.mock_verify_code {
|
||||
return Err(SmsProviderError::InvalidVerifyCode);
|
||||
}
|
||||
@@ -546,7 +541,8 @@ impl AliyunSmsAuthProvider {
|
||||
&self,
|
||||
request: SmsSendCodeRequest,
|
||||
) -> Result<SmsSendCodeResult, SmsProviderError> {
|
||||
let provider_out_id = build_sms_provider_out_id(&request.scene, &request.national_phone_number);
|
||||
let provider_out_id =
|
||||
build_sms_provider_out_id(&request.scene, &request.national_phone_number);
|
||||
let phone_masked = mask_phone_number(&request.national_phone_number);
|
||||
let template_param = serde_json::json!({
|
||||
self.config.template_param_key.clone(): "##code##",
|
||||
@@ -577,26 +573,23 @@ impl AliyunSmsAuthProvider {
|
||||
query.insert("SignatureVersion".to_string(), "1.0".to_string());
|
||||
query.insert(
|
||||
"AccessKeyId".to_string(),
|
||||
self.config
|
||||
.access_key_id
|
||||
.clone()
|
||||
.unwrap_or_default(),
|
||||
self.config.access_key_id.clone().unwrap_or_default(),
|
||||
);
|
||||
query.insert(
|
||||
"PhoneNumber".to_string(),
|
||||
request.national_phone_number.trim().to_string(),
|
||||
);
|
||||
query.insert(
|
||||
"CountryCode".to_string(),
|
||||
self.config.country_code.clone(),
|
||||
);
|
||||
query.insert("CountryCode".to_string(), self.config.country_code.clone());
|
||||
query.insert("SignName".to_string(), self.config.sign_name.clone());
|
||||
query.insert(
|
||||
"TemplateCode".to_string(),
|
||||
self.config.template_code.clone(),
|
||||
);
|
||||
query.insert("TemplateParam".to_string(), template_param);
|
||||
query.insert("CodeLength".to_string(), self.config.code_length.to_string());
|
||||
query.insert(
|
||||
"CodeLength".to_string(),
|
||||
self.config.code_length.to_string(),
|
||||
);
|
||||
query.insert("CodeType".to_string(), self.config.code_type.to_string());
|
||||
query.insert(
|
||||
"ValidTime".to_string(),
|
||||
@@ -640,7 +633,10 @@ impl AliyunSmsAuthProvider {
|
||||
provider_request_id = body
|
||||
.request_id
|
||||
.as_deref()
|
||||
.or_else(|| body.model.as_ref().and_then(|model| model.request_id.as_deref()))
|
||||
.or_else(|| body
|
||||
.model
|
||||
.as_ref()
|
||||
.and_then(|model| model.request_id.as_deref()))
|
||||
.unwrap_or("unknown"),
|
||||
provider_out_id = body
|
||||
.model
|
||||
@@ -661,7 +657,10 @@ impl AliyunSmsAuthProvider {
|
||||
provider_request_id = body
|
||||
.request_id
|
||||
.as_deref()
|
||||
.or_else(|| body.model.as_ref().and_then(|model| model.request_id.as_deref()))
|
||||
.or_else(|| body
|
||||
.model
|
||||
.as_ref()
|
||||
.and_then(|model| model.request_id.as_deref()))
|
||||
.unwrap_or("unknown"),
|
||||
provider_out_id = body
|
||||
.model
|
||||
@@ -680,17 +679,16 @@ impl AliyunSmsAuthProvider {
|
||||
Ok(SmsSendCodeResult {
|
||||
cooldown_seconds: self.config.interval_seconds,
|
||||
expires_in_seconds: self.config.valid_time_seconds,
|
||||
provider_request_id: body
|
||||
.request_id
|
||||
.or_else(|| body.model.as_ref().and_then(|model| model.request_id.clone())),
|
||||
provider_request_id: body.request_id.or_else(|| {
|
||||
body.model
|
||||
.as_ref()
|
||||
.and_then(|model| model.request_id.clone())
|
||||
}),
|
||||
provider_out_id: body.model.and_then(|model| model.out_id),
|
||||
})
|
||||
}
|
||||
|
||||
async fn verify_code(
|
||||
&self,
|
||||
request: SmsVerifyCodeRequest,
|
||||
) -> Result<(), SmsProviderError> {
|
||||
async fn verify_code(&self, request: SmsVerifyCodeRequest) -> Result<(), SmsProviderError> {
|
||||
let mut query = BTreeMap::new();
|
||||
query.insert("Action".to_string(), "CheckSmsVerifyCode".to_string());
|
||||
query.insert("Format".to_string(), "json".to_string());
|
||||
@@ -701,19 +699,13 @@ impl AliyunSmsAuthProvider {
|
||||
query.insert("SignatureVersion".to_string(), "1.0".to_string());
|
||||
query.insert(
|
||||
"AccessKeyId".to_string(),
|
||||
self.config
|
||||
.access_key_id
|
||||
.clone()
|
||||
.unwrap_or_default(),
|
||||
self.config.access_key_id.clone().unwrap_or_default(),
|
||||
);
|
||||
query.insert(
|
||||
"PhoneNumber".to_string(),
|
||||
request.national_phone_number.trim().to_string(),
|
||||
);
|
||||
query.insert(
|
||||
"CountryCode".to_string(),
|
||||
self.config.country_code.clone(),
|
||||
);
|
||||
query.insert("CountryCode".to_string(), self.config.country_code.clone());
|
||||
query.insert(
|
||||
"VerifyCode".to_string(),
|
||||
request.verify_code.trim().to_string(),
|
||||
@@ -746,12 +738,7 @@ impl AliyunSmsAuthProvider {
|
||||
body.code,
|
||||
));
|
||||
}
|
||||
if body
|
||||
.model
|
||||
.and_then(|model| model.verify_result)
|
||||
.as_deref()
|
||||
!= Some("PASS")
|
||||
{
|
||||
if body.model.and_then(|model| model.verify_result).as_deref() != Some("PASS") {
|
||||
return Err(SmsProviderError::InvalidVerifyCode);
|
||||
}
|
||||
|
||||
@@ -759,11 +746,9 @@ impl AliyunSmsAuthProvider {
|
||||
}
|
||||
|
||||
fn sign_query(&self, query: &mut BTreeMap<String, String>) -> Result<(), SmsProviderError> {
|
||||
let access_key_secret = self
|
||||
.config
|
||||
.access_key_secret
|
||||
.as_deref()
|
||||
.ok_or_else(|| SmsProviderError::InvalidConfig("阿里云短信 AccessKeySecret 未配置".to_string()))?;
|
||||
let access_key_secret = self.config.access_key_secret.as_deref().ok_or_else(|| {
|
||||
SmsProviderError::InvalidConfig("阿里云短信 AccessKeySecret 未配置".to_string())
|
||||
})?;
|
||||
let canonicalized = canonicalize_aliyun_rpc_params(query);
|
||||
let string_to_sign = format!(
|
||||
"POST&{}&{}",
|
||||
@@ -771,7 +756,9 @@ impl AliyunSmsAuthProvider {
|
||||
aliyun_percent_encode(&canonicalized)
|
||||
);
|
||||
let mut signer = HmacSha1::new_from_slice(format!("{access_key_secret}&").as_bytes())
|
||||
.map_err(|error| SmsProviderError::InvalidConfig(format!("初始化短信签名器失败:{error}")))?;
|
||||
.map_err(|error| {
|
||||
SmsProviderError::InvalidConfig(format!("初始化短信签名器失败:{error}"))
|
||||
})?;
|
||||
signer.update(string_to_sign.as_bytes());
|
||||
let signature = BASE64_STANDARD.encode(signer.finalize().into_bytes());
|
||||
query.insert("Signature".to_string(), signature);
|
||||
@@ -1138,9 +1125,10 @@ async fn parse_aliyun_json_response(
|
||||
.text()
|
||||
.await
|
||||
.map_err(|error| SmsProviderError::Upstream(format!("{fallback_message}:{error}")))?;
|
||||
let payload = serde_json::from_str::<AliyunSendSmsVerifyCodeResponse>(&body).map_err(|error| {
|
||||
SmsProviderError::Upstream(format!("{fallback_message}:响应解析失败:{error}"))
|
||||
})?;
|
||||
let payload =
|
||||
serde_json::from_str::<AliyunSendSmsVerifyCodeResponse>(&body).map_err(|error| {
|
||||
SmsProviderError::Upstream(format!("{fallback_message}:响应解析失败:{error}"))
|
||||
})?;
|
||||
if status.is_client_error() || status.is_server_error() {
|
||||
return Err(map_http_status_to_sms_provider_error(
|
||||
fallback_message,
|
||||
@@ -1160,8 +1148,10 @@ async fn parse_aliyun_json_response_for_verify(
|
||||
.text()
|
||||
.await
|
||||
.map_err(|error| SmsProviderError::Upstream(format!("验证码校验失败:{error}")))?;
|
||||
let payload = serde_json::from_str::<AliyunCheckSmsVerifyCodeResponse>(&body)
|
||||
.map_err(|error| SmsProviderError::Upstream(format!("验证码校验失败:响应解析失败:{error}")))?;
|
||||
let payload =
|
||||
serde_json::from_str::<AliyunCheckSmsVerifyCodeResponse>(&body).map_err(|error| {
|
||||
SmsProviderError::Upstream(format!("验证码校验失败:响应解析失败:{error}"))
|
||||
})?;
|
||||
if status.is_client_error() || status.is_server_error() {
|
||||
return Err(map_http_status_to_sms_provider_error(
|
||||
"验证码校验失败",
|
||||
@@ -1461,7 +1451,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn sms_auth_provider_kind_parses_supported_values() {
|
||||
assert_eq!(SmsAuthProviderKind::parse("mock"), Some(SmsAuthProviderKind::Mock));
|
||||
assert_eq!(
|
||||
SmsAuthProviderKind::parse("mock"),
|
||||
Some(SmsAuthProviderKind::Mock)
|
||||
);
|
||||
assert_eq!(
|
||||
SmsAuthProviderKind::parse("aliyun"),
|
||||
Some(SmsAuthProviderKind::Aliyun)
|
||||
@@ -1482,7 +1475,10 @@ mod tests {
|
||||
.expect("send code should succeed");
|
||||
|
||||
assert_eq!(send_result.cooldown_seconds, DEFAULT_SMS_INTERVAL_SECONDS);
|
||||
assert_eq!(send_result.expires_in_seconds, DEFAULT_SMS_VALID_TIME_SECONDS);
|
||||
assert_eq!(
|
||||
send_result.expires_in_seconds,
|
||||
DEFAULT_SMS_VALID_TIME_SECONDS
|
||||
);
|
||||
assert_eq!(
|
||||
send_result.provider_request_id.as_deref(),
|
||||
Some("mock-request-id")
|
||||
@@ -1548,7 +1544,10 @@ mod tests {
|
||||
#[test]
|
||||
fn canonicalize_aliyun_rpc_params_keeps_sorted_percent_encoded_order() {
|
||||
let mut params = BTreeMap::new();
|
||||
params.insert("TemplateParam".to_string(), "{\"code\":\"##code##\"}".to_string());
|
||||
params.insert(
|
||||
"TemplateParam".to_string(),
|
||||
"{\"code\":\"##code##\"}".to_string(),
|
||||
);
|
||||
params.insert("Action".to_string(), "SendSmsVerifyCode".to_string());
|
||||
params.insert("PhoneNumber".to_string(), "13800138000".to_string());
|
||||
|
||||
@@ -1580,7 +1579,10 @@ mod tests {
|
||||
assert_eq!(payload.request_id.as_deref(), Some("req_123"));
|
||||
assert_eq!(payload.success, Some(true));
|
||||
assert_eq!(
|
||||
payload.model.as_ref().and_then(|model| model.out_id.as_deref()),
|
||||
payload
|
||||
.model
|
||||
.as_ref()
|
||||
.and_then(|model| model.out_id.as_deref()),
|
||||
Some("out_789")
|
||||
);
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user