Skip to main content

extract_basic_auth

Function extract_basic_auth 

Source
pub fn extract_basic_auth(auth_header: &str) -> Option<(String, String)>
Expand description

Extract basic auth credentials from request.

Parses a Basic Authentication header and returns the username and password. The header format should be: Basic <base64-encoded-credentials> where credentials are in the format username:password.

§Examples

use hammerwork_web::auth::extract_basic_auth;

// Valid basic auth header
let auth_header = "Basic YWRtaW46cGFzc3dvcmQ="; // admin:password
let (username, password) = extract_basic_auth(auth_header).unwrap();
assert_eq!(username, "admin");
assert_eq!(password, "password");

// Invalid format returns None
assert!(extract_basic_auth("Bearer token123").is_none());
assert!(extract_basic_auth("Basic invalid_base64").is_none());

§Returns

  • Some((username, password)) if the header is valid
  • None if the header is malformed or not a Basic auth header