Authentication how handled as seperate router
This commit is contained in:
parent
0cf00faf78
commit
e47ad30a10
4 changed files with 24 additions and 27 deletions
14
src/auth.rs
14
src/auth.rs
|
|
@ -1,4 +1,8 @@
|
|||
use axum::{http::HeaderMap, http::StatusCode, response::IntoResponse, response::Json};
|
||||
use axum::body::Body;
|
||||
use axum::{
|
||||
http::HeaderMap, http::Request, http::StatusCode, middleware::Next, response::IntoResponse,
|
||||
response::Json, response::Response,
|
||||
};
|
||||
use base64::{Engine, engine::general_purpose};
|
||||
use jsonwebtoken::{DecodingKey, EncodingKey, Header, Validation, decode, encode};
|
||||
use pam::Client;
|
||||
|
|
@ -114,6 +118,14 @@ pub fn verify_system_credentials(username: &str, password: &str) -> bool {
|
|||
client.authenticate().is_ok()
|
||||
}
|
||||
|
||||
pub async fn require_auth(headers: HeaderMap, request: Request<Body>, next: Next) -> Response {
|
||||
if verify_token(&headers) {
|
||||
next.run(request).await
|
||||
} else {
|
||||
(StatusCode::UNAUTHORIZED, "Unauthorized").into_response()
|
||||
}
|
||||
}
|
||||
|
||||
// POST /auth/login
|
||||
pub async fn post_login(headers: HeaderMap) -> impl IntoResponse {
|
||||
let (username, password) = match decode_basic_auth(&headers) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue