async fn my_authentication_filter(
state: RequestHeadersState,
authentication: Authentication,
validator: &ContractValidator,
) -> Flow<()> {
// Extract credentials
let (client_id, client_secret) = match basic_auth_credentials(&state) {
Ok(credentials) => credentials,
Err(e) => {
logger::info!("Invalid credentials: {e}");
// For simplicity, we are using a user-defined `unathorized_response()`
// helper function for building responses.
return Flow::Break(unauthorized_response("Invalid credentials", 401));
}
};
// Validate authentication
let validation = validator.authenticate(&client_id, &client_secret);
let client_data = match validation {
Ok(client_data) => client_data,
Err(e) => {
logger::info!("Invalid authentication: {e}");
return Flow::Break(unauthorized_response("Invalid authentication", 403));
}
};
// Update the current authentication
if let Some(mut auth) = authentication.authentication() {
auth.client_id = Some(client_data.client_id);
auth.client_name = Some(client_data.client_name);
authentication.set_authentication(Some(&auth));
}
Flow::Continue(())
}