refactor(auth): refresh crate root, error, pow and handler module
zhenyi committed
Modified lib/auth/handler/mod.rs lib/auth/handler/mod.rs +1 −0 View lib/auth/handler/mod.rs
@@ -4,6 +4,7 @@
44pub(crate) mod login;
55pub(crate) mod logout;
66pub(crate) mod me;
7pub(crate) mod middleware;
78pub(crate) mod oauth;
89pub(crate) mod openapi;
910pub(crate) mod password;
Modified lib/auth/handler/routes.rs lib/auth/handler/routes.rs +3 −0 View lib/auth/handler/routes.rs
@@ -59,6 +59,9 @@
5959 .layer(from_fn_with_state(svc.clone(), inject_jwt_ctx::<AuthService>)),
6060 )
6161 .layer(from_fn_with_state(svc.clone(), ratelimit::<AuthService>))
62 .layer(axum::middleware::from_fn(
63 crate::handler::middleware::metrics::metrics_layer,
64 ))
6265 .with_state(svc);
6366
6467 Router::new().nest("/auth", auth)
Modified lib/auth/pow/mod.rs lib/auth/pow/mod.rs +29 −1 View lib/auth/pow/mod.rs
@@ -42,6 +42,7 @@
4242use altcha::{create_challenge, verify_solution};
4343use base64::Engine;
4444use cache::redis::RedisCache;
45use metrics::histogram;
4546use sha2::{Digest, Sha256};
4647use uuid::Uuid;
4748
@@ -184,6 +185,8 @@
184185 .await
185186 .map_err(|e| PowError::Storage(e.to_string()))?;
186187
188 crate::metrics::record_pow_challenge_issued();
189
187190 Ok((challenge, ttl))
188191 }
189192
@@ -200,6 +203,9 @@
200203 solution: &Solution,
201204 expected_cid: Option<&str>,
202205 ) -> Result<VerifySolutionResult, PowError> {
206 use std::time::Instant;
207 use crate::metrics::{PowVerifyOutcome, record_pow_verify};
208
203209 let kv = self.kv.as_ref().ok_or_else(|| {
204210 PowError::Storage("pow redis handle not attached".to_string())
205211 })?;
@@ -210,6 +216,7 @@
210216 .await
211217 .map_err(|e| PowError::Storage(e.to_string()))?;
212218 if consumed.is_none() {
219 record_pow_verify(PowVerifyOutcome::FormatInvalid);
213220 return Err(PowError::NotIssued);
214221 }
215222
@@ -221,11 +228,32 @@
221228 .and_then(|d| d.get("cid"))
222229 .and_then(|v| v.as_str());
223230 if bound != Some(cid) {
231 record_pow_verify(PowVerifyOutcome::Mismatch);
224232 return Err(PowError::ClientMismatch);
225233 }
226234 }
227235
228 self.verify_solution(challenge, solution)
236 let start = Instant::now();
237 let result = self.verify_solution(challenge, solution);
238 let outcome = match &result {
239 Ok(r) if r.verified => PowVerifyOutcome::Success,
240 Ok(r) if r.expired => PowVerifyOutcome::Expired,
241 Ok(_) => PowVerifyOutcome::Mismatch,
242 Err(_) => PowVerifyOutcome::FormatInvalid,
243 };
244 record_pow_verify(outcome);
245 let outcome_label = match outcome {
246 PowVerifyOutcome::Success => "success",
247 PowVerifyOutcome::Expired => "expired",
248 PowVerifyOutcome::Mismatch => "mismatch",
249 PowVerifyOutcome::FormatInvalid => "format_invalid",
250 };
251 histogram!(
252 "auth_pow_verify_duration_seconds",
253 "outcome" => outcome_label
254 )
255 .record(start.elapsed().as_secs_f64());
256 result
229257 }
230258
231259 fn require_secret(&self) -> Result<String, PowError> {
Modified lib/auth/error.rs lib/auth/error.rs +20 −0 View lib/auth/error.rs
@@ -93,6 +93,26 @@
9393 | AuthError::PowFailed(_)
9494 )
9595 }
96
97 pub fn error_kind(&self) -> &'static str {
98 match self {
99 Self::BadRequest(_) => "bad_request",
100 Self::InvalidCredentials => "invalid_credentials",
101 Self::Unauthorized(_) => "unauthorized",
102 Self::Forbidden(_) => "forbidden",
103 Self::NotFound(_) => "not_found",
104 Self::Conflict(_) => "conflict",
105 Self::RateLimited(_) => "rate_limited",
106 Self::PowFailed(_) => "pow_failed",
107 Self::Internal(_) => "internal_other",
108 Self::Sqlx(_) => "internal_db",
109 Self::Redis(_) => "internal_redis",
110 Self::Pow(_) => "internal_pow",
111 Self::Jwt(_) => "internal_jwt",
112 Self::Email(_) => "internal_email",
113 Self::Uuid(_) => "internal_uuid",
114 }
115 }
96116}
97117
98118impl IntoResponse for AuthError {
Modified lib/auth/lib.rs lib/auth/lib.rs +6 −0 View lib/auth/lib.rs
@@ -2,6 +2,7 @@
22pub mod error;
33pub mod features;
44pub mod handler;
5pub mod metrics;
56pub mod pow;
67
78pub use config::{
@@ -10,4 +11,9 @@
1011};
1112pub use error::{AuthError, AuthResult};
1213pub use handler::router as auth_router;
14pub use metrics::{
15 JwtPairKind, JwtRevokeKind, OAuthAuthorizeOutcome, OAuthCallbackOutcome, OAuthProvider,
16 OAuthResolutionBranch, PasswordOp, PasswordOpTimer, PowVerifyOutcome, Timer, ensure_described,
17 record_outcome,
18};
1319pub use pow::{Challenge, PowError, PowService, PowSettings, Solution, VerifySolutionResult};