nx-auth #906

Merged
philipp merged 2 commits from nx-auth into staging 2025-04-16 10:57:36 +02:00
Showing only changes of commit dc2ee38aa0 - Show all commits

View File

@ -30,6 +30,7 @@ use crate::{
}, },
SCHECKBUCH, SCHECKBUCH,
}; };
use base64::alphabet::STANDARD;
pub(crate) mod admin; pub(crate) mod admin;
mod auth; mod auth;
@ -136,35 +137,35 @@ impl<'r> FromRequest<'r> for BasicAuth {
// Get the Authorization header // Get the Authorization header
let auth_header = match request.headers().get_one("Authorization") { let auth_header = match request.headers().get_one("Authorization") {
Some(h) => h, Some(h) => h,
None => return Outcome::Failure((Status::Unauthorized, ())), None => return Outcome::Error((Status::Unauthorized, ())),
}; };
// Check if it's a Basic auth header // Check if it's a Basic auth header
if !auth_header.starts_with("Basic ") { if !auth_header.starts_with("Basic ") {
return Outcome::Failure((Status::Unauthorized, ())); return Outcome::Error((Status::Unauthorized, ()));
} }
// Decode the base64 credentials // Decode the base64 credentials
let credentials = match BASE64.decode(auth_header[6..].as_bytes()) { let credentials = match base64::decode(&auth_header[6..]) {
Ok(c) => c, Ok(c) => c,
Err(_) => return Outcome::Failure((Status::Unauthorized, ())), Err(_) => return Outcome::Error((Status::Unauthorized, ())),
}; };
// Convert to UTF-8 string // Convert to UTF-8 string
let credentials_str = match str::from_utf8(&credentials) { let credentials_str = match std::str::from_utf8(&credentials) {
Ok(s) => s, Ok(s) => s,
Err(_) => return Outcome::Failure((Status::Unauthorized, ())), Err(_) => return Outcome::Error((Status::Unauthorized, ())),
}; };
// Split into username and password // Split into username and password
let mut parts = credentials_str.splitn(2, ':'); let mut parts = credentials_str.splitn(2, ':');
let username = match parts.next() { let username = match parts.next() {
Some(u) => u.to_string(), Some(u) => u.to_string(),
None => return Outcome::Failure((Status::Unauthorized, ())), None => return Outcome::Error((Status::Unauthorized, ())),
}; };
let password = match parts.next() { let password = match parts.next() {
Some(p) => p.to_string(), Some(p) => p.to_string(),
None => return Outcome::Failure((Status::Unauthorized, ())), None => return Outcome::Error((Status::Unauthorized, ())),
}; };
Outcome::Success(BasicAuth { username, password }) Outcome::Success(BasicAuth { username, password })