Skip to content

Commit c5d3052

Browse files
feat(shield-axum): add user required extractor (#29)
1 parent 48b3182 commit c5d3052

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

packages/integrations/shield-axum/src/extract.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,23 @@ impl<S: Send + Sync, U: User + Clone + 'static> FromRequestParts<S> for ExtractU
6161
))
6262
}
6363
}
64+
65+
pub struct UserRequired<U: User>(pub U);
66+
67+
#[async_trait]
68+
impl<S: Send + Sync, U: User + Clone + 'static> FromRequestParts<S> for UserRequired<U> {
69+
type Rejection = (StatusCode, &'static str);
70+
71+
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
72+
parts
73+
.extensions
74+
.get::<Option<U>>()
75+
.cloned()
76+
.ok_or((
77+
StatusCode::INTERNAL_SERVER_ERROR,
78+
"Can't extract Shield user. Is `ShieldLayer` enabled?",
79+
))
80+
.and_then(|user| user.ok_or((StatusCode::UNAUTHORIZED, "Unauthorized")))
81+
.map(UserRequired)
82+
}
83+
}

0 commit comments

Comments
 (0)