fabaccess-bffh/bffhd/authentication/mod.rs

131 lines
4.2 KiB
Rust
Raw Normal View History

2022-03-15 17:52:47 +01:00
use crate::users::Users;
2022-06-02 17:46:26 +02:00
use miette::{Context, IntoDiagnostic};
2022-03-15 17:52:47 +01:00
use std::sync::Arc;
2022-10-05 17:28:47 +02:00
use rsasl::callback::{CallbackError, Request, SessionCallback, SessionData};
use rsasl::mechanism::SessionError;
use rsasl::prelude::{Mechname, SASLConfig, SASLServer, Session};
use rsasl::property::AuthId;
2022-04-26 23:21:43 +02:00
use crate::authentication::fabfire::FabFireCardKey;
2022-03-16 19:29:36 +01:00
mod fabfire;
2022-03-08 18:52:49 +01:00
2022-03-13 22:50:37 +01:00
struct Callback {
users: Users,
span: tracing::Span,
2022-03-13 22:50:37 +01:00
}
impl Callback {
pub fn new(users: Users) -> Self {
let span = tracing::info_span!("SASL callback");
Self { users, span }
2022-03-13 22:50:37 +01:00
}
}
2022-10-05 17:28:47 +02:00
impl SessionCallback for Callback {
fn callback(&self, session_data: &SessionData, context: &rsasl::callback::Context, request: &mut Request) -> Result<(), SessionError> {
if let Some(authid) = context.get_ref::<AuthId>() {
request.satisfy_with::<FabFireCardKey, _>(|| {
let user = self.users.get_user(authid).ok_or(CallbackError::NoValue)?;
let kv = user.userdata.kv.get("cardkey").ok_or(CallbackError::NoValue)?;
2022-05-05 15:50:44 +02:00
let card_key = <[u8; 16]>::try_from(
2022-10-05 17:28:47 +02:00
hex::decode(kv).map_err(|_| CallbackError::NoValue)?,
).map_err(|_| CallbackError::NoValue)?;
Ok(card_key)
})?;
}
2022-10-05 17:28:47 +02:00
Ok(())
}
2022-10-05 17:28:47 +02:00
/*fn validate(
2022-03-15 17:52:47 +01:00
&self,
session: &mut SessionData,
validation: Validation,
2022-03-15 20:00:43 +01:00
_mechanism: &Mechname,
2022-03-15 17:52:47 +01:00
) -> Result<(), SessionError> {
let span = tracing::info_span!(parent: &self.span, "validate");
let _guard = span.enter();
2022-03-15 17:52:47 +01:00
match validation {
validations::SIMPLE => {
let authnid = session
.get_property::<AuthId>()
.ok_or(SessionError::no_property::<AuthId>())?;
tracing::debug!(authid=%authnid, "SIMPLE validation requested");
2022-05-05 15:50:44 +02:00
if let Some(user) = self.users.get_user(authnid.as_str()) {
let passwd = session
.get_property::<Password>()
.ok_or(SessionError::no_property::<Password>())?;
2022-03-15 17:52:47 +01:00
if user
.check_password(passwd.as_bytes())
.map_err(|_e| SessionError::AuthenticationFailure)?
{
return Ok(());
} else {
tracing::warn!(authid=%authnid, "AUTH FAILED: bad password");
}
2022-03-15 17:52:47 +01:00
} else {
tracing::warn!(authid=%authnid, "AUTH FAILED: no such user '{}'", authnid);
2022-03-15 17:52:47 +01:00
}
Err(SessionError::AuthenticationFailure)
2022-03-15 17:52:47 +01:00
}
_ => {
tracing::error!(?validation, "Unimplemented validation requested");
Err(SessionError::no_validate(validation))
2022-05-05 15:50:44 +02:00
}
2022-03-15 17:52:47 +01:00
}
2022-10-05 17:28:47 +02:00
}*/
2022-03-13 22:50:37 +01:00
}
2022-03-12 01:27:58 +01:00
struct Inner {
2022-10-05 17:28:47 +02:00
rsasl: Arc<SASLConfig>,
2022-03-12 01:27:58 +01:00
}
impl Inner {
2022-10-05 17:28:47 +02:00
pub fn new(rsasl: Arc<SASLConfig>) -> Self {
2022-03-12 01:27:58 +01:00
Self { rsasl }
}
}
2022-03-10 20:52:34 +01:00
2022-03-12 01:27:58 +01:00
#[derive(Clone)]
2022-03-12 17:31:53 +01:00
pub struct AuthenticationHandle {
2022-10-05 17:28:47 +02:00
inner: Inner,
2022-03-10 20:52:34 +01:00
}
2022-03-12 17:31:53 +01:00
impl AuthenticationHandle {
2022-03-13 22:50:37 +01:00
pub fn new(userdb: Users) -> Self {
2022-03-16 19:29:36 +01:00
let span = tracing::debug_span!("authentication");
let _guard = span.enter();
2022-10-05 17:28:47 +02:00
let config = SASLConfig::builder()
.with_defaults()
.with_callback(Callback::new(userdb))
.unwrap();
2022-03-16 19:29:36 +01:00
2022-10-05 17:28:47 +02:00
let mechs: Vec<&'static str> = SASLServer::new(config.clone())
.get_available()
2022-05-05 15:50:44 +02:00
.into_iter()
2022-03-16 19:29:36 +01:00
.map(|m| m.mechanism.as_str())
.collect();
2022-05-05 15:50:44 +02:00
tracing::info!(available_mechs = mechs.len(), "initialized sasl backend");
2022-03-16 19:29:36 +01:00
tracing::debug!(?mechs, "available mechs");
2022-03-15 17:52:47 +01:00
Self {
2022-10-05 17:28:47 +02:00
inner: Inner::new(config),
2022-03-15 17:52:47 +01:00
}
2022-03-12 01:27:58 +01:00
}
2022-03-10 20:52:34 +01:00
2022-06-02 17:46:26 +02:00
pub fn start(&self, mechanism: &Mechname) -> miette::Result<Session> {
2022-10-05 17:28:47 +02:00
Ok(SASLServer::new(self.inner.rsasl.clone())
.start_suggested(mechanism)
2022-06-02 17:46:26 +02:00
.into_diagnostic()
.wrap_err("Failed to start a SASL authentication with the given mechanism")?)
2022-03-12 17:31:53 +01:00
}
2022-03-15 17:52:47 +01:00
pub fn list_available_mechs(&self) -> impl IntoIterator<Item = &Mechname> {
2022-10-05 17:28:47 +02:00
SASLServer::new(self.inner.rsasl.clone())
.get_available()
2022-03-15 17:52:47 +01:00
.into_iter()
.map(|m| m.mechanism)
2022-03-12 01:27:58 +01:00
}
2022-03-15 17:52:47 +01:00
}