Compare commits
2 Commits
main
...
feat/axum-
Author | SHA1 | Date | |
---|---|---|---|
87daff4f40
|
|||
202df06568
|
34
.woodpecker/test.yml
Normal file
34
.woodpecker/test.yml
Normal file
@@ -0,0 +1,34 @@
|
||||
when:
|
||||
- event: [pull_request, tag]
|
||||
- event: push
|
||||
branch:
|
||||
- main
|
||||
|
||||
variables:
|
||||
- &rust_image 'rustlang/rust:nightly'
|
||||
|
||||
steps:
|
||||
build:
|
||||
image: *rust_image
|
||||
group: ci
|
||||
commands:
|
||||
- "cargo build"
|
||||
|
||||
test:
|
||||
image: *rust_image
|
||||
group: ci
|
||||
commands:
|
||||
- "cargo test"
|
||||
|
||||
lint:
|
||||
image: *rust_image
|
||||
group: ci
|
||||
commands:
|
||||
- "cargo clippy"
|
||||
|
||||
fmt:
|
||||
image: *rust_image
|
||||
group: ci
|
||||
commands:
|
||||
- "cargo fmt --all --check"
|
||||
|
1189
Cargo.lock
generated
1189
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -2,8 +2,8 @@ use std::fmt::Display;
|
||||
|
||||
use axum::extract::{FromRef, FromRequestParts, Query, State};
|
||||
use axum::http::request::Parts;
|
||||
use axum::http::{HeaderMap, StatusCode, Uri};
|
||||
use axum::response::{ErrorResponse, IntoResponse, Redirect, Response};
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{ErrorResponse, IntoResponse, Redirect};
|
||||
use axum::routing::get;
|
||||
use axum::{async_trait, Json, RequestPartsExt, Router};
|
||||
|
||||
@@ -97,21 +97,13 @@ pub struct UserFromSession {
|
||||
|
||||
pub static COOKIE_NAME: &str = "SESSION";
|
||||
|
||||
pub struct AuthRedirect((HeaderMap, String));
|
||||
|
||||
impl IntoResponse for AuthRedirect {
|
||||
fn into_response(self) -> Response {
|
||||
(self.0 .0, Redirect::temporary(&self.0 .1.as_str())).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for UserFromSession
|
||||
where
|
||||
AuthService: FromRef<S>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = AuthRedirect;
|
||||
type Rejection = (StatusCode, &'static str);
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let auth_service = AuthService::from_ref(state);
|
||||
@@ -122,21 +114,16 @@ where
|
||||
let basic: Option<TypedHeader<Authorization<Basic>>> = parts.extract().await.unwrap();
|
||||
|
||||
if let Some(basic) = basic {
|
||||
let token = match auth_service
|
||||
let token = auth_service
|
||||
.login_token(basic.username(), basic.password())
|
||||
.await
|
||||
.into_response()
|
||||
{
|
||||
Ok(login) => login,
|
||||
Err(e) => {
|
||||
tracing::info!("did not find a basic login token, will trigger login");
|
||||
let (headers, url) = auth_service
|
||||
.login(Some(parts.uri.to_string()))
|
||||
.await
|
||||
.expect("to be able to request login");
|
||||
return Err(AuthRedirect((headers, url.to_string())));
|
||||
}
|
||||
};
|
||||
.map_err(|_| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"could not get token from basic",
|
||||
)
|
||||
})?;
|
||||
|
||||
return Ok(UserFromSession {
|
||||
user: User {
|
||||
@@ -147,32 +134,24 @@ where
|
||||
});
|
||||
}
|
||||
|
||||
tracing::info!("did not find a cookie, will trigger login");
|
||||
let (headers, url) = auth_service
|
||||
.login(Some(parts.uri.to_string()))
|
||||
.await
|
||||
.expect("to be able to request login");
|
||||
return Err(AuthRedirect((headers, url.to_string())));
|
||||
return Err(anyhow::anyhow!("No session was found"))
|
||||
.into_response()
|
||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "did not find a cookie"))?;
|
||||
}
|
||||
|
||||
let session_cookie = session_cookie.unwrap();
|
||||
|
||||
// continue to decode the session cookie
|
||||
let user = match auth_service
|
||||
let user = auth_service
|
||||
.get_user_from_session(session_cookie)
|
||||
.await
|
||||
.into_response()
|
||||
{
|
||||
Ok(user) => user,
|
||||
Err(_) => {
|
||||
tracing::info!("could not get user from session, will trigger login");
|
||||
let (headers, url) = auth_service
|
||||
.login(Some(parts.uri.to_string()))
|
||||
.await
|
||||
.expect("to be able to request login");
|
||||
return Err(AuthRedirect((headers, url.to_string())));
|
||||
}
|
||||
};
|
||||
.map_err(|_| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"failed to decode session cookie",
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(UserFromSession { user })
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
# yaml-language-server: $schema=https://git.front.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json
|
||||
|
||||
base: "git@git.front.kjuulh.io:kjuulh/cuddle-base.git"
|
||||
base: "git@git.front.kjuulh.io:kjuulh/cuddle-rust-lib-plan.git"
|
||||
|
||||
vars:
|
||||
service: "nefarious-login"
|
||||
|
Reference in New Issue
Block a user