|
| 1 | +use crate::codoraframeworksecurity::context::Context; |
| 2 | +use axum::{ |
| 3 | + extract::{FromRequest, FromRequestParts, Request}, |
| 4 | + response::{IntoResponse, Response}, |
| 5 | +}; |
| 6 | +use http::request::Parts; |
| 7 | + |
| 8 | +#[derive(Debug)] |
| 9 | +pub struct MissingContext; |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +pub enum ContextRejection<T> { |
| 13 | + MissingContext(MissingContext), |
| 14 | + InnerRejection(T), |
| 15 | +} |
| 16 | + |
| 17 | +impl IntoResponse for MissingContext { |
| 18 | + fn into_response(self) -> Response { |
| 19 | + use http::StatusCode; |
| 20 | + |
| 21 | + StatusCode::INTERNAL_SERVER_ERROR.into_response() |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl<T> IntoResponse for ContextRejection<T> |
| 26 | +where |
| 27 | + T: IntoResponse, |
| 28 | +{ |
| 29 | + fn into_response(self) -> Response { |
| 30 | + match self { |
| 31 | + ContextRejection::MissingContext(missing_context) => missing_context.into_response(), |
| 32 | + ContextRejection::InnerRejection(t) => t.into_response(), |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, new)] |
| 38 | +pub struct TypedRequest<T> { |
| 39 | + pub parts: Parts, |
| 40 | + pub value: T, |
| 41 | +} |
| 42 | + |
| 43 | +impl<T> TypedRequest<T> { |
| 44 | + pub fn unpack(self) -> (http::request::Parts, T) { |
| 45 | + (self.parts, self.value) |
| 46 | + } |
| 47 | + |
| 48 | + #[inline] |
| 49 | + pub fn parts(&self) -> &http::request::Parts { |
| 50 | + &self.parts |
| 51 | + } |
| 52 | + |
| 53 | + #[inline] |
| 54 | + pub fn parts_mut(&mut self) -> &http::request::Parts { |
| 55 | + &mut self.parts |
| 56 | + } |
| 57 | + |
| 58 | + #[inline] |
| 59 | + pub fn value(&self) -> &T { |
| 60 | + &self.value |
| 61 | + } |
| 62 | + |
| 63 | + #[inline] |
| 64 | + pub fn value_mut(&mut self) -> &mut T { |
| 65 | + &mut self.value |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<S> FromRequestParts<S> for Context<Parts> |
| 70 | +where |
| 71 | + S: Send + Sync, |
| 72 | +{ |
| 73 | + type Rejection = MissingContext; |
| 74 | + |
| 75 | + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> { |
| 76 | + let context_with_empty_request = parts |
| 77 | + .extensions |
| 78 | + .get::<Context<()>>() |
| 79 | + .cloned() |
| 80 | + .ok_or(MissingContext)?; |
| 81 | + |
| 82 | + Ok(context_with_empty_request.map_request(|_| parts.clone())) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl<T, S> FromRequest<S> for Context<TypedRequest<T>> |
| 87 | +where |
| 88 | + S: Send + Sync, |
| 89 | + T: FromRequest<S>, |
| 90 | +{ |
| 91 | + type Rejection = ContextRejection<T::Rejection>; |
| 92 | + |
| 93 | + async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { |
| 94 | + let (mut parts, body) = req.into_parts(); |
| 95 | + let context = Context::from_request_parts(&mut parts, state) |
| 96 | + .await |
| 97 | + .map_err(ContextRejection::MissingContext)?; |
| 98 | + |
| 99 | + let inner_value = T::from_request(Request::from_parts(parts.clone(), body), state) |
| 100 | + .await |
| 101 | + .map_err(ContextRejection::InnerRejection)?; |
| 102 | + |
| 103 | + Ok(context.map_request(|_| TypedRequest::new(parts, inner_value))) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +#[cfg(test)] |
| 108 | +mod axum_tests { |
| 109 | + use crate::{adapter::TypedRequest, prelude::*}; |
| 110 | + use anyhow::Result; |
| 111 | + use axum::{ |
| 112 | + Json, Router, |
| 113 | + body::Body, |
| 114 | + extract::Request, |
| 115 | + response::{IntoResponse, Response}, |
| 116 | + routing::{get, post}, |
| 117 | + }; |
| 118 | + use http::{StatusCode, request::Parts}; |
| 119 | + use tower::ServiceExt; |
| 120 | + |
| 121 | + #[allow(dead_code)] |
| 122 | + #[derive(Debug, serde::Deserialize)] |
| 123 | + pub struct JwtBody { |
| 124 | + token: String, |
| 125 | + } |
| 126 | + |
| 127 | + #[tokio::test] |
| 128 | + async fn test_context_if_it_compiles_it_works() -> Result<()> { |
| 129 | + let app = Router::new() |
| 130 | + .route("/signup", get(|_context: Context<Parts>| async { () })) |
| 131 | + .route("/signin", get(|_context: Context<TypedRequest<Json<JwtBody>>>| async { () })) |
| 132 | + .layer(CFrameworkSecurity::default()); |
| 133 | + |
| 134 | + let req = Request::builder() |
| 135 | + .uri("/") |
| 136 | + .body(Body::empty())?; |
| 137 | + |
| 138 | + let res = app.oneshot(req).await?; |
| 139 | + assert_eq!(res.status(), StatusCode::OK); |
| 140 | + Ok(()) |
| 141 | + } |
| 142 | + |
| 143 | + #[tokio::test] |
| 144 | + async fn test_cookie_sign_in_with_axum_parts() -> Result<()> { |
| 145 | + let cf = CFrameworkSecurity::default().add_cookie(|option| { |
| 146 | + // setup option here |
| 147 | + |
| 148 | + option |
| 149 | + }); |
| 150 | + |
| 151 | + async fn authentication_handler(ctx: Context<Parts>) -> Result<Response, Response> { |
| 152 | + // cookie response |
| 153 | + let _res = ctx |
| 154 | + .sign_in_with_cookie(CookieState {}, CookiePayload {}) |
| 155 | + .await |
| 156 | + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR.into_response())?; |
| 157 | + |
| 158 | + Ok(String::from("Yet to be implemented").into_response()) |
| 159 | + } |
| 160 | + |
| 161 | + let app = Router::new() |
| 162 | + .route("/", post(authentication_handler)) |
| 163 | + .layer(cf); |
| 164 | + |
| 165 | + let req = Request::builder() |
| 166 | + .uri("/") |
| 167 | + .body(Body::empty())?; |
| 168 | + |
| 169 | + let res = app.oneshot(req).await?; |
| 170 | + assert_eq!(res.status(), StatusCode::OK); |
| 171 | + |
| 172 | + Ok(()) |
| 173 | + } |
| 174 | + |
| 175 | + #[tokio::test] |
| 176 | + async fn test_cookie_sign_in_with_axum_typed_request() -> Result<()> { |
| 177 | + let cf = CFrameworkSecurity::default().add_cookie(|option| { |
| 178 | + // setup option here |
| 179 | + |
| 180 | + option |
| 181 | + }); |
| 182 | + |
| 183 | + async fn authentication_handler(ctx: Context<TypedRequest<Json<JwtBody>>>) -> Result<Response, Response> { |
| 184 | + // cookie response |
| 185 | + let _res = ctx |
| 186 | + .sign_in_with_cookie(CookieState {}, CookiePayload {}) |
| 187 | + .await |
| 188 | + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR.into_response())?; |
| 189 | + |
| 190 | + let (request, _) = ctx.unpack(); |
| 191 | + |
| 192 | + Ok(String::from("Yet to be implemented").into_response()) |
| 193 | + } |
| 194 | + |
| 195 | + let app = Router::new() |
| 196 | + .route("/", post(authentication_handler)) |
| 197 | + .layer(cf); |
| 198 | + |
| 199 | + let req = Request::builder() |
| 200 | + .uri("/") |
| 201 | + .body(Body::empty())?; |
| 202 | + |
| 203 | + let res = app.oneshot(req).await?; |
| 204 | + assert_eq!(res.status(), StatusCode::OK); |
| 205 | + |
| 206 | + Ok(()) |
| 207 | + } |
| 208 | +} |
0 commit comments