fix auth login state race
This commit is contained in:
@@ -3269,6 +3269,31 @@ mod tests {
|
||||
assert_eq!(login_response.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phone_auth_sms_provider_errors_keep_upstream_http_semantics() {
|
||||
let invalid_config = crate::phone_auth::map_phone_auth_error(
|
||||
module_auth::PhoneAuthError::SmsProviderInvalidConfig(
|
||||
"阿里云短信 AccessKeyId 未配置".to_string(),
|
||||
),
|
||||
);
|
||||
assert_eq!(
|
||||
invalid_config.status_code(),
|
||||
StatusCode::SERVICE_UNAVAILABLE
|
||||
);
|
||||
assert_eq!(invalid_config.message(), "阿里云短信 AccessKeyId 未配置");
|
||||
|
||||
let upstream = crate::phone_auth::map_phone_auth_error(
|
||||
module_auth::PhoneAuthError::SmsProviderUpstream(
|
||||
"短信验证码发送失败:check frequency failed".to_string(),
|
||||
),
|
||||
);
|
||||
assert_eq!(upstream.status_code(), StatusCode::BAD_GATEWAY);
|
||||
assert_eq!(
|
||||
upstream.message(),
|
||||
"短信验证码发送失败:check frequency failed"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn wechat_start_returns_mock_callback_url_with_state() {
|
||||
let config = AppConfig {
|
||||
|
||||
@@ -320,6 +320,12 @@ pub fn map_phone_auth_error(error: PhoneAuthError) -> AppError {
|
||||
PhoneAuthError::UserNotFound => {
|
||||
AppError::from_status(StatusCode::UNAUTHORIZED).with_message(error.to_string())
|
||||
}
|
||||
PhoneAuthError::SmsProviderInvalidConfig(_) => {
|
||||
AppError::from_status(StatusCode::SERVICE_UNAVAILABLE).with_message(error.to_string())
|
||||
}
|
||||
PhoneAuthError::SmsProviderUpstream(_) => {
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_message(error.to_string())
|
||||
}
|
||||
PhoneAuthError::Store(_) | PhoneAuthError::PasswordHash(_) => {
|
||||
map_phone_auth_platform_store_error(error.to_string())
|
||||
}
|
||||
|
||||
@@ -396,6 +396,12 @@ fn map_wechat_bind_phone_error(error: module_auth::PhoneAuthError) -> AppError {
|
||||
module_auth::PhoneAuthError::UserNotFound => {
|
||||
AppError::from_status(StatusCode::UNAUTHORIZED).with_message(error.to_string())
|
||||
}
|
||||
module_auth::PhoneAuthError::SmsProviderInvalidConfig(_) => {
|
||||
AppError::from_status(StatusCode::SERVICE_UNAVAILABLE).with_message(error.to_string())
|
||||
}
|
||||
module_auth::PhoneAuthError::SmsProviderUpstream(_) => {
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_message(error.to_string())
|
||||
}
|
||||
module_auth::PhoneAuthError::Store(_) | module_auth::PhoneAuthError::PasswordHash(_) => {
|
||||
AppError::from_status(StatusCode::INTERNAL_SERVER_ERROR).with_message(error.to_string())
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ pub enum PhoneAuthError {
|
||||
VerifyAttemptsExceeded,
|
||||
UserNotFound,
|
||||
UserStateMismatch,
|
||||
SmsProviderInvalidConfig(String),
|
||||
SmsProviderUpstream(String),
|
||||
Store(String),
|
||||
PasswordHash(String),
|
||||
}
|
||||
@@ -88,6 +90,9 @@ impl fmt::Display for PhoneAuthError {
|
||||
Self::VerifyAttemptsExceeded => f.write_str("验证码错误次数过多,请重新获取验证码"),
|
||||
Self::UserNotFound => f.write_str("用户不存在"),
|
||||
Self::UserStateMismatch => f.write_str("当前账号状态不允许执行该操作"),
|
||||
Self::SmsProviderInvalidConfig(message) | Self::SmsProviderUpstream(message) => {
|
||||
f.write_str(message)
|
||||
}
|
||||
Self::Store(message) | Self::PasswordHash(message) => f.write_str(message),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1862,9 +1862,10 @@ impl InMemoryAuthStore {
|
||||
fn map_sms_provider_error_to_phone_error(error: SmsProviderError) -> PhoneAuthError {
|
||||
match error {
|
||||
SmsProviderError::InvalidVerifyCode => PhoneAuthError::InvalidVerifyCode,
|
||||
SmsProviderError::InvalidConfig(message) | SmsProviderError::Upstream(message) => {
|
||||
PhoneAuthError::Store(message)
|
||||
SmsProviderError::InvalidConfig(message) => {
|
||||
PhoneAuthError::SmsProviderInvalidConfig(message)
|
||||
}
|
||||
SmsProviderError::Upstream(message) => PhoneAuthError::SmsProviderUpstream(message),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user