|
| 1 | +use std::borrow::Cow; |
| 2 | + |
| 3 | +use rsasl::callback::{Context, Request, SessionCallback, SessionData}; |
| 4 | +use rsasl::mechanisms::gssapi::properties::GssService; |
| 5 | +use rsasl::prelude::*; |
| 6 | +use rsasl::property::Hostname; |
| 7 | + |
| 8 | +use super::{Result, SaslInitiator, SaslInnerOptions, SaslOptions, SaslSession}; |
| 9 | + |
| 10 | +impl From<GssapiSaslOptions> for SaslOptions { |
| 11 | + fn from(options: GssapiSaslOptions) -> Self { |
| 12 | + Self(SaslInnerOptions::Gssapi(options)) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/// GSSAPI SASL options. |
| 17 | +#[derive(Clone, Debug)] |
| 18 | +pub struct GssapiSaslOptions { |
| 19 | + username: Cow<'static, str>, |
| 20 | + hostname: Option<Cow<'static, str>>, |
| 21 | +} |
| 22 | + |
| 23 | +impl GssapiSaslOptions { |
| 24 | + pub(crate) fn new() -> Self { |
| 25 | + Self { username: Cow::from("zookeeper"), hostname: None } |
| 26 | + } |
| 27 | + |
| 28 | + /// Specifies the primary part of Kerberos principal. |
| 29 | + /// |
| 30 | + /// It is `zookeeper.sasl.client.username` in Java client, but the word "client" is misleading |
| 31 | + /// as it is the username of targeting server. |
| 32 | + /// |
| 33 | + /// Defaults to "zookeeper". |
| 34 | + pub fn with_username(self, username: impl Into<Cow<'static, str>>) -> Self { |
| 35 | + Self { username: username.into(), ..self } |
| 36 | + } |
| 37 | + |
| 38 | + /// Specifies the instance part of Kerberos principal. |
| 39 | + /// |
| 40 | + /// Defaults to hostname or ip of targeting server in connecting string. |
| 41 | + pub fn with_hostname(self, hostname: impl Into<Cow<'static, str>>) -> Self { |
| 42 | + Self { hostname: Some(hostname.into()), ..self } |
| 43 | + } |
| 44 | + |
| 45 | + fn hostname_or(&self, hostname: &str) -> Cow<'static, str> { |
| 46 | + match self.hostname.as_ref() { |
| 47 | + None => Cow::Owned(hostname.to_string()), |
| 48 | + Some(hostname) => hostname.clone(), |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl SaslInitiator for GssapiSaslOptions { |
| 54 | + fn new_session(&self, hostname: &str) -> Result<SaslSession> { |
| 55 | + struct GssapiOptionsProvider { |
| 56 | + username: Cow<'static, str>, |
| 57 | + hostname: Cow<'static, str>, |
| 58 | + } |
| 59 | + impl SessionCallback for GssapiOptionsProvider { |
| 60 | + fn callback( |
| 61 | + &self, |
| 62 | + _session_data: &SessionData, |
| 63 | + _context: &Context, |
| 64 | + request: &mut Request<'_>, |
| 65 | + ) -> Result<(), SessionError> { |
| 66 | + if request.is::<Hostname>() { |
| 67 | + request.satisfy::<Hostname>(&self.hostname)?; |
| 68 | + } else if request.is::<GssService>() { |
| 69 | + request.satisfy::<GssService>(&self.username)?; |
| 70 | + } |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | + } |
| 74 | + let provider = GssapiOptionsProvider { username: self.username.clone(), hostname: self.hostname_or(hostname) }; |
| 75 | + let config = SASLConfig::builder().with_defaults().with_callback(provider).unwrap(); |
| 76 | + let client = SASLClient::new(config); |
| 77 | + let session = client.start_suggested(&[Mechname::parse(b"GSSAPI").unwrap()]).unwrap(); |
| 78 | + SaslSession::new(session) |
| 79 | + } |
| 80 | +} |
0 commit comments